Skip to content

Commit 7a80649

Browse files
authored
πŸ“¦ NEW: Request production AI agent pipe (#123)
* πŸ“¦ NEW: Request prod pipe * πŸ‘Œ IMPROVE: Code
1 parent fd58f2e commit 7a80649

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

β€Žpackages/core/src/pipes/pipes.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface RunOptions {
2222
}
2323

2424
export interface RunOptionsStream extends RunOptions {
25-
stream: true;
25+
stream: boolean;
2626
}
2727

2828
export interface Usage {
@@ -56,6 +56,7 @@ export interface RunResponseStream {
5656

5757
export interface PipeOptions extends PipeI {
5858
maxCalls?: number;
59+
prod?: boolean;
5960
}
6061

6162
interface ChoiceGenerate {
@@ -80,11 +81,16 @@ export class Pipe {
8081
private tools: Record<string, (...args: any[]) => Promise<any>>;
8182
private maxCalls: number;
8283
private hasTools: boolean;
84+
private prod: boolean;
8385

8486
constructor(options: PipeOptions) {
85-
const baseUrl = getApiUrl();
87+
this.prod = options.prod ?? isProd();
88+
const baseUrl = getApiUrl(this.prod);
89+
8690
this.request = new Request({apiKey: options.apiKey, baseUrl});
8791
this.pipe = options;
92+
93+
delete this.pipe.prod;
8894
delete this.pipe.apiKey;
8995

9096
this.tools = this.getToolsFromPipe(this.pipe);
@@ -136,7 +142,7 @@ export class Pipe {
136142
responseMessage: Message,
137143
toolResults: Message[],
138144
): Message[] {
139-
return isProd()
145+
return this.prod
140146
? toolResults
141147
: [...messages, responseMessage, ...toolResults];
142148
}
@@ -260,6 +266,10 @@ export class Pipe {
260266
}
261267

262268
if (!runTools) {
269+
if (!stream) {
270+
return response as RunResponse;
271+
}
272+
263273
return response as RunResponseStream;
264274
}
265275

@@ -347,7 +357,8 @@ export class Pipe {
347357
},
348358
};
349359

350-
const isProdEnv = isProd();
360+
const isProdEnv = this.prod;
361+
351362
if (!isProdEnv) {
352363
const isServerRunning = await isLocalServerRunning();
353364
if (!isServerRunning) return {} as T;

β€Žpackages/core/src/utils/is-prod.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ export function isLocal() {
1010
return process.env.NODE_ENV !== 'production';
1111
}
1212

13-
export function getApiUrl() {
13+
export function getApiUrl(prod?: boolean) {
14+
if (prod) return 'https://api.langbase.com';
15+
else return 'http://localhost:9000';
16+
1417
// TODO: Make local port configurable.
15-
return isProd() ? 'https://api.langbase.com' : 'http://localhost:9000';
18+
// return isProd() ? 'https://api.langbase.com' : 'http://localhost:9000';
1619
// return isProd() ? 'http://localhost:8787' : 'http://localhost:9000';
1720
}

β€Žpackages/core/src/utils/local-server-running.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {getApiUrl} from './is-prod';
1+
import {getApiUrl, isProd} from './is-prod';
22

33
export async function isLocalServerRunning(): Promise<Boolean> {
44
try {
5-
const endpoint = getApiUrl();
5+
const prod = isProd();
6+
const endpoint = getApiUrl(prod);
67

78
const response = await fetch(endpoint, {
89
mode: 'no-cors',

0 commit comments

Comments
Β (0)