11import { Request } from '../common/request' ;
2- import {
3- Pipe as PipeBaseAI ,
4- RunOptions as RunOptionsT ,
5- RunOptionsStream as RunOptionsStreamT ,
6- RunResponse ,
7- RunResponseStream ,
8- } from '@baseai/core' ;
92
103export type Role = 'user' | 'assistant' | 'system' | 'tool' ;
114
12- // Base types without name and apiKey
13- type BaseRunOptions = Omit < RunOptionsT , 'name' | 'apiKey' > & {
5+ export interface RunOptionsBase {
6+ messages ?: Message [ ] ;
7+ variables ?: Variable [ ] ;
8+ threadId ?: string ;
9+ rawResponse ?: boolean ;
10+ runTools ?: boolean ;
11+ tools ?: Tools [ ] ;
12+ name ?: string ; // Pipe name for SDK,
13+ apiKey ?: string ; // pipe level key for SDK
14+ llmKey ?: string ; // LLM API key
15+ }
16+
17+ export interface RunOptionsT extends RunOptionsBase {
18+ stream ?: false ;
19+ }
20+
21+ export interface RunOptionsStreamT extends RunOptionsBase {
22+ stream : true ;
23+ }
24+
25+ interface ChoiceGenerate {
26+ index : number ;
27+ message : Message ;
28+ logprobs : boolean | null ;
29+ finish_reason : string ;
30+ }
31+
32+ export interface Usage {
33+ prompt_tokens : number ;
34+ completion_tokens : number ;
35+ total_tokens : number ;
36+ }
37+
38+ export interface RunResponse {
39+ completion : string ;
40+ threadId ?: string ;
41+ id : string ;
42+ object : string ;
43+ created : number ;
44+ model : string ;
45+ choices : ChoiceGenerate [ ] ;
46+ usage : Usage ;
47+ system_fingerprint : string | null ;
48+ rawResponse ?: {
49+ headers : Record < string , string > ;
50+ } ;
1451 messages : Message [ ] ;
1552 llmKey ?: string ;
16- } ;
53+ name ?: string ;
54+ }
55+
56+ export interface RunResponseStream {
57+ stream : ReadableStream < any > ;
58+ threadId : string | null ;
59+ rawResponse ?: {
60+ headers : Record < string , string > ;
61+ } ;
62+ }
1763
1864// Union type for RunOptions
1965export type RunOptions =
20- | ( BaseRunOptions & { name : string ; apiKey ?: never } )
21- | ( BaseRunOptions & { name ?: never ; apiKey : string } ) ;
22-
23- // Similar structure for RunOptionsStream
24- type BaseRunOptionsStream = Omit < RunOptionsStreamT , 'name' | 'apiKey' > & {
25- messages : Message [ ] ;
26- llmKey ?: string ;
27- } ;
66+ | ( RunOptionsT & { name : string ; apiKey ?: never } )
67+ | ( RunOptionsT & { name ?: never ; apiKey : string } ) ;
2868
2969export type RunOptionsStream =
30- | ( BaseRunOptionsStream & { name : string ; apiKey ?: never } )
31- | ( BaseRunOptionsStream & { name ?: never ; apiKey : string } ) ;
70+ | ( RunOptionsStreamT & { name : string ; apiKey ?: never } )
71+ | ( RunOptionsStreamT & { name ?: never ; apiKey : string } ) ;
3272
3373export interface Function {
3474 name : string ;
@@ -59,6 +99,15 @@ interface ToolChoice {
5999 function : { name : string } ;
60100}
61101
102+ interface Tools {
103+ type : 'function' ;
104+ function : {
105+ name : string ;
106+ description ?: string ;
107+ parameters ?: Record < string , any > ;
108+ } ;
109+ }
110+
62111interface PipeBaseOptions {
63112 name : string ;
64113 description ?: string ;
@@ -75,14 +124,7 @@ interface PipeBaseOptions {
75124 presence_penalty ?: number ;
76125 frequency_penalty ?: number ;
77126 stop ?: string [ ] ;
78- tools ?: {
79- type : 'function' ;
80- function : {
81- name : string ;
82- description ?: string ;
83- parameters ?: Record < string , any > ;
84- } ;
85- } [ ] ;
127+ tools ?: Tools [ ] ;
86128 tool_choice ?: 'auto' | 'required' | ToolChoice ;
87129 parallel_tool_calls ?: boolean ;
88130 messages ?: Message [ ] ;
@@ -113,16 +155,7 @@ export interface PipeListResponse {
113155 parallel_tool_calls : boolean ;
114156 messages : Message [ ] ;
115157 variables : Variable [ ] | [ ] ;
116- tools :
117- | {
118- type : 'function' ;
119- function : {
120- name : string ;
121- description ?: string ;
122- parameters ?: Record < string , any > ;
123- } ;
124- } [ ]
125- | [ ] ;
158+ tools : Tools [ ] | [ ] ;
126159 memory :
127160 | {
128161 name : string ;
@@ -373,16 +406,20 @@ export class Langbase {
373406 ) ;
374407 }
375408
376- const pipe = new PipeBaseAI ( {
377- apiKey : options . apiKey ?? this . apiKey ,
378- name : options . name ?. trim ( ) || '' , // Pipe name
379- prod : true ,
380- // default values
381- model : 'openai:gpt-4o-mini' ,
382- tools : [ ] ,
383- } as any ) ;
409+ // Remove stream property if it's not set to true
410+ if ( typeof options . stream === 'undefined' ) {
411+ delete options . stream ;
412+ }
384413
385- return await pipe . run ( { ...options , runTools : false } ) ;
414+ return this . request . post ( {
415+ endpoint : '/v1/pipes/run' ,
416+ body : options ,
417+ headers : {
418+ ...( options . llmKey && {
419+ 'LB-LLM-KEY' : options . llmKey ,
420+ } ) ,
421+ } ,
422+ } ) ;
386423 }
387424
388425 /**
0 commit comments