feat: implement enhance prompt (#294)

* feat: implement enhance prompt

* add unit test

* fix prompt

* fix: fix eslint and compiling issues

* feat: add border-beam animation

* fix: fix importing issues

---------

Co-authored-by: Henry Li <henry1943@163.com>
This commit is contained in:
DanielWalnut
2025-06-08 19:41:59 +08:00
committed by GitHub
parent 8081a14c21
commit 1cd6aa0ece
19 changed files with 1100 additions and 4 deletions

View File

@@ -4,4 +4,5 @@
export * from "./chat";
export * from "./mcp";
export * from "./podcast";
export * from "./prompt-enhancer";
export * from "./types";

View File

@@ -0,0 +1,62 @@
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
import { resolveServiceURL } from "./resolve-service-url";
export interface EnhancePromptRequest {
prompt: string;
context?: string;
report_style?: string;
}
export interface EnhancePromptResponse {
enhanced_prompt: string;
}
export async function enhancePrompt(
request: EnhancePromptRequest,
): Promise<string> {
const response = await fetch(resolveServiceURL("prompt/enhance"), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(request),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log("Raw API response:", data); // Debug log
// The backend now returns the enhanced prompt directly in the result field
let enhancedPrompt = data.result;
// If the result is somehow still a JSON object, extract the enhanced_prompt
if (typeof enhancedPrompt === "object" && enhancedPrompt.enhanced_prompt) {
enhancedPrompt = enhancedPrompt.enhanced_prompt;
}
// If the result is a JSON string, try to parse it
if (typeof enhancedPrompt === "string") {
try {
const parsed = JSON.parse(enhancedPrompt);
if (parsed.enhanced_prompt) {
enhancedPrompt = parsed.enhanced_prompt;
}
} catch {
// If parsing fails, use the string as-is (which is what we want)
console.log("Using enhanced prompt as-is:", enhancedPrompt);
}
}
// Fallback to original prompt if something went wrong
if (!enhancedPrompt || enhancedPrompt.trim() === "") {
console.warn("No enhanced prompt received, using original");
enhancedPrompt = request.prompt;
}
return enhancedPrompt;
}