107 lines
4.0 KiB
Svelte
107 lines
4.0 KiB
Svelte
<script lang="ts">
|
||
import { resolve } from "$app/paths";
|
||
import type { PromptTool } from "./state.svelte";
|
||
import Button from "$lib/components/ui/Button.svelte";
|
||
import { toast } from "$lib/toast";
|
||
let { tool }: { tool: PromptTool } = $props();
|
||
async function copy() {
|
||
try {
|
||
await navigator.clipboard.writeText(tool.result?.result ?? "");
|
||
toast.success("已复制");
|
||
} catch {
|
||
toast.error("复制失败,请手动选择文本");
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<svelte:window
|
||
onkeydown={(e) => {
|
||
if (e.key === "Escape" && tool.state !== "idle") {
|
||
if (tool.state === "loading") tool.cancel();
|
||
else tool.close();
|
||
}
|
||
}}
|
||
/>
|
||
{#if tool.state !== "idle"}
|
||
<section class="ptband" aria-label="提示词工具结果" aria-live="polite">
|
||
<div class="pt-h">
|
||
<b>{tool.action === "translate" ? "翻译" : "润色"} · {tool.label}</b>
|
||
{#if tool.result}<span class="pt-dir"
|
||
>{tool.result.direction
|
||
? (tool.result.direction === "zh-en" ? "中→英" : "英→中") +
|
||
" · " +
|
||
(tool.result.direction_source === "auto"
|
||
? "自动判断"
|
||
: "手动换向")
|
||
: "同语言润色"}</span
|
||
>{/if}
|
||
<span class="mg-grow"></span>
|
||
{#if tool.state === "loading"}<Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onclick={tool.cancel}>取消</Button
|
||
>
|
||
{:else if tool.state === "result"}
|
||
{#if tool.confirm}<span class="pt-confirm">替换会覆盖你刚才的修改</span
|
||
><Button size="sm" variant="ghost" onclick={tool.cancelConfirm}
|
||
>取消</Button
|
||
><Button size="sm" variant="scene" onclick={tool.replace}
|
||
>仍然替换</Button
|
||
>
|
||
{:else}<Button size="sm" variant="ghost" onclick={copy}>复制</Button
|
||
>{#if tool.action === "translate"}<Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onclick={() =>
|
||
tool.run(
|
||
"translate",
|
||
tool.result?.direction === "zh-en" ? "en-zh" : "zh-en",
|
||
)}>换向重译</Button
|
||
>{/if}<Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onclick={() => tool.run(tool.action)}
|
||
>重新{tool.action === "translate" ? "翻译" : "润色"}</Button
|
||
><Button size="sm" variant="scene" onclick={tool.replace}>替换</Button
|
||
>{/if}
|
||
{:else}<Button size="sm" onclick={() => tool.run(tool.action)}
|
||
>按当前文本重新处理</Button
|
||
>{/if}
|
||
<button
|
||
type="button"
|
||
class="pt-btn"
|
||
aria-label="关闭提示词结果"
|
||
onclick={() =>
|
||
tool.state === "loading" ? tool.cancel() : tool.close()}>×</button
|
||
>
|
||
</div>
|
||
<div class="pt-b">
|
||
{#if tool.text.length > 8000}<small>内容较长,处理可能较慢</small>{/if}
|
||
{#if tool.state === "loading"}<div class="pt-wait">
|
||
<i></i><i></i>你可以继续编辑原文;返回前若原文有改动,这次结果会被丢弃
|
||
</div>
|
||
{:else if tool.state === "stale"}<p class="pt-confirm">
|
||
原文已修改,结果已丢弃
|
||
</p>
|
||
{:else if tool.state === "error"}<p class="pt-error">{tool.error}</p>
|
||
<a href={resolve(`/connections?tab=llm&role=${tool.action}`)}
|
||
>去指定{tool.action === "translate" ? "翻译" : "润色"}模型</a
|
||
>
|
||
{:else if tool.result}
|
||
{#if tool.changed}<p class="pt-confirm">
|
||
原文已修改,替换会覆盖新改动
|
||
</p>{/if}
|
||
<div class="pt-res">{tool.result.result}</div>
|
||
<div class="pt-note">
|
||
<span
|
||
>{tool.result.model.following_default ? "跟随默认 · " : ""}{tool
|
||
.result.model.name} · {tool.result.elapsed_ms} ms</span
|
||
><span class="pt-verified"
|
||
>技术片段已逐字核对 {tool.result.protected_fragments.length} 处</span
|
||
><span>原提示词未改动 · 点「替换」才会写入</span>
|
||
</div>
|
||
{/if}
|
||
</div>
|
||
</section>
|
||
{/if}
|