@polka-codes/core 0.4.0 → 0.4.2

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 +112 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2351,6 +2351,35 @@ Anthropic.Beta = Beta;
2351
2351
 
2352
2352
  // src/AiService/AiServiceBase.ts
2353
2353
  class AiServiceBase {
2354
+ async request(systemPrompt, messages) {
2355
+ const stream = this.send(systemPrompt, messages);
2356
+ const usage = {
2357
+ inputTokens: 0,
2358
+ outputTokens: 0,
2359
+ cacheWriteTokens: 0,
2360
+ cacheReadTokens: 0,
2361
+ totalCost: 0
2362
+ };
2363
+ let resp = "";
2364
+ for await (const chunk of stream) {
2365
+ switch (chunk.type) {
2366
+ case "usage":
2367
+ usage.inputTokens = chunk.inputTokens;
2368
+ usage.outputTokens = chunk.outputTokens;
2369
+ usage.cacheWriteTokens = chunk.cacheWriteTokens ?? 0;
2370
+ usage.cacheReadTokens = chunk.cacheReadTokens ?? 0;
2371
+ usage.totalCost = chunk.totalCost;
2372
+ break;
2373
+ case "text":
2374
+ resp += chunk.text;
2375
+ break;
2376
+ }
2377
+ }
2378
+ return {
2379
+ response: resp,
2380
+ usage
2381
+ };
2382
+ }
2354
2383
  }
2355
2384
 
2356
2385
  // src/AiService/ModelInfo.ts
@@ -8190,7 +8219,12 @@ var handler6 = async (provider, args) => {
8190
8219
  const resp = [];
8191
8220
  for (const path of paths) {
8192
8221
  const fileContent = await provider.readFile(path);
8193
- resp.push(`<read_file_file_conten path="${path}">${fileContent}</read_file_file_content>`);
8222
+ const isEmpty = fileContent.trim().length === 0;
8223
+ if (isEmpty) {
8224
+ resp.push(`<read_file_file_content path="${path}" is_empty="true" />`);
8225
+ } else {
8226
+ resp.push(`<read_file_file_conten path="${path}">${fileContent}</read_file_file_content>`);
8227
+ }
8194
8228
  }
8195
8229
  return {
8196
8230
  type: "Reply" /* Reply */,
@@ -8662,14 +8696,91 @@ class CoderAgent extends AgentBase {
8662
8696
  });
8663
8697
  }
8664
8698
  }
8699
+ // src/AiTool/generateGitCommitMessage.ts
8700
+ var prompt = `
8701
+ You are an advanced assistant specialized in creating concise and accurate Git commit messages. When you receive:
8702
+ - A Git diff inside the <tool_input> tag.
8703
+ - Additional user-supplied context inside the <tool_input_context> tag (if any).
8704
+
8705
+ You will produce a single commit message enclosed within <tool_output> tags. The commit message must accurately reflect the changes shown in the diff and should be clear, descriptive, and devoid of unnecessary or repeated information. If a context is provided, it MUST be incorporated into the commit message.
8706
+
8707
+ Here’s an example of the input and the expected output format:
8708
+
8709
+ \`\`\`
8710
+ <tool_input>
8711
+ --- a/example_file.py
8712
+ +++ b/example_file.py
8713
+ @@ -10,7 +10,7 @@ def example_function():
8714
+ - print("Old behavior")
8715
+ + print("New behavior")
8716
+ </tool_input>
8717
+ <tool_input_context>
8718
+ Changing print statement to update the user-facing message.
8719
+ </tool_input_context>
8720
+ \`\`\`
8721
+
8722
+ Example Output:
8723
+
8724
+ \`\`\`
8725
+ <tool_output>
8726
+ Update print statement for revised user-facing message
8727
+ </tool_output>
8728
+ \`\`\`
8729
+
8730
+ Follow the same structure for any new input. Never repeat questions; focus on generating a concise commit message that captures the essence of the changes.
8731
+ `;
8732
+ var generateGitCommitMessage_default = {
8733
+ name: "generateGitCommitMessage",
8734
+ description: "Generates git commit messages from git diff output",
8735
+ prompt,
8736
+ formatInput: (params) => {
8737
+ let ret = `<tool_input>
8738
+ ${params.diff}
8739
+ </tool_input>`;
8740
+ if (params.context) {
8741
+ ret += `
8742
+ <tool_input_context>
8743
+ ${params.context}
8744
+ </tool_input_context>`;
8745
+ }
8746
+ return ret;
8747
+ },
8748
+ parseOutput: (output) => {
8749
+ const regex = /<tool_output>([\s\S]*)<\/tool_output>/gm;
8750
+ const match = regex.exec(output);
8751
+ if (match) {
8752
+ return match[1];
8753
+ }
8754
+ throw new Error(`Could not parse output:
8755
+ ${output}`);
8756
+ }
8757
+ };
8758
+
8759
+ // src/AiTool/index.ts
8760
+ var executeTool = async (definition, ai, params) => {
8761
+ const { response, usage } = await ai.request(definition.prompt, [{ role: "user", content: definition.formatInput(params) }]);
8762
+ return {
8763
+ response: definition.parseOutput(response),
8764
+ usage
8765
+ };
8766
+ };
8767
+ var makeTool = (definition) => {
8768
+ return async (ai, params) => {
8769
+ return executeTool(definition, ai, params);
8770
+ };
8771
+ };
8772
+ var generateGitCommitMessage = makeTool(generateGitCommitMessage_default);
8665
8773
  export {
8666
8774
  writeToFile_default as writeToFile,
8667
8775
  searchFiles_default as searchFiles,
8668
8776
  replaceInFile_default as replaceInFile,
8669
8777
  readFile_default as readFile,
8670
8778
  openAiModelInfoSaneDefaults,
8779
+ makeTool,
8671
8780
  listFiles_default as listFiles,
8672
8781
  listCodeDefinitionNames_default as listCodeDefinitionNames,
8782
+ generateGitCommitMessage,
8783
+ executeTool,
8673
8784
  executeCommand_default as executeCommand,
8674
8785
  defaultModels,
8675
8786
  deepSeekModels,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "license": "AGPL-3.0",
5
5
  "type": "module",
6
6
  "exports": {