|
1 | | -import { useState, useEffect, useCallback } from "react"; |
| 1 | +import { useState, useEffect, useCallback, useRef } from "react"; |
2 | 2 | import { Drawer, Form, Input, Button, message } from "@agentscope-ai/design"; |
3 | 3 | import { useTranslation } from "react-i18next"; |
| 4 | +import { ThunderboltOutlined, StopOutlined } from "@ant-design/icons"; |
4 | 5 | import type { FormInstance } from "antd"; |
5 | 6 | import type { SkillSpec } from "../../../../api/types"; |
6 | 7 | import { MarkdownCopy } from "../../../../components/MarkdownCopy/MarkdownCopy"; |
| 8 | +import { api } from "../../../../api"; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * Parse frontmatter from content string. |
@@ -48,9 +50,11 @@ export function SkillDrawer({ |
48 | 50 | onSubmit, |
49 | 51 | onContentChange, |
50 | 52 | }: SkillDrawerProps) { |
51 | | - const { t } = useTranslation(); |
| 53 | + const { t, i18n } = useTranslation(); |
52 | 54 | const [showMarkdown, setShowMarkdown] = useState(true); |
53 | 55 | const [contentValue, setContentValue] = useState(""); |
| 56 | + const [optimizing, setOptimizing] = useState(false); |
| 57 | + const abortControllerRef = useRef<AbortController | null>(null); |
54 | 58 |
|
55 | 59 | const validateFrontmatter = useCallback( |
56 | 60 | (_: unknown, value: string) => { |
@@ -105,13 +109,55 @@ export function SkillDrawer({ |
105 | 109 | const handleContentChange = (content: string) => { |
106 | 110 | setContentValue(content); |
107 | 111 | form.setFieldsValue({ content }); |
108 | | - // Re-validate the content field to give real-time feedback |
109 | 112 | form.validateFields(["content"]).catch(() => {}); |
110 | 113 | if (onContentChange) { |
111 | 114 | onContentChange(content); |
112 | 115 | } |
113 | 116 | }; |
114 | 117 |
|
| 118 | + const handleOptimize = async () => { |
| 119 | + if (!contentValue.trim()) { |
| 120 | + message.warning(t("skills.noContentToOptimize")); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + setOptimizing(true); |
| 125 | + abortControllerRef.current = new AbortController(); |
| 126 | + const originalContent = contentValue; |
| 127 | + setContentValue(""); // Clear content for streaming output |
| 128 | + |
| 129 | + try { |
| 130 | + await api.streamOptimizeSkill( |
| 131 | + originalContent, |
| 132 | + (textChunk) => { |
| 133 | + setContentValue((prev) => { |
| 134 | + const newContent = prev + textChunk; |
| 135 | + form.setFieldsValue({ content: newContent }); |
| 136 | + return newContent; |
| 137 | + }); |
| 138 | + }, |
| 139 | + abortControllerRef.current.signal, |
| 140 | + i18n.language, // Pass current language to API |
| 141 | + ); |
| 142 | + message.success(t("skills.optimizeSuccess")); |
| 143 | + } catch (error: any) { |
| 144 | + if (error.name !== "AbortError") { |
| 145 | + message.error(error.message || t("skills.optimizeFailed")); |
| 146 | + } |
| 147 | + } finally { |
| 148 | + setOptimizing(false); |
| 149 | + abortControllerRef.current = null; |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + const handleStopOptimize = () => { |
| 154 | + if (abortControllerRef.current) { |
| 155 | + abortControllerRef.current.abort(); |
| 156 | + setOptimizing(false); |
| 157 | + abortControllerRef.current = null; |
| 158 | + } |
| 159 | + }; |
| 160 | + |
115 | 161 | return ( |
116 | 162 | <Drawer |
117 | 163 | width={520} |
@@ -154,15 +200,37 @@ export function SkillDrawer({ |
154 | 200 | <div |
155 | 201 | style={{ |
156 | 202 | display: "flex", |
157 | | - justifyContent: "flex-end", |
158 | | - gap: 8, |
| 203 | + justifyContent: "space-between", |
159 | 204 | marginTop: 16, |
160 | 205 | }} |
161 | 206 | > |
162 | | - <Button onClick={onClose}>{t("common.cancel")}</Button> |
163 | | - <Button type="primary" htmlType="submit"> |
164 | | - {t("skills.create")} |
165 | | - </Button> |
| 207 | + <div style={{ display: "flex", gap: 8 }}> |
| 208 | + {!optimizing ? ( |
| 209 | + <Button |
| 210 | + type="default" |
| 211 | + icon={<ThunderboltOutlined />} |
| 212 | + onClick={handleOptimize} |
| 213 | + disabled={!contentValue.trim()} |
| 214 | + > |
| 215 | + {t("skills.optimizeWithAI")} |
| 216 | + </Button> |
| 217 | + ) : ( |
| 218 | + <Button |
| 219 | + type="default" |
| 220 | + danger |
| 221 | + icon={<StopOutlined />} |
| 222 | + onClick={handleStopOptimize} |
| 223 | + > |
| 224 | + {t("skills.stopOptimize")} |
| 225 | + </Button> |
| 226 | + )} |
| 227 | + </div> |
| 228 | + <div style={{ display: "flex", gap: 8 }}> |
| 229 | + <Button onClick={onClose}>{t("common.cancel")}</Button> |
| 230 | + <Button type="primary" htmlType="submit"> |
| 231 | + {t("skills.create")} |
| 232 | + </Button> |
| 233 | + </div> |
166 | 234 | </div> |
167 | 235 | </Form.Item> |
168 | 236 | </> |
|
0 commit comments