@lvce-editor/chat-tool-worker 7.7.0 → 7.9.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,39 @@ const executeClosePreviewTool = async (_args, _options) => {
994
994
  }
995
995
  };
996
996
 
997
+ const ABSOLUTE_URI_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//;
998
+ const isAbsoluteUri = value => {
999
+ return ABSOLUTE_URI_REGEX.test(value);
1000
+ };
1001
+
1002
+ const executeCreateDirectoryTool = async (args, _options) => {
1003
+ const uri = typeof args.uri === 'string' ? args.uri : '';
1004
+ if (!uri || !isAbsoluteUri(uri)) {
1005
+ return {
1006
+ error: 'Invalid argument: uri must be an absolute URI.'
1007
+ };
1008
+ }
1009
+ try {
1010
+ new URL(uri);
1011
+ } catch {
1012
+ return {
1013
+ error: 'Invalid argument: invalid URL.'
1014
+ };
1015
+ }
1016
+ try {
1017
+ await invoke('FileSystem.mkdir', uri);
1018
+ return {
1019
+ ok: true,
1020
+ uri
1021
+ };
1022
+ } catch (error) {
1023
+ return {
1024
+ ...getToolErrorPayload(error),
1025
+ uri
1026
+ };
1027
+ }
1028
+ };
1029
+
997
1030
  const applyTextEdit = (content, start, end, text) => {
998
1031
  if (start > content.length || end > content.length) {
999
1032
  throw new Error('Invalid argument: edit range is out of bounds.');
@@ -1001,11 +1034,6 @@ const applyTextEdit = (content, start, end, text) => {
1001
1034
  return `${content.slice(0, start)}${text}${content.slice(end)}`;
1002
1035
  };
1003
1036
 
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
1037
  const executeEditFileTool = async (args, _options) => {
1010
1038
  const uri = typeof args.uri === 'string' ? args.uri : '';
1011
1039
  const start = typeof args.start === 'number' ? args.start : -1;
@@ -1155,6 +1183,45 @@ const executeReadFileTool = async (args, _options) => {
1155
1183
  }
1156
1184
  };
1157
1185
 
1186
+ /* eslint-disable @cspell/spellchecker */
1187
+ /* eslint-disable e18e/prefer-url-canparse */
1188
+ const executeRenameTool = async (args, _options) => {
1189
+ const oldUri = typeof args.oldUri === 'string' ? args.oldUri : '';
1190
+ const newUri = typeof args.newUri === 'string' ? args.newUri : '';
1191
+ if (!oldUri || !isAbsoluteUri(oldUri)) {
1192
+ return {
1193
+ error: 'Invalid argument: oldUri must be an absolute URI.'
1194
+ };
1195
+ }
1196
+ if (!newUri || !isAbsoluteUri(newUri)) {
1197
+ return {
1198
+ error: 'Invalid argument: newUri must be an absolute URI.'
1199
+ };
1200
+ }
1201
+ try {
1202
+ new URL(oldUri);
1203
+ new URL(newUri);
1204
+ } catch {
1205
+ return {
1206
+ error: 'Invalid argument: invalid URL.'
1207
+ };
1208
+ }
1209
+ try {
1210
+ await invoke('FileSystem.rename', oldUri, newUri);
1211
+ return {
1212
+ newUri,
1213
+ ok: true,
1214
+ oldUri
1215
+ };
1216
+ } catch (error) {
1217
+ return {
1218
+ ...getToolErrorPayload(error),
1219
+ newUri,
1220
+ oldUri
1221
+ };
1222
+ }
1223
+ };
1224
+
1158
1225
  const maxPayloadLength = 40_000;
1159
1226
  const executeRenderHtmlTool = async (args, _options) => {
1160
1227
  const html = typeof args.html === 'string' ? args.html : '';
@@ -1178,6 +1245,51 @@ const executeRenderHtmlTool = async (args, _options) => {
1178
1245
  };
1179
1246
  };
1180
1247
 
1248
+ const getRgArgs = args => {
1249
+ const {
1250
+ '-n': lineNumbers,
1251
+ output_mode,
1252
+ path,
1253
+ pattern
1254
+ } = args;
1255
+ if (typeof pattern !== 'string') {
1256
+ return undefined;
1257
+ }
1258
+ if (path !== undefined && typeof path !== 'string') {
1259
+ return undefined;
1260
+ }
1261
+ if (output_mode !== undefined && output_mode !== 'content' && output_mode !== 'files_with_matches' && output_mode !== 'count') {
1262
+ return undefined;
1263
+ }
1264
+ if (lineNumbers !== undefined && typeof lineNumbers !== 'boolean') {
1265
+ return undefined;
1266
+ }
1267
+ return {
1268
+ ...(lineNumbers === undefined ? {} : {
1269
+ '-n': lineNumbers
1270
+ }),
1271
+ ...(output_mode === undefined ? {} : {
1272
+ output_mode
1273
+ }),
1274
+ ...(path === undefined ? {} : {
1275
+ path
1276
+ }),
1277
+ pattern
1278
+ };
1279
+ };
1280
+ const executeRgTool = async (args, _options) => {
1281
+ const rgArgs = getRgArgs(args);
1282
+ if (!rgArgs) {
1283
+ return {
1284
+ error: 'Invalid argument: rg requires pattern (string), optional path (string), optional output_mode ("content" | "files_with_matches" | "count"), and optional -n (boolean).'
1285
+ };
1286
+ }
1287
+ return {
1288
+ arguments: rgArgs,
1289
+ result: 'No matches found.'
1290
+ };
1291
+ };
1292
+
1181
1293
  const getRunInTerminalOptions = args => {
1182
1294
  const {
1183
1295
  options
@@ -1267,6 +1379,96 @@ const executeSearchTextTool = async (args, _options) => {
1267
1379
  };
1268
1380
  };
1269
1381
 
1382
+ const todoDbName = 'chat-tool-worker';
1383
+ const todoStoreName = 'state';
1384
+ const todoKey = 'todoList';
1385
+ const todoCacheName = 'chat-tool-worker-todo';
1386
+ const todoCacheRequest = new Request('https://chat-tool-worker.local/todo-list');
1387
+ let memoryTodoList = '';
1388
+ const setTodosInIndexedDb = async todos => {
1389
+ const db = await new Promise((resolve, reject) => {
1390
+ const openRequest = indexedDB.open(todoDbName, 1);
1391
+ openRequest.onupgradeneeded = () => {
1392
+ const nextDb = openRequest.result;
1393
+ if (!nextDb.objectStoreNames.contains(todoStoreName)) {
1394
+ nextDb.createObjectStore(todoStoreName);
1395
+ }
1396
+ };
1397
+ openRequest.onsuccess = () => {
1398
+ resolve(openRequest.result);
1399
+ };
1400
+ openRequest.onerror = () => {
1401
+ reject(openRequest.error ?? new Error('Failed to open IndexedDB'));
1402
+ };
1403
+ });
1404
+ await new Promise((resolve, reject) => {
1405
+ const transaction = db.transaction(todoStoreName, 'readwrite');
1406
+ transaction.objectStore(todoStoreName).put(todos, todoKey);
1407
+ transaction.oncomplete = () => {
1408
+ db.close();
1409
+ resolve();
1410
+ };
1411
+ transaction.onerror = () => {
1412
+ db.close();
1413
+ reject(transaction.error ?? new Error('Failed to persist todo list'));
1414
+ };
1415
+ transaction.onabort = () => {
1416
+ db.close();
1417
+ reject(transaction.error ?? new Error('Failed to persist todo list'));
1418
+ };
1419
+ });
1420
+ };
1421
+ const setTodosInCacheStorage = async todos => {
1422
+ const cache = await caches.open(todoCacheName);
1423
+ const payload = JSON.stringify({
1424
+ todos
1425
+ });
1426
+ await cache.put(todoCacheRequest, new Response(payload, {
1427
+ headers: {
1428
+ 'content-type': 'application/json'
1429
+ }
1430
+ }));
1431
+ };
1432
+ const executeUpdateTodoTool = async (args, _options) => {
1433
+ const todos = typeof args.todos === 'string' ? args.todos : '';
1434
+ if (!todos) {
1435
+ return {
1436
+ error: 'Missing required argument: todos'
1437
+ };
1438
+ }
1439
+ try {
1440
+ if (typeof indexedDB !== 'undefined') {
1441
+ await setTodosInIndexedDb(todos);
1442
+ return {
1443
+ message: 'TODO list updated',
1444
+ ok: true,
1445
+ storage: 'indexeddb',
1446
+ todos
1447
+ };
1448
+ }
1449
+ if (typeof caches !== 'undefined') {
1450
+ await setTodosInCacheStorage(todos);
1451
+ return {
1452
+ message: 'TODO list updated',
1453
+ ok: true,
1454
+ storage: 'cache',
1455
+ todos
1456
+ };
1457
+ }
1458
+ const previousTodos = memoryTodoList;
1459
+ memoryTodoList = todos;
1460
+ return {
1461
+ message: 'TODO list updated',
1462
+ ok: true,
1463
+ previousTodos,
1464
+ storage: 'memory',
1465
+ todos
1466
+ };
1467
+ } catch (error) {
1468
+ return getToolErrorPayload(error);
1469
+ }
1470
+ };
1471
+
1270
1472
  const executeWriteFileTool = async (args, _options) => {
1271
1473
  const uri = typeof args.uri === 'string' ? args.uri : '';
1272
1474
  const content = typeof args.content === 'string' ? args.content : '';
@@ -1319,6 +1521,9 @@ const executeChatTool = async (name, rawArguments, options) => {
1319
1521
  if (name === 'write_file') {
1320
1522
  return executeWriteFileTool(args);
1321
1523
  }
1524
+ if (name === 'rename') {
1525
+ return executeRenameTool(args);
1526
+ }
1322
1527
  if (name === 'edit_file') {
1323
1528
  return executeEditFileTool(args);
1324
1529
  }
@@ -1343,9 +1548,18 @@ const executeChatTool = async (name, rawArguments, options) => {
1343
1548
  if (name === 'search_text') {
1344
1549
  return executeSearchTextTool(args);
1345
1550
  }
1551
+ if (name === 'rg') {
1552
+ return executeRgTool(args);
1553
+ }
1346
1554
  if (name === 'run_in_terminal') {
1347
1555
  return executeRunInTerminalTool(args);
1348
1556
  }
1557
+ if (name === 'create_directory') {
1558
+ return executeCreateDirectoryTool(args);
1559
+ }
1560
+ if (name === 'update_todo') {
1561
+ return executeUpdateTodoTool(args);
1562
+ }
1349
1563
  return {
1350
1564
  error: `Unknown tool: ${name}`
1351
1565
  };
@@ -1395,6 +1609,30 @@ const getWriteFileTool = () => {
1395
1609
  type: 'function'
1396
1610
  };
1397
1611
  };
1612
+ const getRenameTool = () => {
1613
+ return {
1614
+ function: {
1615
+ description: 'Rename or move a file or folder from one absolute URI to another absolute URI (for example `file://...`, `memfs://...`, or extension-provided file system URIs).',
1616
+ name: 'rename',
1617
+ parameters: {
1618
+ additionalProperties: false,
1619
+ properties: {
1620
+ newUri: {
1621
+ description: 'Absolute target URI for the file or folder after renaming.',
1622
+ type: 'string'
1623
+ },
1624
+ oldUri: {
1625
+ description: 'Absolute source URI for the existing file or folder to rename.',
1626
+ type: 'string'
1627
+ }
1628
+ },
1629
+ required: ['oldUri', 'newUri'],
1630
+ type: 'object'
1631
+ }
1632
+ },
1633
+ type: 'function'
1634
+ };
1635
+ };
1398
1636
  const getEditFileTool = () => {
1399
1637
  return {
1400
1638
  function: {
@@ -1584,6 +1822,39 @@ const getSearchTextTool = () => {
1584
1822
  type: 'function'
1585
1823
  };
1586
1824
  };
1825
+ const getRgTool = () => {
1826
+ return {
1827
+ function: {
1828
+ description: 'Search text or regex in workspace files using ripgrep-style arguments and return matching results.',
1829
+ name: 'rg',
1830
+ parameters: {
1831
+ additionalProperties: false,
1832
+ properties: {
1833
+ '-n': {
1834
+ description: 'When true, include line numbers in the output.',
1835
+ type: 'boolean'
1836
+ },
1837
+ output_mode: {
1838
+ description: 'Result format to return.',
1839
+ enum: ['content', 'files_with_matches', 'count'],
1840
+ type: 'string'
1841
+ },
1842
+ path: {
1843
+ description: 'Optional file or directory path inside the current workspace to scope the search.',
1844
+ type: 'string'
1845
+ },
1846
+ pattern: {
1847
+ description: 'Text or regular expression pattern to search for.',
1848
+ type: 'string'
1849
+ }
1850
+ },
1851
+ required: ['pattern'],
1852
+ type: 'object'
1853
+ }
1854
+ },
1855
+ type: 'function'
1856
+ };
1857
+ };
1587
1858
  const getRunInTerminalTool = () => {
1588
1859
  return {
1589
1860
  function: {
@@ -1613,8 +1884,48 @@ const getRunInTerminalTool = () => {
1613
1884
  type: 'function'
1614
1885
  };
1615
1886
  };
1887
+ const getCreateDirectoryTool = () => {
1888
+ return {
1889
+ function: {
1890
+ description: 'Create a new directory at the given absolute URI (for example `file://...`, `memfs://...`, or extension-provided file system URIs). Do not pass a relative path.',
1891
+ name: 'create_directory',
1892
+ parameters: {
1893
+ additionalProperties: false,
1894
+ properties: {
1895
+ uri: {
1896
+ description: 'Absolute URI for the target directory (for example `file://...`, `memfs://...`, or extension-provided file system URIs). Relative paths are not allowed.',
1897
+ type: 'string'
1898
+ }
1899
+ },
1900
+ required: ['uri'],
1901
+ type: 'object'
1902
+ }
1903
+ },
1904
+ type: 'function'
1905
+ };
1906
+ };
1907
+ const getUpdateTodoTool = () => {
1908
+ return {
1909
+ function: {
1910
+ description: 'Update the active TODO checklist for the current chat session. Pass the full markdown checklist text with one item per line (for example "- [ ] Step one").',
1911
+ name: 'update_todo',
1912
+ parameters: {
1913
+ additionalProperties: false,
1914
+ properties: {
1915
+ todos: {
1916
+ description: 'Complete markdown checklist content to store as the current TODO list state.',
1917
+ type: 'string'
1918
+ }
1919
+ },
1920
+ required: ['todos'],
1921
+ type: 'object'
1922
+ }
1923
+ },
1924
+ type: 'function'
1925
+ };
1926
+ };
1616
1927
  const getBasicChatTools = () => {
1617
- return [getReadFileTool(), getWriteFileTool(), getEditFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool(), getSearchTextTool(), getRunInTerminalTool()];
1928
+ return [getReadFileTool(), getWriteFileTool(), getRenameTool(), getEditFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool(), getSearchTextTool(), getRgTool(), getRunInTerminalTool(), getCreateDirectoryTool(), getUpdateTodoTool()];
1618
1929
  };
1619
1930
 
1620
1931
  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.7.0",
3
+ "version": "7.9.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",