@lvce-editor/chat-tool-worker 7.6.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,42 @@ 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
+
1126
1217
  const getSearchOptions = args => {
1127
1218
  const {
1128
1219
  options
@@ -1228,6 +1319,9 @@ const executeChatTool = async (name, rawArguments, options) => {
1228
1319
  if (name === 'write_file') {
1229
1320
  return executeWriteFileTool(args);
1230
1321
  }
1322
+ if (name === 'edit_file') {
1323
+ return executeEditFileTool(args);
1324
+ }
1231
1325
  if (name === 'list_files') {
1232
1326
  return executeListFilesTool(args);
1233
1327
  }
@@ -1249,6 +1343,9 @@ const executeChatTool = async (name, rawArguments, options) => {
1249
1343
  if (name === 'search_text') {
1250
1344
  return executeSearchTextTool(args);
1251
1345
  }
1346
+ if (name === 'run_in_terminal') {
1347
+ return executeRunInTerminalTool(args);
1348
+ }
1252
1349
  return {
1253
1350
  error: `Unknown tool: ${name}`
1254
1351
  };
@@ -1298,6 +1395,38 @@ const getWriteFileTool = () => {
1298
1395
  type: 'function'
1299
1396
  };
1300
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
+ };
1301
1430
  const getListFilesTool = () => {
1302
1431
  return {
1303
1432
  function: {
@@ -1321,7 +1450,7 @@ const getListFilesTool = () => {
1321
1450
  const getGetWorkspaceUriTool = () => {
1322
1451
  return {
1323
1452
  function: {
1324
- 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.',
1325
1454
  name: 'getWorkspaceUri',
1326
1455
  parameters: {
1327
1456
  additionalProperties: false,
@@ -1455,8 +1584,37 @@ const getSearchTextTool = () => {
1455
1584
  type: 'function'
1456
1585
  };
1457
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
+ };
1458
1616
  const getBasicChatTools = () => {
1459
- return [getReadFileTool(), getWriteFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool(), getSearchTextTool()];
1617
+ return [getReadFileTool(), getWriteFileTool(), getEditFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool(), getSearchTextTool(), getRunInTerminalTool()];
1460
1618
  };
1461
1619
 
1462
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.6.0",
3
+ "version": "7.7.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",