|
| 1 | +"use server" |
| 2 | + |
| 3 | +import {create, del, get, list, update} from "@/actions/common" |
| 4 | +import { GATEWAY_URL, gpt } from "@/config/env" |
| 5 | +import { Block } from "@gptscript-ai/gptscript" |
| 6 | + |
| 7 | +export interface ParsedScript extends Script { |
| 8 | + agentName?: string |
| 9 | + description?: string |
| 10 | + script: Block[]; |
| 11 | +} |
| 12 | + |
| 13 | +export interface Script { |
| 14 | + displayName?: string |
| 15 | + createdAt?: string |
| 16 | + updatedAt?: string |
| 17 | + content?: string |
| 18 | + id?: number |
| 19 | + owner?: string |
| 20 | + tags?: string[] |
| 21 | + visibility?: string |
| 22 | + publicURL?: string |
| 23 | + slug?: string |
| 24 | +} |
| 25 | + |
| 26 | +export interface ScriptsQuery { |
| 27 | + owner?: string |
| 28 | + filter?: string |
| 29 | + limit?: number |
| 30 | + continue?: string |
| 31 | + search?: string |
| 32 | +} |
| 33 | + |
| 34 | +export interface ScriptsQueryResponse { |
| 35 | + continue?: string |
| 36 | + scripts?: Script[] |
| 37 | +} |
| 38 | + |
| 39 | +export interface ParsedScriptsQueryResponse { |
| 40 | + continue?: string |
| 41 | + scripts?: ParsedScript[] |
| 42 | +} |
| 43 | + |
| 44 | +// note: can combine these two functions into one to save cycles |
| 45 | +function getDescription(script: Block[]): string { |
| 46 | + for (let tool of script) { |
| 47 | + if (tool.type === 'text') continue; |
| 48 | + return tool.description || ''; |
| 49 | + } |
| 50 | + return ''; |
| 51 | +} |
| 52 | +function getName(script: Block[]): string { |
| 53 | + for (let tool of script) { |
| 54 | + if (tool.type === 'text') continue; |
| 55 | + return tool.name || ''; |
| 56 | + } |
| 57 | + return ''; |
| 58 | +} |
| 59 | + |
| 60 | +export async function getScripts(query?: ScriptsQuery): Promise<ParsedScriptsQueryResponse> { |
| 61 | + let scripts: ScriptsQueryResponse = {}; |
| 62 | + if (!query) scripts = await list("scripts"); |
| 63 | + else scripts = await list("scripts?" + new URLSearchParams(query as any).toString()); |
| 64 | + |
| 65 | + let parsedScripts: ParsedScript[] = []; |
| 66 | + for (const script of scripts.scripts || []) { |
| 67 | + const parsedScript = await gpt().parseTool(script.content || ''); |
| 68 | + |
| 69 | + parsedScripts.push({ ...script, |
| 70 | + script: parsedScript, |
| 71 | + description: getDescription(parsedScript), |
| 72 | + agentName: getName(parsedScript) |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + return { continue: scripts.continue, scripts: parsedScripts }; |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +export async function getScript(id: string): Promise<ParsedScript> { |
| 81 | + const scripts = await get("scripts", id.replace(`${GATEWAY_URL()}/`, '')) as Script |
| 82 | + const parsedScript = await gpt().parseTool(scripts.content || '') |
| 83 | + return { ...scripts, |
| 84 | + script: parsedScript, |
| 85 | + description: getDescription(parsedScript), |
| 86 | + agentName: getName(parsedScript) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export async function createScript(script: Script) { |
| 91 | + return await create(script, `scripts`) |
| 92 | +} |
| 93 | + |
| 94 | +export async function updateScript(script: Script) { |
| 95 | + return await update(script, `scripts/${script.id}`) |
| 96 | +} |
| 97 | + |
| 98 | +export async function deleteScript(script: Script) { |
| 99 | + return await del(`${script.id}`, "scripts") |
| 100 | +} |
| 101 | + |
| 102 | +export async function getScriptContent(scriptURL: string) { |
| 103 | + const script = await gpt().parse(scriptURL); |
| 104 | + return gpt().stringify(script); |
| 105 | +} |
0 commit comments