Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit f04386c

Browse files
committed
refactor: move logic to getPromptWithQuery
1 parent 15c1428 commit f04386c

File tree

1 file changed

+55
-62
lines changed

1 file changed

+55
-62
lines changed

src/api.ts

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,11 @@ export class API {
486486
* @returns The data part of the response from the GraphQL endpoint.
487487
* @throws Will throw an error if the GraphQL call returns errors or if the request fails.
488488
*/
489-
private async makeGqlCall(query: string, variables: any) {
489+
private async makeGqlCall(
490+
query: string,
491+
variables: any,
492+
timeout: number = 10000
493+
) {
490494
try {
491495
const response = await axios({
492496
url: this.graphqlEndpoint,
@@ -495,7 +499,8 @@ export class API {
495499
data: {
496500
query: query,
497501
variables: variables
498-
}
502+
},
503+
timeout
499504
});
500505
if (response.data.errors) {
501506
throw new Error(JSON.stringify(response.data.errors));
@@ -2198,13 +2203,9 @@ export class API {
21982203

21992204
/**
22002205
* Retrieves a prompt by its id. If the request fails, it will try to get the prompt from the cache.
2201-
*
2202-
* @param id ID of the prompt to retrieve.
2203-
* @returns The prompt with given ID.
22042206
*/
22052207
public async getPromptById(id: string) {
2206-
try {
2207-
const query = `
2208+
const query = `
22082209
query GetPrompt($id: String!) {
22092210
promptVersion(id: $id) {
22102211
createdAt
@@ -2226,16 +2227,38 @@ export class API {
22262227
}
22272228
}
22282229
}
2229-
`;
2230+
`;
2231+
2232+
return this.getPromptWithQuery(query, { id });
2233+
}
2234+
2235+
/**
2236+
* Private helper method to execute prompt queries with error handling and caching
2237+
*/
2238+
private async getPromptWithQuery(
2239+
query: string,
2240+
variables: Record<string, any>
2241+
) {
2242+
try {
2243+
const result = await this.makeGqlCall(query, variables);
22302244

2231-
const prompt = await this.getPromptWithQuery(query, { id });
2232-
if (prompt) {
2233-
await this.promptCache.put(prompt);
2234-
return prompt;
2245+
if (!result.data || !result.data.promptVersion) {
2246+
return null;
22352247
}
2236-
return null;
2248+
2249+
const promptData = result.data.promptVersion;
2250+
promptData.provider = promptData.settings?.provider;
2251+
promptData.name = promptData.lineage?.name;
2252+
delete promptData.lineage;
2253+
if (promptData.settings) {
2254+
delete promptData.settings.provider;
2255+
}
2256+
2257+
const prompt = new Prompt(this, promptData);
2258+
await this.promptCache.put(prompt);
2259+
return prompt;
22372260
} catch (error) {
2238-
return await this.promptCache.get({ id });
2261+
return await this.promptCache.get(variables);
22392262
}
22402263
}
22412264

@@ -2247,57 +2270,27 @@ export class API {
22472270
* @returns An instance of `Prompt` containing the prompt data, or `null` if not found.
22482271
*/
22492272
public async getPrompt(name: string, version?: number) {
2250-
try {
2251-
const query = `
2252-
query GetPrompt($name: String!, $version: Int) {
2253-
promptVersion(name: $name, version: $version) {
2254-
id
2255-
createdAt
2256-
updatedAt
2257-
type
2258-
templateMessages
2259-
tools
2260-
settings
2261-
variables
2262-
variablesDefaultValues
2263-
version
2264-
url
2265-
lineage {
2266-
name
2267-
}
2273+
const query = `
2274+
query GetPrompt($name: String!, $version: Int) {
2275+
promptVersion(name: $name, version: $version) {
2276+
id
2277+
createdAt
2278+
updatedAt
2279+
type
2280+
templateMessages
2281+
tools
2282+
settings
2283+
variables
2284+
variablesDefaultValues
2285+
version
2286+
url
2287+
lineage {
2288+
name
22682289
}
22692290
}
2270-
`;
2271-
const prompt = await this.getPromptWithQuery(query, { name, version });
2272-
if (prompt) {
2273-
await this.promptCache.put(prompt);
2274-
return prompt;
2275-
}
2276-
return null;
2277-
} catch (error) {
2278-
return await this.promptCache.get({ name, version });
22792291
}
2280-
}
2281-
2282-
private async getPromptWithQuery(
2283-
query: string,
2284-
variables: Record<string, any>
2285-
) {
2286-
const result = await this.makeGqlCall(query, variables);
2287-
2288-
if (!result.data || !result.data.promptVersion) {
2289-
return null;
2290-
}
2291-
2292-
const promptData = result.data.promptVersion;
2293-
promptData.provider = promptData.settings?.provider;
2294-
promptData.name = promptData.lineage?.name;
2295-
delete promptData.lineage;
2296-
if (promptData.settings) {
2297-
delete promptData.settings.provider;
2298-
}
2299-
2300-
return new Prompt(this, promptData);
2292+
`;
2293+
return this.getPromptWithQuery(query, { name, version });
23012294
}
23022295

23032296
/**

0 commit comments

Comments
 (0)