Skip to content

Commit

Permalink
fix: fix ai-native package
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk committed Feb 7, 2025
1 parent cb5004d commit 9a0515d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 83 deletions.
138 changes: 69 additions & 69 deletions packages/libro-ai-native/src/ai-native-code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,77 +62,77 @@ export const CodeBlockInCell = (props: any) => {
return <code className={`chat-msg-md-code-code`}>{children}</code>;
};

// export const CodeBlockInChat = (props: any) => {
// const { className, children } = props;
// const instance = useInject<LibroAiNativeChatView>(ViewInstance);
// const cell = instance.libro?.activeCell;
export const CodeBlockInChat = (props: any) => {
const { className, children } = props;
const instance = useInject<LibroAiNativeChatView>(ViewInstance);
const cell = instance.libro?.activeCell;

// const insertCell = async (data: any) => {
// const libro = instance.libro;
// if (!libro) {
// return;
// }
// const insertIndex = libro.model.cells.findIndex(
// (c) => c.id === libro.activeCell?.id,
// );
// await libro.addCell(
// {
// id: v4(),
// cell: { cell_type: 'code', source: data, metadata: {} },
// },
// insertIndex + 1,
// );
// };
const insertCell = async (data: any) => {
const libro = instance.libro;
if (!libro) {
return;
}
const insertIndex = libro.model.cells.findIndex(
(c) => c.id === libro.activeCell?.id,
);
await libro.addCell(
{
id: v4(),
cell: { cell_type: 'code', source: data, metadata: {} },
},
insertIndex + 1,
);
};

// const replace = (data: any) => {
// if (cell instanceof LibroCodeCellView && cell.editor) {
// const length = cell.editor.model.value.length;
// const start = cell.editor.getPositionAt(0);
// const end = cell.editor.getPositionAt(length);
// if (start && end) {
// cell.editor.replaceSelection(data, {
// start,
// end,
// });
// }
// }
// };
const replace = (data: any) => {
if (cell instanceof LibroCodeCellView && cell.editor) {
const length = cell.editor.model.value.length;
const start = cell.editor.getPositionAt(0);
const end = cell.editor.getPositionAt(length);
if (start && end) {
cell.editor.replaceSelection(data, {
start,
end,
});
}
}
};

// if (!props.inline && className) {
// const [, lang] = /language-(\w+)/.exec(className || '') || [];
if (!props.inline && className) {
const [, lang] = /language-(\w+)/.exec(className || '') || [];

// return (
// <pre className={`chat-msg-md-code-wrap`}>
// {lang && <div className={`chat-msg-md-code-lang`}>{lang}</div>}
// <CopyOutlined
// onClick={() => {
// copy(children);
// message.success(l10n.t('代码已复制'));
// }}
// className={`chat-msg-md-code-copy`}
// />
// <SyntaxHighlighter
// className={`libro-llm-syntax-highlighter`}
// language={lang}
// style={{}}
// >
// {typeof children === 'string' ? children.trim() : children}
// </SyntaxHighlighter>
// <div className="libro-ai-native-debug-code-btn-container">
// <Button
// color="primary"
// variant="outlined"
// onClick={() => insertCell(children)}
// >
// {l10n.t('插入 Cell')}
// </Button>
// <Button color="primary" variant="outlined" onClick={() => replace(children)}>
// {l10n.t('替换 Cell')}
// </Button>
// </div>
// </pre>
// );
// }
return (
<pre className={`chat-msg-md-code-wrap`}>
{lang && <div className={`chat-msg-md-code-lang`}>{lang}</div>}
<CopyOutlined
onClick={() => {
copy(children);
message.success(l10n.t('代码已复制'));
}}
className={`chat-msg-md-code-copy`}
/>
<SyntaxHighlighter
className={`libro-llm-syntax-highlighter`}
language={lang}
style={{}}
>
{typeof children === 'string' ? children.trim() : children}
</SyntaxHighlighter>
<div className="libro-ai-native-debug-code-btn-container">
<Button
color="primary"
variant="outlined"
onClick={() => insertCell(children)}
>
{l10n.t('插入 Cell')}
</Button>
<Button color="primary" variant="outlined" onClick={() => replace(children)}>
{l10n.t('替换 Cell')}
</Button>
</div>
</pre>
);
}

// return <code className={`chat-msg-md-code-code`}>{children}</code>;
// };
return <code className={`chat-msg-md-code-code`}>{children}</code>;
};
28 changes: 14 additions & 14 deletions packages/libro-ai-native/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CodeEditorModule } from '@difizen/libro-code-editor';
import { ManaModule } from '@difizen/libro-common/app';
import { CellOutputTopProvider, ErrorOutputModel } from '@difizen/libro-jupyter';
// import { ChatView } from '@difizen/magent-chat';
// import { LibroChatModule, LibroChatService } from '@difizen/magent-libro';
import { ChatView } from '@difizen/magent-chat';
import { LibroChatModule, LibroChatService } from '@difizen/magent-libro';

import { LibroAICompletionModule } from './ai-inline-completions/module.js';
import { LibroAINativeCommandContribution } from './ai-native-command-contribution.js';
Expand All @@ -13,8 +13,8 @@ import { LibroAIChatSlotContribution } from './chat-slot-contribution.js';
import { LibroChatView } from './chat-view.js';
import { AIErrorOutputModel } from './error-output-model.js';
import { LibroAIChatMessageItemModel } from './libro-ai-msg-item-model.js';
// import { LibroAINativeChatService } from './libro-ai-native-chat-service.js';
// import { LibroAiNativeChatView } from './libro-ai-native-chat-view.js';
import { LibroAINativeChatService } from './libro-ai-native-chat-service.js';
import { LibroAiNativeChatView } from './libro-ai-native-chat-view.js';
import { LibroAINativeColorRegistry } from './libro-ai-native-color-registry.js';

export const LibroAINativeModuleSetting = {
Expand All @@ -29,23 +29,23 @@ export const LibroAINativeModule = ManaModule.create()
LibroAINativeCommandContribution,
LibroAINativeService,
LibroAIChatMessageItemModel,
// LibroAINativeForCellView,
LibroAINativeForCellView,
{
token: ErrorOutputModel,
useClass: AIErrorOutputModel,
},
// {
// token: ChatView,
// useClass: LibroAiNativeChatView,
// },
// {
// token: LibroChatService,
// useClass: LibroAINativeChatService,
// },
{
token: ChatView,
useClass: LibroAiNativeChatView,
},
{
token: LibroChatService,
useClass: LibroAINativeChatService,
},
{
token: CellOutputTopProvider,
useValue: LibroAINativeCellTopBlank,
},
)
.canload(() => Promise.resolve(LibroAINativeModuleSetting.loadable))
.dependOn(CodeEditorModule, LibroAICompletionModule);
.dependOn(LibroChatModule, CodeEditorModule, LibroAICompletionModule);

0 comments on commit 9a0515d

Please sign in to comment.