@lvce-editor/chat-view 1.2.0 → 1.4.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.
@@ -983,6 +983,7 @@ const TargetValue = 'event.target.value';
983
983
  const ExtensionHostWorker = 44;
984
984
  const RendererWorker = 1;
985
985
 
986
+ const SetCss = 'Viewlet.setCss';
986
987
  const SetDom2 = 'Viewlet.setDom2';
987
988
  const SetPatches = 'Viewlet.setPatches';
988
989
 
@@ -1059,6 +1060,7 @@ const {
1059
1060
  } = create$2(ExtensionHostWorker);
1060
1061
 
1061
1062
  const {
1063
+ invoke,
1062
1064
  invokeAndTransfer,
1063
1065
  set: set$1
1064
1066
  } = create$2(RendererWorker);
@@ -1230,15 +1232,20 @@ const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1230
1232
  set(uid, state, state);
1231
1233
  };
1232
1234
 
1235
+ const isEqual$1 = (oldState, newState) => {
1236
+ return oldState.initial === newState.initial;
1237
+ };
1238
+
1233
1239
  const isEqual = (oldState, newState) => {
1234
1240
  return oldState.composerValue === newState.composerValue && oldState.ignoreNextInput === newState.ignoreNextInput && oldState.initial === newState.initial && oldState.renamingSessionId === newState.renamingSessionId && oldState.selectedSessionId === newState.selectedSessionId && oldState.sessions === newState.sessions;
1235
1241
  };
1236
1242
 
1237
1243
  const RenderItems = 4;
1244
+ const RenderCss = 10;
1238
1245
  const RenderIncremental = 11;
1239
1246
 
1240
- const modules = [isEqual];
1241
- const numbers = [RenderIncremental];
1247
+ const modules = [isEqual, isEqual$1];
1248
+ const numbers = [RenderIncremental, RenderCss];
1242
1249
 
1243
1250
  const diff = (oldState, newState) => {
1244
1251
  const diffResult = [];
@@ -1264,10 +1271,91 @@ const getKeyBindings = () => {
1264
1271
  return [];
1265
1272
  };
1266
1273
 
1274
+ const submitRename = state => {
1275
+ const {
1276
+ composerValue,
1277
+ renamingSessionId,
1278
+ sessions
1279
+ } = state;
1280
+ const title = composerValue.trim();
1281
+ if (!renamingSessionId || !title) {
1282
+ return {
1283
+ ...state,
1284
+ renamingSessionId: ''
1285
+ };
1286
+ }
1287
+ const updatedSessions = sessions.map(session => {
1288
+ if (session.id !== renamingSessionId) {
1289
+ return session;
1290
+ }
1291
+ return {
1292
+ ...session,
1293
+ title
1294
+ };
1295
+ });
1296
+ return {
1297
+ ...state,
1298
+ composerValue: '',
1299
+ ignoreNextInput: true,
1300
+ renamingSessionId: '',
1301
+ sessions: updatedSessions
1302
+ };
1303
+ };
1304
+ const submitMessage = state => {
1305
+ const {
1306
+ composerValue,
1307
+ nextMessageId,
1308
+ selectedSessionId,
1309
+ sessions
1310
+ } = state;
1311
+ const text = composerValue.trim();
1312
+ if (!text) {
1313
+ return {
1314
+ ...state,
1315
+ ignoreNextInput: true
1316
+ };
1317
+ }
1318
+ const updatedSessions = sessions.map(session => {
1319
+ if (session.id !== selectedSessionId) {
1320
+ return session;
1321
+ }
1322
+ const message = {
1323
+ id: `message-${nextMessageId}`,
1324
+ role: 'user',
1325
+ text
1326
+ };
1327
+ return {
1328
+ ...session,
1329
+ messages: [...session.messages, message]
1330
+ };
1331
+ });
1332
+ return {
1333
+ ...state,
1334
+ composerValue: '',
1335
+ ignoreNextInput: true,
1336
+ lastSubmittedSessionId: selectedSessionId,
1337
+ nextMessageId: nextMessageId + 1,
1338
+ sessions: updatedSessions
1339
+ };
1340
+ };
1341
+ const handleKeyDown = async (state, key, shiftKey) => {
1342
+ if (key !== 'Enter' || shiftKey) {
1343
+ return state;
1344
+ }
1345
+ if (state.renamingSessionId) {
1346
+ return submitRename(state);
1347
+ }
1348
+ return submitMessage(state);
1349
+ };
1350
+
1267
1351
  const CREATE_SESSION = 'create-session';
1268
1352
  const SESSION_PREFIX = 'session:';
1269
1353
  const RENAME_PREFIX = 'session-rename:';
1270
1354
  const DELETE_PREFIX = 'session-delete:';
1355
+ const SEND = 'send';
1356
+ const handleClickSend = async state => {
1357
+ return handleKeyDown(state, 'Enter', false);
1358
+ };
1271
1359
  const getNextSelectedSessionId = (sessions, deletedId) => {
1272
1360
  if (sessions.length === 0) {
1273
1361
  return '';
@@ -1363,9 +1451,19 @@ const handleClick = async (state, name) => {
1363
1451
  const id = name.slice(DELETE_PREFIX.length);
1364
1452
  return deleteSession(state, id);
1365
1453
  }
1454
+ if (name === SEND) {
1455
+ return handleClickSend(state);
1456
+ }
1366
1457
  return state;
1367
1458
  };
1368
1459
 
1460
+ const handleClickClose = async () => {
1461
+ // @ts-ignore
1462
+ await invoke('Chat.terminate');
1463
+ };
1464
+
1465
+ const handleClickSettings = async () => {};
1466
+
1369
1467
  const handleInput = async (state, value) => {
1370
1468
  if (state.ignoreNextInput) {
1371
1469
  return {
@@ -1379,83 +1477,6 @@ const handleInput = async (state, value) => {
1379
1477
  };
1380
1478
  };
1381
1479
 
1382
- const submitRename = state => {
1383
- const {
1384
- composerValue,
1385
- renamingSessionId,
1386
- sessions
1387
- } = state;
1388
- const title = composerValue.trim();
1389
- if (!renamingSessionId || !title) {
1390
- return {
1391
- ...state,
1392
- renamingSessionId: ''
1393
- };
1394
- }
1395
- const updatedSessions = sessions.map(session => {
1396
- if (session.id !== renamingSessionId) {
1397
- return session;
1398
- }
1399
- return {
1400
- ...session,
1401
- title
1402
- };
1403
- });
1404
- return {
1405
- ...state,
1406
- composerValue: '',
1407
- ignoreNextInput: true,
1408
- renamingSessionId: '',
1409
- sessions: updatedSessions
1410
- };
1411
- };
1412
- const submitMessage = state => {
1413
- const {
1414
- composerValue,
1415
- nextMessageId,
1416
- selectedSessionId,
1417
- sessions
1418
- } = state;
1419
- const text = composerValue.trim();
1420
- if (!text) {
1421
- return {
1422
- ...state,
1423
- ignoreNextInput: true
1424
- };
1425
- }
1426
- const updatedSessions = sessions.map(session => {
1427
- if (session.id !== selectedSessionId) {
1428
- return session;
1429
- }
1430
- const message = {
1431
- id: `message-${nextMessageId}`,
1432
- role: 'user',
1433
- text
1434
- };
1435
- return {
1436
- ...session,
1437
- messages: [...session.messages, message]
1438
- };
1439
- });
1440
- return {
1441
- ...state,
1442
- composerValue: '',
1443
- ignoreNextInput: true,
1444
- lastSubmittedSessionId: selectedSessionId,
1445
- nextMessageId: nextMessageId + 1,
1446
- sessions: updatedSessions
1447
- };
1448
- };
1449
- const handleKeyDown = async (state, key, shiftKey) => {
1450
- if (key !== 'Enter' || shiftKey) {
1451
- return state;
1452
- }
1453
- if (state.renamingSessionId) {
1454
- return submitRename(state);
1455
- }
1456
- return submitMessage(state);
1457
- };
1458
-
1459
1480
  const id = 7201;
1460
1481
  const sendMessagePortToExtensionHostWorker = async port => {
1461
1482
  await sendMessagePortToExtensionHostWorker$1(port, id);
@@ -1501,6 +1522,14 @@ const loadContent = async state => {
1501
1522
  };
1502
1523
  };
1503
1524
 
1525
+ // TODO render things like scrollbar height,scrollbar offset, textarea height,
1526
+ // list height
1527
+ const css = `
1528
+ `;
1529
+ const renderCss = (oldState, newState) => {
1530
+ return [SetCss, newState.uid, css];
1531
+ };
1532
+
1504
1533
  const text = data => {
1505
1534
  return {
1506
1535
  childCount: 0,
@@ -1797,29 +1826,53 @@ const diffTree = (oldNodes, newNodes) => {
1797
1826
  return removeTrailingNavigationPatches(patches);
1798
1827
  };
1799
1828
 
1800
- const Actions = 'Actions';
1801
- const TestActive = 'TestActive';
1802
- const SideBarLocation = 'SideBarLocation';
1829
+ const ChatActions = 'ChatActions';
1830
+ const ChatHeader = 'ChatHeader';
1803
1831
  const Button = 'Button';
1804
- const Editor = 'Editor';
1805
- const EditorContent = 'EditorContent';
1832
+ const ChatDetails = 'ChatDetails';
1833
+ const ChatDetailsContent = 'ChatDetailsContent';
1806
1834
  const IconButton = 'IconButton';
1807
1835
  const Label = 'Label';
1808
- const List = 'List';
1809
- const ListItems = 'ListItems';
1836
+ const ChatList = 'ChatList';
1810
1837
  const Markdown = 'Markdown';
1811
1838
  const Message = 'Message';
1812
1839
  const MultilineInputBox = 'MultilineInputBox';
1813
1840
  const Viewlet = 'Viewlet';
1814
- const WelcomeMessage = 'WelcomeMessage';
1841
+ const ChatWelcomeMessage = 'ChatWelcomeMessage';
1815
1842
 
1816
1843
  const HandleInput = 4;
1817
1844
  const HandleClick = 11;
1818
1845
  const HandleKeyDown = 12;
1846
+ const HandleClickClose = 13;
1847
+ const HandleClickSettings = 14;
1819
1848
 
1820
- const getSessionDom = (session, selectedSessionId) => {
1821
- const selected = session.id === selectedSessionId;
1822
- const sessionClassName = selected ? `${List} ${TestActive}` : List;
1849
+ const getMessagesDom = messages => {
1850
+ if (messages.length === 0) {
1851
+ return [{
1852
+ childCount: 1,
1853
+ className: ChatWelcomeMessage,
1854
+ type: Div
1855
+ }, text('Start a conversation by typing below.')];
1856
+ }
1857
+ return messages.flatMap(message => {
1858
+ return [{
1859
+ childCount: 2,
1860
+ className: Message,
1861
+ type: Div
1862
+ }, {
1863
+ childCount: 1,
1864
+ className: Label,
1865
+ type: Strong
1866
+ }, text(message.role === 'user' ? 'You' : 'Assistant'), {
1867
+ childCount: 1,
1868
+ className: Markdown,
1869
+ type: P
1870
+ }, text(message.text)];
1871
+ });
1872
+ };
1873
+
1874
+ const getSessionDom = (session, _selectedSessionId) => {
1875
+ const sessionClassName = ChatList;
1823
1876
  return [{
1824
1877
  childCount: 2,
1825
1878
  className: sessionClassName,
@@ -1833,7 +1886,7 @@ const getSessionDom = (session, selectedSessionId) => {
1833
1886
  type: Button$1
1834
1887
  }, text(session.title), {
1835
1888
  childCount: 2,
1836
- className: Actions,
1889
+ className: ChatActions,
1837
1890
  type: Div
1838
1891
  }, {
1839
1892
  childCount: 1,
@@ -1853,49 +1906,26 @@ const getSessionDom = (session, selectedSessionId) => {
1853
1906
  type: Button$1
1854
1907
  }, text('Delete')];
1855
1908
  };
1856
- const getMessagesDom = messages => {
1857
- if (messages.length === 0) {
1858
- return [{
1859
- childCount: 1,
1860
- className: WelcomeMessage,
1861
- type: Div
1862
- }, text('Start a conversation by typing below.')];
1863
- }
1864
- return messages.flatMap(message => {
1865
- return [{
1866
- childCount: 2,
1867
- className: Message,
1868
- type: Div
1869
- }, {
1870
- childCount: 1,
1871
- className: Label,
1872
- type: Strong
1873
- }, text(message.role === 'user' ? 'You' : 'Assistant'), {
1874
- childCount: 1,
1875
- className: Markdown,
1876
- type: P
1877
- }, text(message.text)];
1878
- });
1879
- };
1880
- const getStatusBarVirtualDom = (sessions, selectedSessionId, composerValue) => {
1909
+
1910
+ const getChatVirtualDom = (sessions, selectedSessionId, composerValue) => {
1881
1911
  const selectedSession = sessions.find(session => session.id === selectedSessionId);
1882
1912
  const messages = selectedSession ? selectedSession.messages : [];
1883
- const sessionNodes = sessions.flatMap(session => getSessionDom(session, selectedSessionId));
1913
+ const sessionNodes = sessions.flatMap(session => getSessionDom(session));
1884
1914
  const messagesNodes = getMessagesDom(messages);
1885
1915
  const dom = [{
1886
1916
  childCount: 3,
1887
- className: Viewlet,
1917
+ className: Viewlet + ' Chat',
1888
1918
  onClick: HandleClick,
1889
1919
  onInput: HandleInput,
1890
1920
  onKeyDown: HandleKeyDown,
1891
1921
  type: Div
1892
1922
  }, {
1893
1923
  childCount: 2,
1894
- className: SideBarLocation,
1924
+ className: ChatHeader,
1895
1925
  type: Div
1896
1926
  }, {
1897
- childCount: 2,
1898
- className: Actions,
1927
+ childCount: 4,
1928
+ className: ChatActions,
1899
1929
  type: Div
1900
1930
  }, {
1901
1931
  childCount: 1,
@@ -1910,20 +1940,36 @@ const getStatusBarVirtualDom = (sessions, selectedSessionId, composerValue) => {
1910
1940
  title: 'New Chat',
1911
1941
  type: Button$1
1912
1942
  }, text('+'), {
1943
+ childCount: 1,
1944
+ className: IconButton,
1945
+ onClick: HandleClickSettings,
1946
+ role: Button$2,
1947
+ tabIndex: 0,
1948
+ title: 'Settings',
1949
+ type: Button$1
1950
+ }, text('⚙'), {
1951
+ childCount: 1,
1952
+ className: IconButton,
1953
+ onClick: HandleClickClose,
1954
+ role: Button$2,
1955
+ tabIndex: 0,
1956
+ title: 'Close Chat',
1957
+ type: Button$1
1958
+ }, text('×'), {
1913
1959
  childCount: sessions.length,
1914
- className: ListItems,
1960
+ className: ChatList,
1915
1961
  type: Div
1916
1962
  }, ...sessionNodes, {
1917
1963
  childCount: 2,
1918
- className: Editor,
1964
+ className: ChatDetails,
1919
1965
  type: Div
1920
1966
  }, {
1921
1967
  childCount: Math.max(messagesNodes.length, 0),
1922
- className: EditorContent,
1968
+ className: ChatDetailsContent,
1923
1969
  type: Div
1924
1970
  }, ...messagesNodes, {
1925
- childCount: 1,
1926
- className: Actions,
1971
+ childCount: 2,
1972
+ className: ChatActions,
1927
1973
  type: Div
1928
1974
  }, {
1929
1975
  childCount: 0,
@@ -1933,7 +1979,15 @@ const getStatusBarVirtualDom = (sessions, selectedSessionId, composerValue) => {
1933
1979
  rows: 4,
1934
1980
  type: TextArea,
1935
1981
  value: composerValue
1936
- }];
1982
+ }, {
1983
+ childCount: 1,
1984
+ className: Button,
1985
+ name: 'send',
1986
+ role: Button$2,
1987
+ tabIndex: 0,
1988
+ title: 'Send message',
1989
+ type: Button$1
1990
+ }, text('Send')];
1937
1991
  return dom;
1938
1992
  };
1939
1993
 
@@ -1948,7 +2002,7 @@ const renderItems = (oldState, newState) => {
1948
2002
  if (initial) {
1949
2003
  return [SetDom2, uid, []];
1950
2004
  }
1951
- const dom = getStatusBarVirtualDom(sessions, selectedSessionId, composerValue);
2005
+ const dom = getChatVirtualDom(sessions, selectedSessionId, composerValue);
1952
2006
  return [SetDom2, uid, dom];
1953
2007
  };
1954
2008
 
@@ -1961,6 +2015,8 @@ const renderIncremental = (oldState, newState) => {
1961
2015
 
1962
2016
  const getRenderer = diffType => {
1963
2017
  switch (diffType) {
2018
+ case RenderCss:
2019
+ return renderCss;
1964
2020
  case RenderIncremental:
1965
2021
  return renderIncremental;
1966
2022
  case RenderItems:
@@ -1996,6 +2052,12 @@ const renderEventListeners = () => {
1996
2052
  return [{
1997
2053
  name: HandleClick,
1998
2054
  params: ['handleClick', TargetName]
2055
+ }, {
2056
+ name: HandleClickClose,
2057
+ params: ['handleClickClose']
2058
+ }, {
2059
+ name: HandleClickSettings,
2060
+ params: ['handleClickSettings']
1999
2061
  }, {
2000
2062
  name: HandleInput,
2001
2063
  params: ['handleInput', TargetValue]
@@ -2031,12 +2093,36 @@ const saveState = state => {
2031
2093
  };
2032
2094
  };
2033
2095
 
2096
+ const dummySessions = [{
2097
+ id: 'session-a',
2098
+ messages: [],
2099
+ title: 'Dummy Chat A'
2100
+ }, {
2101
+ id: 'session-b',
2102
+ messages: [],
2103
+ title: 'Dummy Chat B'
2104
+ }, {
2105
+ id: 'session-c',
2106
+ messages: [],
2107
+ title: 'Dummy Chat C'
2108
+ }];
2109
+ const setChatList = state => {
2110
+ return {
2111
+ ...state,
2112
+ nextSessionId: dummySessions.length + 1,
2113
+ selectedSessionId: dummySessions[0].id,
2114
+ sessions: dummySessions
2115
+ };
2116
+ };
2117
+
2034
2118
  const commandMap = {
2035
2119
  'Chat.create': create,
2036
2120
  'Chat.diff2': diff2,
2037
2121
  'Chat.getCommandIds': getCommandIds,
2038
2122
  'Chat.getKeyBindings': getKeyBindings,
2039
2123
  'Chat.handleClick': wrapCommand(handleClick),
2124
+ 'Chat.handleClickClose': handleClickClose,
2125
+ 'Chat.handleClickSettings': handleClickSettings,
2040
2126
  'Chat.handleInput': wrapCommand(handleInput),
2041
2127
  'Chat.handleKeyDown': wrapCommand(handleKeyDown),
2042
2128
  'Chat.initialize': initialize,
@@ -2046,6 +2132,7 @@ const commandMap = {
2046
2132
  'Chat.renderEventListeners': renderEventListeners,
2047
2133
  'Chat.resize': wrapCommand(resize),
2048
2134
  'Chat.saveState': wrapGetter(saveState),
2135
+ 'Chat.setChatList': wrapCommand(setChatList),
2049
2136
  'Chat.terminate': terminate
2050
2137
  };
2051
2138
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-view",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Chat View Worker",
5
5
  "repository": {
6
6
  "type": "git",