@polka-codes/core 0.8.20 → 0.8.21

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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { replaceInFileHelper } from './_tsup-dts-rollup.js';
2
- export { editFileHelper } from './_tsup-dts-rollup.js';
3
2
  export { AiServiceProvider } from './_tsup-dts-rollup.js';
4
3
  export { defaultModels } from './_tsup-dts-rollup.js';
5
4
  export { createService } from './_tsup-dts-rollup.js';
@@ -62,7 +61,6 @@ export { ToolUse } from './_tsup-dts-rollup.js';
62
61
  export { AssistantMessageContent } from './_tsup-dts-rollup.js';
63
62
  export { KnowledgeManagementPolicy } from './_tsup-dts-rollup.js';
64
63
  export { TruncateContextPolicy } from './_tsup-dts-rollup.js';
65
- export { editFile } from './_tsup-dts-rollup.js';
66
64
  export { allTools } from './_tsup-dts-rollup.js';
67
65
  export { FilesystemProvider } from './_tsup-dts-rollup.js';
68
66
  export { CommandProvider } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -914,40 +914,6 @@ var ToolResponseType = /* @__PURE__ */ ((ToolResponseType2) => {
914
914
  return ToolResponseType2;
915
915
  })(ToolResponseType || {});
916
916
 
917
- // src/tools/utils/editFile.ts
918
- var START_OF_FILE = "<<<START_OF_FILE>>>";
919
- var END_OF_FILE = "<<<END_OF_FILE>>>";
920
- var editFile = async (fileContent, operations) => {
921
- if (!operations || operations.length === 0) {
922
- throw new Error("At least one edit operation is required");
923
- }
924
- let updatedContent = fileContent;
925
- for (const operation of operations) {
926
- updatedContent = await applyEditOperation(updatedContent, operation);
927
- }
928
- return updatedContent;
929
- };
930
- async function applyEditOperation(fileContent, operation) {
931
- const { search, replace } = operation;
932
- if (search === START_OF_FILE && replace === END_OF_FILE) {
933
- throw new Error("Cannot search for START_OF_FILE and replace with END_OF_FILE");
934
- }
935
- if (search === END_OF_FILE && replace === START_OF_FILE) {
936
- throw new Error("Cannot search for END_OF_FILE and replace with START_OF_FILE");
937
- }
938
- if (search === START_OF_FILE) {
939
- return replace + fileContent;
940
- }
941
- if (search === END_OF_FILE) {
942
- return fileContent + replace;
943
- }
944
- const index = fileContent.indexOf(search);
945
- if (index === -1) {
946
- throw new Error(`Could not find text: ${search}`);
947
- }
948
- return fileContent.slice(0, index) + replace + fileContent.slice(index + search.length);
949
- }
950
-
951
917
  // src/tools/utils/getArg.ts
952
918
  var getString = (args, name, defaultValue) => {
953
919
  if (typeof args !== "object" || Array.isArray(args)) {
@@ -1559,29 +1525,29 @@ var toolInfo7 = {
1559
1525
  {
1560
1526
  name: "diff",
1561
1527
  description: `One or more SEARCH/REPLACE blocks following this exact format:
1562
- \`\`\`
1563
- <<<<<<< SEARCH
1564
- [exact content to find]
1565
- =======
1566
- [new content to replace with]
1567
- >>>>>>> REPLACE
1568
- \`\`\`
1569
- Critical rules:
1570
- 1. SEARCH content must match the associated file section to find EXACTLY:
1571
- * Match character-for-character including whitespace, indentation, line endings
1572
- * Include all comments, docstrings, etc.
1573
- 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence.
1574
- * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes.
1575
- * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change.
1576
- * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file.
1577
- 3. Keep SEARCH/REPLACE blocks concise:
1578
- * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file.
1579
- * Include just the changing lines, and a few surrounding lines if needed for uniqueness.
1580
- * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks.
1581
- * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures.
1582
- 4. Special operations:
1583
- * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location)
1584
- * To delete code: Use empty REPLACE section`,
1528
+ \`\`\`
1529
+ <<<<<<< SEARCH
1530
+ [exact content to find]
1531
+ =======
1532
+ [new content to replace with]
1533
+ >>>>>>> REPLACE
1534
+ \`\`\`
1535
+ Critical rules:
1536
+ 1. SEARCH content must match the associated file section to find EXACTLY:
1537
+ * Match character-for-character including whitespace, indentation, line endings
1538
+ * Include all comments, docstrings, etc.
1539
+ 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence.
1540
+ * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes.
1541
+ * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change.
1542
+ * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file.
1543
+ 3. Keep SEARCH/REPLACE blocks concise:
1544
+ * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file.
1545
+ * Include just the changing lines, and a few surrounding lines if needed for uniqueness.
1546
+ * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks.
1547
+ * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures.
1548
+ 4. Special operations:
1549
+ * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location)
1550
+ * To delete code: Use empty REPLACE section`,
1585
1551
  required: true,
1586
1552
  usageValue: "Search and replace blocks here"
1587
1553
  }
@@ -1624,6 +1590,73 @@ function handleSubmit() {
1624
1590
  return (
1625
1591
  <div>
1626
1592
  >>>>>>> REPLACE
1593
+ `
1594
+ }
1595
+ ]
1596
+ },
1597
+ {
1598
+ description: "Request to perform a simple, single-line replacement",
1599
+ parameters: [
1600
+ {
1601
+ name: "path",
1602
+ value: "src/config.js"
1603
+ },
1604
+ {
1605
+ name: "diff",
1606
+ value: `
1607
+ <<<<<<< SEARCH
1608
+ const API_URL = 'https://api.example.com';
1609
+ =======
1610
+ const API_URL = 'https://api.staging.example.com';
1611
+ >>>>>>> REPLACE
1612
+ `
1613
+ }
1614
+ ]
1615
+ },
1616
+ {
1617
+ description: "Request to add a new function to a file",
1618
+ parameters: [
1619
+ {
1620
+ name: "path",
1621
+ value: "src/utils.js"
1622
+ },
1623
+ {
1624
+ name: "diff",
1625
+ value: `
1626
+ <<<<<<< SEARCH
1627
+ function helperA() {
1628
+ // ...
1629
+ }
1630
+ =======
1631
+ function helperA() {
1632
+ // ...
1633
+ }
1634
+
1635
+ function newHelper() {
1636
+ // implementation
1637
+ }
1638
+ >>>>>>> REPLACE
1639
+ `
1640
+ }
1641
+ ]
1642
+ },
1643
+ {
1644
+ description: "Request to delete a block of code from a file",
1645
+ parameters: [
1646
+ {
1647
+ name: "path",
1648
+ value: "src/app.js"
1649
+ },
1650
+ {
1651
+ name: "diff",
1652
+ value: `
1653
+ <<<<<<< SEARCH
1654
+ function oldFeature() {
1655
+ // This is no longer needed
1656
+ }
1657
+
1658
+ =======
1659
+ >>>>>>> REPLACE
1627
1660
  `
1628
1661
  }
1629
1662
  ]
@@ -2251,158 +2284,6 @@ var renameFile_default = {
2251
2284
  isAvailable: isAvailable13
2252
2285
  };
2253
2286
 
2254
- // src/tools/editFile.ts
2255
- var toolInfo14 = {
2256
- name: "edit_file",
2257
- description: "Request to edit file contents using search/replace operations. Supports multiple edit operations in a single call.",
2258
- parameters: [
2259
- {
2260
- name: "path",
2261
- description: "The path of the file to edit",
2262
- required: true,
2263
- usageValue: "File path here"
2264
- },
2265
- {
2266
- name: "operations",
2267
- description: "Edit operation with search and replace parameters",
2268
- required: true,
2269
- allowMultiple: true,
2270
- children: [
2271
- {
2272
- name: "search",
2273
- description: `Text to search for and replace (use ${START_OF_FILE} to insert at file start, ${END_OF_FILE} to insert at file end)`,
2274
- required: true,
2275
- usageValue: "Text to find and replace"
2276
- },
2277
- {
2278
- name: "replace",
2279
- description: "Text to replace the search text with",
2280
- required: true,
2281
- usageValue: "Replacement text"
2282
- }
2283
- ],
2284
- usageValue: "operations here"
2285
- }
2286
- ],
2287
- examples: [
2288
- {
2289
- description: "Replace specific text",
2290
- parameters: [
2291
- {
2292
- name: "path",
2293
- value: "src/main.ts"
2294
- },
2295
- {
2296
- name: "operations",
2297
- value: {
2298
- search: "function oldFunction() {\n return 42;\n}",
2299
- replace: 'function newFunction() {\n return "new implementation";\n}'
2300
- }
2301
- }
2302
- ]
2303
- },
2304
- {
2305
- description: "Insert at start of file",
2306
- parameters: [
2307
- {
2308
- name: "path",
2309
- value: "src/header.ts"
2310
- },
2311
- {
2312
- name: "operations",
2313
- value: {
2314
- search: START_OF_FILE,
2315
- replace: "// File header comment\n"
2316
- }
2317
- }
2318
- ]
2319
- },
2320
- {
2321
- description: "Multiple operations in one call",
2322
- parameters: [
2323
- {
2324
- name: "path",
2325
- value: "src/utils.ts"
2326
- },
2327
- {
2328
- name: "operations",
2329
- value: [
2330
- {
2331
- search: 'import React from "react"',
2332
- replace: 'import React, { useState } from "react"'
2333
- },
2334
- {
2335
- search: "function Component() {\n return (",
2336
- replace: "function Component() {\n const [state, setState] = useState(false);\n return ("
2337
- }
2338
- ]
2339
- }
2340
- ]
2341
- },
2342
- {
2343
- description: "Append content at end of file",
2344
- parameters: [
2345
- {
2346
- name: "path",
2347
- value: "src/footer.ts"
2348
- },
2349
- {
2350
- name: "operations",
2351
- value: {
2352
- search: END_OF_FILE,
2353
- replace: "\n// End of file appended comment\n"
2354
- }
2355
- }
2356
- ]
2357
- }
2358
- ],
2359
- permissionLevel: 2 /* Write */
2360
- };
2361
- var handler14 = async (provider, args) => {
2362
- if (!provider.readFile || !provider.writeFile) {
2363
- return {
2364
- type: "Error" /* Error */,
2365
- message: "Not possible to edit file. Abort."
2366
- };
2367
- }
2368
- const path = getString(args, "path");
2369
- const operations = getArray(args, "operations");
2370
- if (!operations || operations.length === 0) {
2371
- return {
2372
- type: "Error" /* Error */,
2373
- message: `<error><edit_file_path>${path}</edit_file_path><error_message>At least one edit operation is required</error_message></error>`
2374
- };
2375
- }
2376
- const fileContent = await provider.readFile(path);
2377
- if (fileContent == null) {
2378
- return {
2379
- type: "Error" /* Error */,
2380
- message: `<error><edit_file_path>${path}</edit_file_path><error_message>File not found</error_message></error>`
2381
- };
2382
- }
2383
- try {
2384
- const result = await editFile(fileContent, operations);
2385
- await provider.writeFile(path, result);
2386
- return {
2387
- type: "Reply" /* Reply */,
2388
- message: `<edit_file_path>${path}</edit_file_path>`
2389
- };
2390
- } catch (error) {
2391
- return {
2392
- type: "Error" /* Error */,
2393
- message: `<error><edit_file_path>${path}</edit_file_path><error_message>${error instanceof Error ? error.message : String(error)}</error_message></error>`
2394
- };
2395
- }
2396
- };
2397
- var isAvailable14 = (provider) => {
2398
- return !!provider.readFile && !!provider.writeFile;
2399
- };
2400
- var editFile_default = {
2401
- ...toolInfo14,
2402
- handler: handler14,
2403
- isAvailable: isAvailable14
2404
- };
2405
-
2406
2287
  // src/getAvailableTools.ts
2407
2288
  var getAvailableTools = ({
2408
2289
  provider,
@@ -2623,6 +2504,8 @@ Tool use is formatted using XML-style tags. The tool name is enclosed in opening
2623
2504
  ...
2624
2505
  </${toolNamePrefix}tool_name>
2625
2506
 
2507
+ **It is crucial that all tags are correctly nested and closed.**
2508
+
2626
2509
  ## Array Parameters
2627
2510
 
2628
2511
  To create an array of values for a parameter, repeat the parameter tag multiple times:
@@ -2657,7 +2540,7 @@ You can also combine array parameters with nested objects:
2657
2540
  </${parameterPrefix}key>
2658
2541
  </${toolNamePrefix}example_tool>
2659
2542
 
2660
- Always adhere to this format for the tool use to ensure proper parsing and execution.
2543
+ Always adhere to this format, ensuring every opening tag has a matching closing tag, to ensure proper parsing and execution.
2661
2544
 
2662
2545
  NEVER surround tool use with triple backticks (\`\`\`).
2663
2546
 
@@ -3009,8 +2892,8 @@ ${instance.prompt}`;
3009
2892
  }
3010
2893
  async #invokeTool(name, args) {
3011
2894
  try {
3012
- const handler15 = this.handlers[name]?.handler;
3013
- if (!handler15) {
2895
+ const handler14 = this.handlers[name]?.handler;
2896
+ if (!handler14) {
3014
2897
  return {
3015
2898
  type: "Error" /* Error */,
3016
2899
  message: responsePrompts.errorInvokeTool(name, "Tool not found"),
@@ -3029,7 +2912,7 @@ ${instance.prompt}`;
3029
2912
  if (resp) {
3030
2913
  return resp;
3031
2914
  }
3032
- return await handler15(this.config.provider, args);
2915
+ return await handler14(this.config.provider, args);
3033
2916
  } catch (error) {
3034
2917
  return {
3035
2918
  type: "Error" /* Error */,
@@ -3387,7 +3270,7 @@ var editingFilesPrompt = (toolNamePrefix) => `
3387
3270
 
3388
3271
  EDITING FILES
3389
3272
 
3390
- You have two file-manipulation tools: **${toolNamePrefix}write_to_file** (full overwrite) and **${toolNamePrefix}edit_file** (targeted anchor-based edits). Choose the smallest safe operation for every change.
3273
+ You have two file-manipulation tools: **${toolNamePrefix}write_to_file** (full overwrite) and **${toolNamePrefix}replace_in_file** (targeted anchor-based edits). Choose the smallest safe operation for every change.
3391
3274
 
3392
3275
  # ${toolNamePrefix}write_to_file
3393
3276
 
@@ -3399,16 +3282,16 @@ You have two file-manipulation tools: **${toolNamePrefix}write_to_file** (full o
3399
3282
 
3400
3283
  - Initial file creation, such as when scaffolding a new project.
3401
3284
  - Overwriting large boilerplate files where you want to replace the entire content at once.
3402
- - When the complexity or number of changes would make ${toolNamePrefix}edit_file unwieldy or error-prone.
3285
+ - When the complexity or number of changes would make ${toolNamePrefix}replace_in_file unwieldy or error-prone.
3403
3286
  - When you need to completely restructure a file's content or change its fundamental organization.
3404
3287
 
3405
3288
  ## Important Considerations
3406
3289
 
3407
3290
  - Using ${toolNamePrefix}write_to_file requires providing the file's complete final content.
3408
- - If you only need to make small changes to an existing file, consider using ${toolNamePrefix}edit_file instead to avoid unnecessarily rewriting the entire file.
3291
+ - If you only need to make small changes to an existing file, consider using ${toolNamePrefix}replace_in_file instead to avoid unnecessarily rewriting the entire file.
3409
3292
  - While ${toolNamePrefix}write_to_file should not be your default choice, don't hesitate to use it when the situation truly calls for it.
3410
3293
 
3411
- # ${toolNamePrefix}edit_file
3294
+ # ${toolNamePrefix}replace_in_file
3412
3295
 
3413
3296
  ## Purpose
3414
3297
 
@@ -3427,10 +3310,10 @@ You have two file-manipulation tools: **${toolNamePrefix}write_to_file** (full o
3427
3310
 
3428
3311
  # Choosing the Appropriate Tool
3429
3312
 
3430
- - **Default to ${toolNamePrefix}edit_file** for most changes. It keeps diffs small and reduces risk.
3313
+ - **Default to ${toolNamePrefix}replace_in_file** for most changes. It keeps diffs small and reduces risk.
3431
3314
  - **Use ${toolNamePrefix}write_to_file** when:
3432
3315
  - Creating new files
3433
- - The changes are so extensive that using ${toolNamePrefix}edit_file would be more complex or risky
3316
+ - The changes are so extensive that using ${toolNamePrefix}replace_in_file would be more complex or risky
3434
3317
  - You need to completely reorganize or restructure a file
3435
3318
  - The file is relatively small and the changes affect most of its content
3436
3319
  - You're generating boilerplate or template files
@@ -3438,9 +3321,9 @@ You have two file-manipulation tools: **${toolNamePrefix}write_to_file** (full o
3438
3321
  # Workflow Tips
3439
3322
 
3440
3323
  1. Before editing, assess the scope of your changes and decide which tool to use.
3441
- 2. For targeted edits, apply ${toolNamePrefix}edit_file with carefully crafted before/after text anchors. If you need multiple changes, you can stack multiple operations within a single ${toolNamePrefix}edit_file call.
3324
+ 2. For targeted edits, apply ${toolNamePrefix}replace_in_file with carefully crafted before/after text anchors. If you need multiple changes, you can stack multiple operations within a single ${toolNamePrefix}replace_in_file call.
3442
3325
  3. For major overhauls or initial file creation, rely on ${toolNamePrefix}write_to_file.
3443
- 4. Once the file has been edited with either ${toolNamePrefix}write_to_file or ${toolNamePrefix}edit_file, the system will provide you with the final state of the modified file. Use this updated content as the reference point for any subsequent operations, since it reflects any auto-formatting or user-applied changes.
3326
+ 4. Once the file has been edited with either ${toolNamePrefix}write_to_file or ${toolNamePrefix}replace_in_file, the system will provide you with the final state of the modified file. Use this updated content as the reference point for any subsequent operations, since it reflects any auto-formatting or user-applied changes.
3444
3327
 
3445
3328
  Picking the right tool keeps edits minimal, safe, and easy to review.
3446
3329
  `;
@@ -3454,10 +3337,10 @@ RULES
3454
3337
  For text files (e.g. README.md), append a footer with the same notice.
3455
3338
  - Never describe what changed inside code comments; comments must focus on purpose or usage only.
3456
3339
  - Before using ${toolNamePrefix}execute_command, consider SYSTEM INFORMATION to ensure commands suit the user's OS. If a command must run in a subdirectory, prepend a single \`cd childDir &&\` segment.
3457
- - Use ${toolNamePrefix}search_files for broad analysis, then ${toolNamePrefix}read_file to inspect context, and finally ${toolNamePrefix}edit_file or ${toolNamePrefix}write_to_file to modify.
3458
- - Prefer ${toolNamePrefix}edit_file for focused edits; choose ${toolNamePrefix}write_to_file for new files or complete rewrites.
3340
+ - Use ${toolNamePrefix}search_files for broad analysis, then ${toolNamePrefix}read_file to inspect context, and finally ${toolNamePrefix}replace_in_file or ${toolNamePrefix}write_to_file to modify.
3341
+ - Prefer ${toolNamePrefix}replace_in_file for focused edits; choose ${toolNamePrefix}write_to_file for new files or complete rewrites.
3459
3342
  - When creating a new file, look for existing files with similar content or patterns; if found, read them and use their structure or conventions as a reference.
3460
- - Use before/after text anchors in ${toolNamePrefix}edit_file to target changes. If multiple operations are needed, list them in file order.
3343
+ - Use before/after text anchors in ${toolNamePrefix}replace_in_file to target changes. If multiple operations are needed, list them in file order.
3461
3344
  - Do not guess unseen content. Read existing files first unless creating new ones.
3462
3345
  - Follow existing style, lint, and naming conventions. Ensure all changes compile and pass tests where applicable.
3463
3346
  - ALWAYS wait for the user's confirmation after each tool call before starting the next step.
@@ -4356,8 +4239,6 @@ export {
4356
4239
  deepSeekModels,
4357
4240
  defaultModels,
4358
4241
  delegate_default as delegate,
4359
- editFile_default as editFile,
4360
- editFile as editFileHelper,
4361
4242
  executeAgentTool,
4362
4243
  executeCommand_default as executeCommand,
4363
4244
  executeTool,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.8.20",
3
+ "version": "0.8.21",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",