Skip to content

Commit

Permalink
fix: reduce the number of new GPTScript clients created
Browse files Browse the repository at this point in the history
Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Jul 3, 2024
1 parent 4ca27f8 commit 776d6d0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
11 changes: 4 additions & 7 deletions actions/scripts/fetch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server"
import { Tool, GPTScript, Block } from '@gptscript-ai/gptscript';
import { SCRIPTS_PATH } from '@/config/env';
import { SCRIPTS_PATH, gpt } from '@/config/env';
import fs from 'fs/promises';

const external = (file: string): boolean => {
Expand All @@ -15,9 +15,8 @@ export const path = async (file: string): Promise<string> => {
export const fetchFullScript = async (file: string): Promise<Block[]> => {
if (!external(file)) file = `${SCRIPTS_PATH()}/${file}.gpt`;

const gptscript = new GPTScript();
try {
return await gptscript.parse(file);
return await gpt().parse(file);
} catch (e) {
throw e;
}
Expand All @@ -26,9 +25,8 @@ export const fetchFullScript = async (file: string): Promise<Block[]> => {
export const fetchScript = async (file: string): Promise<Tool> => {
if (!external(file)) file = `${SCRIPTS_PATH()}/${file}.gpt`;

const gptscript = new GPTScript();
try {
const script = await gptscript.parse(file);
const script = await gpt().parse(file);
for (let tool of script) {
if (tool.type === 'text') continue;
return tool;
Expand All @@ -49,10 +47,9 @@ export const fetchScripts = async (): Promise<Record<string, string>> => {

if (gptFiles.length === 0) throw new Error('no files found in scripts directory');

const gptscript = new GPTScript();
const scripts: Record<string, string> = {};
for (const file of gptFiles) {
const script = await gptscript.parse(`${SCRIPTS_PATH()}/${file}`);
const script = await gpt().parse(`${SCRIPTS_PATH()}/${file}`);
let description = '';
for (let tool of script) {
if (tool.type === 'text') continue;
Expand Down
8 changes: 3 additions & 5 deletions actions/scripts/update.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server"

import { Tool, GPTScript, Block } from '@gptscript-ai/gptscript';
import { SCRIPTS_PATH } from '@/config/env';
import { SCRIPTS_PATH, gpt} from '@/config/env';
import fs from 'fs/promises';

const external = (file: string): boolean => {
Expand All @@ -16,9 +16,8 @@ export const path = async (file: string): Promise<string> => {
export const updateScript = async (file: string, script: Block[]) => {
if (external(file)) throw new Error('cannot update external tools');

const gptscript = new GPTScript();
try {
await fs.writeFile(`${SCRIPTS_PATH()}/${file}.gpt`, await gptscript.stringify(script));
await fs.writeFile(`${SCRIPTS_PATH()}/${file}.gpt`, await gpt().stringify(script));
} catch (e) {
throw e;
}
Expand All @@ -27,9 +26,8 @@ export const updateScript = async (file: string, script: Block[]) => {
export const updateTool = async (file: string, name: string, script: Block[]) => {
if (external(file)) throw new Error('cannot update external tools');

const gptscript = new GPTScript();
try {
return await gptscript.stringify(script);
return await gpt().stringify(script);
} catch (e) {
throw e;
}
Expand Down
14 changes: 6 additions & 8 deletions app/api/file/[name]/[tool]/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
export const dynamic = 'force-dynamic' // defaults to autover'
import { GPTScript, type Block, Tool} from '@gptscript-ai/gptscript'
import {type Block, Tool} from '@gptscript-ai/gptscript'
import { Positions } from '../route';
import { promises as fs } from 'fs';
import path from 'path';
import { SCRIPTS_PATH } from '@/config/env';

const gptscript = new GPTScript();
import { SCRIPTS_PATH, gpt} from '@/config/env';

// Create a datastructure for the tool bindings in the UI
export async function PUT(
Expand All @@ -15,11 +13,11 @@ export async function PUT(
try {
const { name, tool } = params as any;

const script = await gptscript.parse(path.join(SCRIPTS_PATH(),`${name}.gpt`));
const script = await gpt().parse(path.join(SCRIPTS_PATH(),`${name}.gpt`));
const updatedScript = updateScript(script, tool, (await req.json()) as Tool);

await fs.writeFile(path.join(SCRIPTS_PATH(),`${name}.gpt`), await gptscript.stringify(updatedScript));
return Response.json(await gptscript.parse(path.join(SCRIPTS_PATH(),`${name}.gpt`)));
await fs.writeFile(path.join(SCRIPTS_PATH(),`${name}.gpt`), await gpt().stringify(updatedScript));
return Response.json(await gpt().parse(path.join(SCRIPTS_PATH(),`${name}.gpt`)));
} catch (e) {
if (`${e}`.includes('no such file')){
return Response.json({ error: '.gpt file not found' }, { status: 404 });
Expand All @@ -39,7 +37,7 @@ const updateScript = (script: Block[], tool: string, updatedTool: Tool) => {
if (tool !== updatedTool.name) {
updatedScript = script.map(block => {
if (block.type === 'tool' && block.name !== tool) {
block.tools = block.tools?.map(t => t === tool ? updatedTool.name : t);
block.tools = block.tools?.map(t => t === tool ? updatedTool.name : t) as string[] | undefined;
} else if (block.type === 'text') {
const positions = JSON.parse(block.content) as Positions
block.content = JSON.stringify(
Expand Down
13 changes: 5 additions & 8 deletions app/api/file/[name]/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
export const dynamic = 'force-dynamic' // defaults to auto
import { NextRequest } from 'next/server'
import { GPTScript, type Block, Text } from '@gptscript-ai/gptscript'
import { type Block, Text } from '@gptscript-ai/gptscript'
import { promises as fs } from 'fs';
import path from 'path';
import { SCRIPTS_PATH } from '@/config/env';
import { SCRIPTS_PATH, gpt } from '@/config/env';
import type {
Node as RFNode,
Edge as RFEdge,
XYPosition,
} from 'reactflow';

const gptscript = new GPTScript();


export async function DELETE(
_: NextRequest,
{ params }: { params: { slug: string } }
Expand Down Expand Up @@ -46,7 +43,7 @@ export async function GET(
) {
try {
const { name } = params as any;
const script = await gptscript.parse(path.join(SCRIPTS_PATH(),`${name}.gpt`));
const script = await gpt().parse(path.join(SCRIPTS_PATH(),`${name}.gpt`));
if (req.nextUrl.searchParams.get('nodeify') === 'true') {
const { nodes, edges } = await nodeify(script);
return Response.json({ nodes: nodes, edges: edges });
Expand All @@ -69,8 +66,8 @@ export async function PUT(
const nodes = (await req.json()) as RFNode[];
const script = denodeify(nodes);

await fs.writeFile(path.join(SCRIPTS_PATH(),`${name}.gpt`), await gptscript.stringify(script));
return Response.json(await gptscript.parse(path.join(SCRIPTS_PATH(),`${name}.gpt`)));
await fs.writeFile(path.join(SCRIPTS_PATH(),`${name}.gpt`), await gpt().stringify(script));
return Response.json(await gpt().parse(path.join(SCRIPTS_PATH(),`${name}.gpt`)));
} catch (e) {
if (`${e}`.includes('no such file')){
return Response.json({ error: '.gpt file not found' }, { status: 404 });
Expand Down
6 changes: 2 additions & 4 deletions app/api/file/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export const dynamic = 'force-dynamic' // defaults to auto
import { GPTScript } from '@gptscript-ai/gptscript'
import { promises as fs } from 'fs';
import { SCRIPTS_PATH } from '@/config/env';

const gptscript = new GPTScript();
import { SCRIPTS_PATH, gpt } from '@/config/env';

export async function GET() {
try {
Expand All @@ -15,7 +13,7 @@ export async function GET() {

const scripts: Record<string, string> = {};
for (const file of gptFiles) {
const script = await gptscript.parse(`${SCRIPTS_PATH()}/${file}`);
const script = await gpt().parse(`${SCRIPTS_PATH()}/${file}`);
let description = '';
for (let tool of script) {
if (tool.type === 'text') continue;
Expand Down
12 changes: 11 additions & 1 deletion config/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { GPTScript } from "@gptscript-ai/gptscript"

export const SCRIPTS_PATH = () => process.env.SCRIPTS_PATH || "gptscripts";
export const WORKSPACE_DIR = () => process.env.GPTSCRIPT_WORKSPACE_DIR || "";

export const set_WORKSPACE_DIR = (dir: string) => process.env.GPTSCRIPT_WORKSPACE_DIR = dir;
export const set_SCRIPTS_PATH = (dir: string) => process.env.SCRIPTS_PATH = dir;
export const set_SCRIPTS_PATH = (dir: string) => process.env.SCRIPTS_PATH = dir;

let gptscript: GPTScript | null = null;
export function gpt() {
if (!gptscript) {
gptscript = new GPTScript()
};
return gptscript;
}

0 comments on commit 776d6d0

Please sign in to comment.