Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix ai-native package #383

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading