@lvce-editor/chat-tool-worker 7.5.0 → 7.7.0

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.
@@ -994,6 +994,66 @@ const executeClosePreviewTool = async (_args, _options) => {
994
994
  }
995
995
  };
996
996
 
997
+ const applyTextEdit = (content, start, end, text) => {
998
+ if (start > content.length || end > content.length) {
999
+ throw new Error('Invalid argument: edit range is out of bounds.');
1000
+ }
1001
+ return `${content.slice(0, start)}${text}${content.slice(end)}`;
1002
+ };
1003
+
1004
+ const ABSOLUTE_URI_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//;
1005
+ const isAbsoluteUri = value => {
1006
+ return ABSOLUTE_URI_REGEX.test(value);
1007
+ };
1008
+
1009
+ const executeEditFileTool = async (args, _options) => {
1010
+ const uri = typeof args.uri === 'string' ? args.uri : '';
1011
+ const start = typeof args.start === 'number' ? args.start : -1;
1012
+ const end = typeof args.end === 'number' ? args.end : -1;
1013
+ const text = typeof args.text === 'string' ? args.text : '';
1014
+ if (!uri || !isAbsoluteUri(uri)) {
1015
+ return {
1016
+ error: 'Invalid argument: uri must be an absolute URI.'
1017
+ };
1018
+ }
1019
+ try {
1020
+ new URL(uri);
1021
+ } catch {
1022
+ return {
1023
+ error: 'Invalid argument: invalid URL.'
1024
+ };
1025
+ }
1026
+ if (!Number.isInteger(start) || start < 0) {
1027
+ return {
1028
+ error: 'Invalid argument: start must be a non-negative integer.'
1029
+ };
1030
+ }
1031
+ if (!Number.isInteger(end) || end < 0) {
1032
+ return {
1033
+ error: 'Invalid argument: end must be a non-negative integer.'
1034
+ };
1035
+ }
1036
+ if (start > end) {
1037
+ return {
1038
+ error: 'Invalid argument: start must be less than or equal to end.'
1039
+ };
1040
+ }
1041
+ try {
1042
+ const content = await readFile(uri);
1043
+ const nextContent = applyTextEdit(content, start, end, text);
1044
+ await writeFile(uri, nextContent);
1045
+ return {
1046
+ ok: true,
1047
+ uri
1048
+ };
1049
+ } catch (error) {
1050
+ return {
1051
+ ...getToolErrorPayload(error),
1052
+ uri
1053
+ };
1054
+ }
1055
+ };
1056
+
997
1057
  const executeGetWorkspaceUriTool = async (_args, _options) => {
998
1058
  try {
999
1059
  const workspaceUri = await getWorkspacePath();
@@ -1005,11 +1065,6 @@ const executeGetWorkspaceUriTool = async (_args, _options) => {
1005
1065
  }
1006
1066
  };
1007
1067
 
1008
- const ABSOLUTE_URI_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//;
1009
- const isAbsoluteUri = value => {
1010
- return ABSOLUTE_URI_REGEX.test(value);
1011
- };
1012
-
1013
1068
  const executeListFilesTool = async (args, _options) => {
1014
1069
  const uri = typeof args.uri === 'string' ? args.uri : '';
1015
1070
  if (!uri || !isAbsoluteUri(uri)) {
@@ -1123,6 +1178,95 @@ const executeRenderHtmlTool = async (args, _options) => {
1123
1178
  };
1124
1179
  };
1125
1180
 
1181
+ const getRunInTerminalOptions = args => {
1182
+ const {
1183
+ options
1184
+ } = args;
1185
+ if (!options || typeof options !== 'object') {
1186
+ return undefined;
1187
+ }
1188
+ const candidate = options;
1189
+ const {
1190
+ command,
1191
+ shell
1192
+ } = candidate;
1193
+ if (typeof command !== 'string' || typeof shell !== 'string') {
1194
+ return undefined;
1195
+ }
1196
+ return {
1197
+ command,
1198
+ shell
1199
+ };
1200
+ };
1201
+ const executeRunInTerminalTool = async (args, _options) => {
1202
+ const runInTerminalOptions = getRunInTerminalOptions(args);
1203
+ if (!runInTerminalOptions) {
1204
+ return {
1205
+ error: 'Invalid argument: options must include shell (string) and command (string).'
1206
+ };
1207
+ }
1208
+ return {
1209
+ output: {
1210
+ exitCode: 0,
1211
+ stderr: '',
1212
+ stdout: `Mock output for "${runInTerminalOptions.command}" using shell "${runInTerminalOptions.shell}"`
1213
+ }
1214
+ };
1215
+ };
1216
+
1217
+ const getSearchOptions = args => {
1218
+ const {
1219
+ options
1220
+ } = args;
1221
+ if (!options || typeof options !== 'object') {
1222
+ return undefined;
1223
+ }
1224
+ const candidate = options;
1225
+ const {
1226
+ exclude,
1227
+ isRegex,
1228
+ machWholeWord,
1229
+ matchCase,
1230
+ value
1231
+ } = candidate;
1232
+ if (typeof value !== 'string' || typeof isRegex !== 'boolean' || typeof matchCase !== 'boolean' || typeof machWholeWord !== 'boolean') {
1233
+ return undefined;
1234
+ }
1235
+ if (!Array.isArray(exclude) || exclude.some(item => typeof item !== 'string')) {
1236
+ return undefined;
1237
+ }
1238
+ return {
1239
+ exclude,
1240
+ isRegex,
1241
+ machWholeWord,
1242
+ matchCase,
1243
+ value
1244
+ };
1245
+ };
1246
+ const executeSearchTextTool = async (args, _options) => {
1247
+ const searchOptions = getSearchOptions(args);
1248
+ if (!searchOptions) {
1249
+ return {
1250
+ error: 'Invalid argument: options must include value (string), isRegex (boolean), matchCase (boolean), machWholeWord (boolean), and exclude (string[]).'
1251
+ };
1252
+ }
1253
+ const results = [{
1254
+ column: 12,
1255
+ line: 5,
1256
+ text: `Mock match for "${searchOptions.value}" in src/main.ts`,
1257
+ uri: 'file:///workspace/src/main.ts'
1258
+ }, {
1259
+ column: 3,
1260
+ line: 18,
1261
+ text: `Mock match for "${searchOptions.value}" in src/utils/search.ts`,
1262
+ uri: 'file:///workspace/src/utils/search.ts'
1263
+ }];
1264
+ return {
1265
+ options: searchOptions,
1266
+ results
1267
+ };
1268
+ };
1269
+
1126
1270
  const executeWriteFileTool = async (args, _options) => {
1127
1271
  const uri = typeof args.uri === 'string' ? args.uri : '';
1128
1272
  const content = typeof args.content === 'string' ? args.content : '';
@@ -1175,6 +1319,9 @@ const executeChatTool = async (name, rawArguments, options) => {
1175
1319
  if (name === 'write_file') {
1176
1320
  return executeWriteFileTool(args);
1177
1321
  }
1322
+ if (name === 'edit_file') {
1323
+ return executeEditFileTool(args);
1324
+ }
1178
1325
  if (name === 'list_files') {
1179
1326
  return executeListFilesTool(args);
1180
1327
  }
@@ -1193,6 +1340,12 @@ const executeChatTool = async (name, rawArguments, options) => {
1193
1340
  if (name === 'close_preview') {
1194
1341
  return executeClosePreviewTool();
1195
1342
  }
1343
+ if (name === 'search_text') {
1344
+ return executeSearchTextTool(args);
1345
+ }
1346
+ if (name === 'run_in_terminal') {
1347
+ return executeRunInTerminalTool(args);
1348
+ }
1196
1349
  return {
1197
1350
  error: `Unknown tool: ${name}`
1198
1351
  };
@@ -1242,6 +1395,38 @@ const getWriteFileTool = () => {
1242
1395
  type: 'function'
1243
1396
  };
1244
1397
  };
1398
+ const getEditFileTool = () => {
1399
+ return {
1400
+ function: {
1401
+ description: 'Apply a targeted text edit to an existing file by character offsets. This reads the current file, applies the edit, and writes the updated result. Use this for small changes instead of rewriting the full file.',
1402
+ name: 'edit_file',
1403
+ parameters: {
1404
+ additionalProperties: false,
1405
+ properties: {
1406
+ end: {
1407
+ description: 'Zero-based end character offset (exclusive) in the current file content.',
1408
+ type: 'number'
1409
+ },
1410
+ start: {
1411
+ description: 'Zero-based start character offset (inclusive) in the current file content.',
1412
+ type: 'number'
1413
+ },
1414
+ text: {
1415
+ description: 'Replacement text inserted at the range [start, end).',
1416
+ type: 'string'
1417
+ },
1418
+ uri: {
1419
+ description: 'Absolute URI for the target file (for example `file://...`, `memfs://...`, or extension-provided file system URIs). Relative paths are not allowed.',
1420
+ type: 'string'
1421
+ }
1422
+ },
1423
+ required: ['uri', 'start', 'end', 'text'],
1424
+ type: 'object'
1425
+ }
1426
+ },
1427
+ type: 'function'
1428
+ };
1429
+ };
1245
1430
  const getListFilesTool = () => {
1246
1431
  return {
1247
1432
  function: {
@@ -1265,7 +1450,7 @@ const getListFilesTool = () => {
1265
1450
  const getGetWorkspaceUriTool = () => {
1266
1451
  return {
1267
1452
  function: {
1268
- description: 'Get the URI of the currently open workspace folder. Call this first before using list_files, read_file, write_file, open_preview, or openEditor when you do not already have a concrete workspace URI.',
1453
+ description: 'Get the URI of the currently open workspace folder. Call this first before using list_files, read_file, write_file, edit_file, open_preview, or openEditor when you do not already have a concrete workspace URI.',
1269
1454
  name: 'getWorkspaceUri',
1270
1455
  parameters: {
1271
1456
  additionalProperties: false,
@@ -1358,8 +1543,78 @@ const getClosePreviewTool = () => {
1358
1543
  type: 'function'
1359
1544
  };
1360
1545
  };
1546
+ const getSearchTextTool = () => {
1547
+ return {
1548
+ function: {
1549
+ description: 'Search text in workspace files using query options and return matching results.',
1550
+ name: 'search_text',
1551
+ parameters: {
1552
+ additionalProperties: false,
1553
+ properties: {
1554
+ options: {
1555
+ additionalProperties: false,
1556
+ properties: {
1557
+ exclude: {
1558
+ items: {
1559
+ type: 'string'
1560
+ },
1561
+ type: 'array'
1562
+ },
1563
+ isRegex: {
1564
+ type: 'boolean'
1565
+ },
1566
+ machWholeWord: {
1567
+ type: 'boolean'
1568
+ },
1569
+ matchCase: {
1570
+ type: 'boolean'
1571
+ },
1572
+ value: {
1573
+ type: 'string'
1574
+ }
1575
+ },
1576
+ required: ['value', 'isRegex', 'matchCase', 'machWholeWord', 'exclude'],
1577
+ type: 'object'
1578
+ }
1579
+ },
1580
+ required: ['options'],
1581
+ type: 'object'
1582
+ }
1583
+ },
1584
+ type: 'function'
1585
+ };
1586
+ };
1587
+ const getRunInTerminalTool = () => {
1588
+ return {
1589
+ function: {
1590
+ description: 'Run a shell command in a terminal using the provided shell and command options.',
1591
+ name: 'run_in_terminal',
1592
+ parameters: {
1593
+ additionalProperties: false,
1594
+ properties: {
1595
+ options: {
1596
+ additionalProperties: false,
1597
+ properties: {
1598
+ command: {
1599
+ type: 'string'
1600
+ },
1601
+ shell: {
1602
+ type: 'string'
1603
+ }
1604
+ },
1605
+ required: ['shell', 'command'],
1606
+ type: 'object'
1607
+ }
1608
+ },
1609
+ required: ['options'],
1610
+ type: 'object'
1611
+ }
1612
+ },
1613
+ type: 'function'
1614
+ };
1615
+ };
1361
1616
  const getBasicChatTools = () => {
1362
- return [getReadFileTool(), getWriteFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool()];
1617
+ return [getReadFileTool(), getWriteFileTool(), getEditFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool(), getSearchTextTool(), getRunInTerminalTool()];
1363
1618
  };
1364
1619
 
1365
1620
  const handleMessagePort = async port => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "7.5.0",
3
+ "version": "7.7.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",