@difizen/libro-ai-native 0.2.41-next.3 → 0.2.41-next.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@difizen/libro-ai-native",
3
- "version": "0.2.41-next.3",
3
+ "version": "0.2.41-next.5",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "libro",
@@ -32,7 +32,7 @@
32
32
  "src"
33
33
  ],
34
34
  "dependencies": {
35
- "@difizen/libro-jupyter": "0.2.41-next.3",
35
+ "@difizen/libro-jupyter": "0.2.41-next.5",
36
36
  "@ant-design/icons": "^5.4.0",
37
37
  "react-zoom-pan-pinch": "^3.6.1",
38
38
  "@difizen/mana-app": "latest",
@@ -198,10 +198,12 @@ export class LibroAINativeCommandContribution
198
198
  );
199
199
  libroAINativeForCellView.showAI = true;
200
200
  addCellAIClassname(cell);
201
- libroAINativeForCellView.chatStream({
202
- content: l10n.getLang()
201
+ const code =
202
+ l10n.getLang() === 'en-US'
203
203
  ? `Could you please explain this piece of code?:${cell.model.value},Provide the reasons and the results of the optimization.`
204
- : `帮忙优化一下这段代码:${cell.model.value},给出原因以及优化结果`,
204
+ : `帮忙优化一下这段代码:${cell.model.value},给出原因以及优化结果`;
205
+ libroAINativeForCellView.chatStream({
206
+ content: code,
205
207
  });
206
208
  // this.libroService.active?.enterEditMode();
207
209
  },
@@ -229,11 +231,13 @@ export class LibroAINativeCommandContribution
229
231
  cell,
230
232
  );
231
233
  libroAINativeForCellView.showAI = true;
234
+ const code =
235
+ l10n.getLang() === 'en-US'
236
+ ? `Please help optimize this piece of code: ${cell.model.value},provide reasons and the optimized result.`
237
+ : `帮忙优化一下这段代码:${cell.model.value},给出原因以及优化结果`;
232
238
  addCellAIClassname(cell);
233
239
  libroAINativeForCellView.chatStream({
234
- content: l10n.getLang()
235
- ? `Please help optimize this piece of code: ${cell.model.value},provide reasons and the optimized result.`
236
- : `帮忙优化一下这段代码:${cell.model.value},给出原因以及优化结果`,
240
+ content: code,
237
241
  });
238
242
  // this.libroService.active?.enterEditMode();
239
243
  },
@@ -16,6 +16,7 @@ import { inject } from '@difizen/mana-app';
16
16
  import { BaseView, transient, view } from '@difizen/mana-app';
17
17
  import { l10n } from '@difizen/mana-l10n';
18
18
  import { Button } from 'antd';
19
+ import type { ParsedEvent } from 'eventsource-parser/stream';
19
20
  import { EventSourceParserStream } from 'eventsource-parser/stream';
20
21
  import breaks from 'remark-breaks';
21
22
  import remarkGfm from 'remark-gfm';
@@ -100,7 +101,7 @@ export class LibroAINativeForCellView extends BaseView {
100
101
  const url = `/libro/api/chatstream`;
101
102
  const msg = {
102
103
  chat_key: chat_key,
103
- prompt: content,
104
+ prompt: encodeURIComponent(content),
104
105
  language: language,
105
106
  };
106
107
  const res = await this.fetcher.post<ReadableStream<Uint8Array>>(url, msg, {
@@ -130,9 +131,13 @@ export class LibroAINativeForCellView extends BaseView {
130
131
  });
131
132
  this.libroAIChatMessageItemModel = msgItem;
132
133
  let alreayDone = false;
134
+ let error: ParsedEvent = { data: '', type: 'event' };
133
135
  try {
134
136
  while (!alreayDone) {
135
137
  const { value, done } = await reader.read();
138
+ if (value) {
139
+ error = value;
140
+ }
136
141
  if (done) {
137
142
  alreayDone = true;
138
143
  msgItem.handleEventData({
@@ -146,6 +151,7 @@ export class LibroAINativeForCellView extends BaseView {
146
151
  msgItem.handleEventData(event);
147
152
  }
148
153
  } catch {
154
+ console.error('libro-ai-error:' + error.data);
149
155
  msgItem.handleEventData({
150
156
  type: 'error',
151
157
  });
package/src/index.less CHANGED
@@ -265,7 +265,7 @@
265
265
 
266
266
  .libro-ai-native-for-cell-container {
267
267
  display: flex;
268
- padding-top: 18px;
268
+ padding: 18px 0;
269
269
  justify-content: space-between;
270
270
  }
271
271
 
@@ -2,6 +2,7 @@ import type { IChatEvent, IChatMessageItem } from '@difizen/magent-chat';
2
2
  import { ChatEvent } from '@difizen/magent-chat';
3
3
  import { LibroChatService } from '@difizen/magent-libro';
4
4
  import { singleton } from '@difizen/mana-app';
5
+ import type { ParsedEvent } from 'eventsource-parser/stream';
5
6
  import { EventSourceParserStream } from 'eventsource-parser/stream';
6
7
 
7
8
  import type { LibroAINativeChatMessageItemOption } from './protocol.js';
@@ -14,7 +15,7 @@ export class LibroAINativeChatService extends LibroChatService {
14
15
  ): Promise<IChatMessageItem[]> => {
15
16
  const { content, system_prompt } = option;
16
17
  const res = await this.fetcher.post<any>(`/libro/api/chat`, {
17
- prompt: content,
18
+ prompt: encodeURIComponent(content),
18
19
  system_prompt,
19
20
  });
20
21
 
@@ -39,7 +40,7 @@ export class LibroAINativeChatService extends LibroChatService {
39
40
 
40
41
  const url = `/libro/api/chatstream`;
41
42
  const msg = {
42
- prompt: content,
43
+ prompt: encodeURIComponent(content),
43
44
  system_prompt,
44
45
  };
45
46
  const res = await this.fetcher.post<ReadableStream<Uint8Array>>(url, msg, {
@@ -69,6 +70,7 @@ export class LibroAINativeChatService extends LibroChatService {
69
70
  content: '',
70
71
  });
71
72
  let alreayDone = false;
73
+ let error: ParsedEvent = { data: '', type: 'event' };
72
74
  try {
73
75
  while (!alreayDone) {
74
76
  const { value, done } = await reader.read();
@@ -80,13 +82,18 @@ export class LibroAINativeChatService extends LibroChatService {
80
82
 
81
83
  break;
82
84
  }
85
+ if (value) {
86
+ error = value;
87
+ }
83
88
  const data = JSON.parse(value.data);
84
89
  const event = ChatEvent.format(value.event || 'chunk', data);
85
90
  eventCallback(event);
86
91
  }
87
92
  } catch {
93
+ console.error('libro-ai-error:' + error.data);
88
94
  eventCallback({
89
95
  type: 'error',
96
+ message: error.data,
90
97
  });
91
98
  }
92
99
  return;