@lvce-editor/chat-tool-worker 6.0.0 → 7.1.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.
@@ -981,14 +981,25 @@ const getToolErrorPayload = error => {
981
981
  };
982
982
  };
983
983
 
984
+ const executeClosePreviewTool = async (_args, _options) => {
985
+ try {
986
+ await invoke('Preview.close');
987
+ return {
988
+ ok: true
989
+ };
990
+ } catch (error) {
991
+ return getToolErrorPayload(error);
992
+ }
993
+ };
994
+
984
995
  const executeGetWorkspaceUriTool = async (_args, _options) => {
985
996
  try {
986
997
  const workspaceUri = await getWorkspacePath();
987
- return JSON.stringify({
998
+ return {
988
999
  workspaceUri
989
- });
1000
+ };
990
1001
  } catch (error) {
991
- return JSON.stringify(getToolErrorPayload(error));
1002
+ return getToolErrorPayload(error);
992
1003
  }
993
1004
  };
994
1005
 
@@ -1000,21 +1011,63 @@ const isAbsoluteUri = value => {
1000
1011
  const executeListFilesTool = async (args, _options) => {
1001
1012
  const uri = typeof args.uri === 'string' ? args.uri : '';
1002
1013
  if (!uri || !isAbsoluteUri(uri)) {
1003
- return JSON.stringify({
1014
+ return {
1004
1015
  error: 'Invalid argument: uri must be an absolute URI.'
1005
- });
1016
+ };
1006
1017
  }
1007
1018
  try {
1008
1019
  const entries = await invoke('FileSystem.readDirWithFileTypes', uri);
1009
- return JSON.stringify({
1020
+ return {
1010
1021
  entries,
1011
1022
  uri
1012
- });
1023
+ };
1013
1024
  } catch (error) {
1014
- return JSON.stringify({
1025
+ return {
1015
1026
  ...getToolErrorPayload(error),
1016
1027
  uri
1017
- });
1028
+ };
1029
+ }
1030
+ };
1031
+
1032
+ const executeOpenEditorTool = async (args, _options) => {
1033
+ const uri = typeof args.uri === 'string' ? args.uri : '';
1034
+ if (!uri || !isAbsoluteUri(uri)) {
1035
+ return {
1036
+ error: 'Invalid argument: uri must be an absolute URI.'
1037
+ };
1038
+ }
1039
+ try {
1040
+ await invoke('Main.openUri', uri);
1041
+ return {
1042
+ ok: true,
1043
+ uri
1044
+ };
1045
+ } catch (error) {
1046
+ return {
1047
+ ...getToolErrorPayload(error),
1048
+ uri
1049
+ };
1050
+ }
1051
+ };
1052
+
1053
+ const executeOpenPreviewTool = async (args, _options) => {
1054
+ const uri = typeof args.uri === 'string' ? args.uri : '';
1055
+ if (!uri || !isAbsoluteUri(uri)) {
1056
+ return {
1057
+ error: 'Invalid argument: uri must be an absolute URI.'
1058
+ };
1059
+ }
1060
+ try {
1061
+ await invoke('Preview.open', uri);
1062
+ return {
1063
+ ok: true,
1064
+ uri
1065
+ };
1066
+ } catch (error) {
1067
+ return {
1068
+ ...getToolErrorPayload(error),
1069
+ uri
1070
+ };
1018
1071
  }
1019
1072
  };
1020
1073
 
@@ -1050,41 +1103,41 @@ const executeReadFileTool = async (args, _options) => {
1050
1103
  const uri = typeof args.uri === 'string' ? args.uri : '';
1051
1104
  if (uri) {
1052
1105
  if (!isAbsoluteUri(uri)) {
1053
- return JSON.stringify({
1106
+ return {
1054
1107
  error: 'Invalid argument: uri must be an absolute URI.'
1055
- });
1108
+ };
1056
1109
  }
1057
1110
  try {
1058
1111
  const content = await readFile(uri);
1059
- return JSON.stringify({
1112
+ return {
1060
1113
  content,
1061
1114
  uri
1062
- });
1115
+ };
1063
1116
  } catch (error) {
1064
- return JSON.stringify({
1117
+ return {
1065
1118
  ...getToolErrorPayload(error),
1066
1119
  uri
1067
- });
1120
+ };
1068
1121
  }
1069
1122
  }
1070
1123
  const filePath = typeof args.path === 'string' ? args.path : '';
1071
1124
  if (!filePath || isPathTraversalAttempt(filePath)) {
1072
- return JSON.stringify({
1125
+ return {
1073
1126
  error: 'Access denied: path must be relative and stay within the open workspace folder.'
1074
- });
1127
+ };
1075
1128
  }
1076
1129
  const normalizedPath = normalizeRelativePath(filePath);
1077
1130
  try {
1078
1131
  const content = await readFile(normalizedPath);
1079
- return JSON.stringify({
1132
+ return {
1080
1133
  content,
1081
1134
  path: normalizedPath
1082
- });
1135
+ };
1083
1136
  } catch (error) {
1084
- return JSON.stringify({
1137
+ return {
1085
1138
  ...getToolErrorPayload(error),
1086
1139
  path: normalizedPath
1087
- });
1140
+ };
1088
1141
  }
1089
1142
  };
1090
1143
 
@@ -1094,43 +1147,49 @@ const executeRenderHtmlTool = async (args, _options) => {
1094
1147
  const css = typeof args.css === 'string' ? args.css : '';
1095
1148
  const title = typeof args.title === 'string' ? args.title : '';
1096
1149
  if (!html) {
1097
- return JSON.stringify({
1150
+ return {
1098
1151
  error: 'Missing required argument: html'
1099
- });
1152
+ };
1100
1153
  }
1101
1154
  if (html.length > maxPayloadLength || css.length > maxPayloadLength) {
1102
- return JSON.stringify({
1155
+ return {
1103
1156
  error: 'Payload too large: keep html/css under 40,000 characters each.'
1104
- });
1157
+ };
1105
1158
  }
1106
- return JSON.stringify({
1159
+ return {
1107
1160
  css,
1108
1161
  html,
1109
1162
  ok: true,
1110
1163
  title
1111
- });
1164
+ };
1112
1165
  };
1113
1166
 
1114
1167
  const executeWriteFileTool = async (args, _options) => {
1115
- const filePath = typeof args.path === 'string' ? args.path : '';
1168
+ const uri = typeof args.uri === 'string' ? args.uri : '';
1116
1169
  const content = typeof args.content === 'string' ? args.content : '';
1117
- if (!filePath || isPathTraversalAttempt(filePath)) {
1118
- return JSON.stringify({
1119
- error: 'Access denied: path must be relative and stay within the open workspace folder.'
1120
- });
1170
+ if (!uri || !isAbsoluteUri(uri)) {
1171
+ return {
1172
+ error: 'Invalid argument: uri must be an absolute URI.'
1173
+ };
1174
+ }
1175
+ try {
1176
+ new URL(uri);
1177
+ } catch {
1178
+ return {
1179
+ error: 'Invalid argument: invalid URL.'
1180
+ };
1121
1181
  }
1122
- const normalizedPath = normalizeRelativePath(filePath);
1123
1182
  try {
1124
- await writeFile(normalizedPath, content);
1125
- return JSON.stringify({
1183
+ await writeFile(uri, content);
1184
+ return {
1126
1185
  ok: true,
1127
- path: normalizedPath
1128
- });
1186
+ uri
1187
+ };
1129
1188
  } catch (error) {
1130
- return JSON.stringify({
1189
+ return {
1131
1190
  ...getToolErrorPayload(error),
1132
- path: normalizedPath
1133
- });
1191
+ uri
1192
+ };
1134
1193
  }
1135
1194
  };
1136
1195
 
@@ -1166,9 +1225,18 @@ const executeChatTool = async (name, rawArguments, options) => {
1166
1225
  if (name === 'render_html') {
1167
1226
  return executeRenderHtmlTool(args);
1168
1227
  }
1169
- return JSON.stringify({
1228
+ if (name === 'open_preview') {
1229
+ return executeOpenPreviewTool(args);
1230
+ }
1231
+ if (name === 'openEditor') {
1232
+ return executeOpenEditorTool(args);
1233
+ }
1234
+ if (name === 'close_preview') {
1235
+ return executeClosePreviewTool();
1236
+ }
1237
+ return {
1170
1238
  error: `Unknown tool: ${name}`
1171
- });
1239
+ };
1172
1240
  };
1173
1241
 
1174
1242
  const getReadFileTool = () => {
@@ -1194,7 +1262,7 @@ const getReadFileTool = () => {
1194
1262
  const getWriteFileTool = () => {
1195
1263
  return {
1196
1264
  function: {
1197
- description: 'Write UTF-8 text content to a file inside the currently open workspace folder.',
1265
+ description: 'Write UTF-8 text content to a file inside the currently open workspace folder. Only pass an absolute URI.',
1198
1266
  name: 'write_file',
1199
1267
  parameters: {
1200
1268
  additionalProperties: false,
@@ -1203,12 +1271,12 @@ const getWriteFileTool = () => {
1203
1271
  description: 'New UTF-8 text content to write to the file.',
1204
1272
  type: 'string'
1205
1273
  },
1206
- path: {
1207
- description: 'Relative file path within the workspace (for example: src/index.ts).',
1274
+ uri: {
1275
+ description: 'Absolute file URI within the workspace (for example: file:///workspace/src/index.ts).',
1208
1276
  type: 'string'
1209
1277
  }
1210
1278
  },
1211
- required: ['path', 'content'],
1279
+ required: ['uri', 'content'],
1212
1280
  type: 'object'
1213
1281
  }
1214
1282
  },
@@ -1277,8 +1345,62 @@ const getRenderHtmlTool = () => {
1277
1345
  type: 'function'
1278
1346
  };
1279
1347
  };
1348
+ const getOpenPreviewTool = () => {
1349
+ return {
1350
+ function: {
1351
+ description: 'Open the right-side preview for an HTML file URI inside the currently open workspace folder.',
1352
+ name: 'open_preview',
1353
+ parameters: {
1354
+ additionalProperties: false,
1355
+ properties: {
1356
+ uri: {
1357
+ description: 'Absolute HTML file URI within the workspace (for example: file:///workspace/index.html).',
1358
+ type: 'string'
1359
+ }
1360
+ },
1361
+ required: ['uri'],
1362
+ type: 'object'
1363
+ }
1364
+ },
1365
+ type: 'function'
1366
+ };
1367
+ };
1368
+ const getOpenEditorTool = () => {
1369
+ return {
1370
+ function: {
1371
+ description: 'Open a file URI in the editor.',
1372
+ name: 'openEditor',
1373
+ parameters: {
1374
+ additionalProperties: false,
1375
+ properties: {
1376
+ uri: {
1377
+ description: 'Absolute file URI to open in the editor (for example: file:///workspace/src/index.ts).',
1378
+ type: 'string'
1379
+ }
1380
+ },
1381
+ required: ['uri'],
1382
+ type: 'object'
1383
+ }
1384
+ },
1385
+ type: 'function'
1386
+ };
1387
+ };
1388
+ const getClosePreviewTool = () => {
1389
+ return {
1390
+ function: {
1391
+ description: 'Close the right-side preview if it is currently open.',
1392
+ name: 'close_preview',
1393
+ parameters: {
1394
+ additionalProperties: false,
1395
+ properties: {},
1396
+ type: 'object'
1397
+ }
1398
+ },
1399
+ type: 'function'
1400
+ };
1401
+ };
1280
1402
  const getBasicChatTools = () => {
1281
- return [getReadFileTool(), getWriteFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool()];
1403
+ return [getReadFileTool(), getWriteFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getOpenEditorTool(), getClosePreviewTool()];
1282
1404
  };
1283
1405
 
1284
1406
  const handleMessagePort = async port => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "6.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",