1
1
export const dynamic = 'force-dynamic' // defaults to autover'
2
- import { GPTScript , type Block , Tool } from '@gptscript-ai/gptscript'
2
+ import { type Block , Tool } from '@gptscript-ai/gptscript'
3
3
import { Positions } from '../route' ;
4
4
import { promises as fs } from 'fs' ;
5
5
import path from 'path' ;
6
- import { SCRIPTS_PATH } from '@/config/env' ;
7
-
8
- const gptscript = new GPTScript ( ) ;
6
+ import { SCRIPTS_PATH , gpt } from '@/config/env' ;
9
7
10
8
// Create a datastructure for the tool bindings in the UI
11
9
export async function PUT (
@@ -15,11 +13,11 @@ export async function PUT(
15
13
try {
16
14
const { name, tool } = params as any ;
17
15
18
- const script = await gptscript . parse ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) ) ;
16
+ const script = await gpt ( ) . parse ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) ) ;
19
17
const updatedScript = updateScript ( script , tool , ( await req . json ( ) ) as Tool ) ;
20
18
21
- await fs . writeFile ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) , await gptscript . stringify ( updatedScript ) ) ;
22
- return Response . json ( await gptscript . parse ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) ) ) ;
19
+ await fs . writeFile ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) , await gpt ( ) . stringify ( updatedScript ) ) ;
20
+ return Response . json ( await gpt ( ) . parse ( path . join ( SCRIPTS_PATH ( ) , `${ name } .gpt` ) ) ) ;
23
21
} catch ( e ) {
24
22
if ( `${ e } ` . includes ( 'no such file' ) ) {
25
23
return Response . json ( { error : '.gpt file not found' } , { status : 404 } ) ;
@@ -39,7 +37,7 @@ const updateScript = (script: Block[], tool: string, updatedTool: Tool) => {
39
37
if ( tool !== updatedTool . name ) {
40
38
updatedScript = script . map ( block => {
41
39
if ( block . type === 'tool' && block . name !== tool ) {
42
- block . tools = block . tools ?. map ( t => t === tool ? updatedTool . name : t ) ;
40
+ block . tools = block . tools ?. map ( t => t === tool ? updatedTool . name : t ) as string [ ] | undefined ;
43
41
} else if ( block . type === 'text' ) {
44
42
const positions = JSON . parse ( block . content ) as Positions
45
43
block . content = JSON . stringify (
0 commit comments