-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathindex.ts
More file actions
390 lines (342 loc) · 11.4 KB
/
index.ts
File metadata and controls
390 lines (342 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { getStoredAuth } from '@/auth';
import { dim, dimItalic } from '@/utils/formatting';
import { getAvailablePipes } from '@/utils/get-available-pipes';
import { getAvailableTools } from '@/utils/get-available-tools';
import { heading } from '@/utils/heading';
import icons from '@/utils/icons';
import { isToolPresent } from '@/utils/is-tool-present';
import { formatCode } from '@/utils/ts-format-code';
import * as p from '@clack/prompts';
import slugify from '@sindresorhus/slugify';
import camelCase from 'camelcase';
import figures from 'figures';
import fs from 'fs';
import pMap from 'p-map';
import path from 'path';
import color from 'picocolors';
import type { Pipe } from 'types/pipe';
import type { PipeTool } from 'types/tools';
interface ErrorResponse {
error?: { message: string };
}
type Spinner = ReturnType<typeof p.spinner>;
/**
* Extracts the login name and name from a given pipe-separated string.
*
* @param loginAndPipe - login/pipe string to extract from.
* @returns An object containing the extracted `login` and `name`.
*/
function extractLoginName(loginAndPipe: string) {
if (!loginAndPipe) return { login: '', name: '' };
const split = loginAndPipe.split('/');
const length = split.length;
if (length < 2) return { login: '', name: '' };
return {
login: split[length - 2],
name: split[length - 1]
};
}
/**
* Represents an account with login credentials and an API key.
*/
interface Account {
login: string;
apiKey: string;
}
/**
* Retrieves the stored authentication account.
*
* This function attempts to retrieve the stored authentication account
* asynchronously. If the account is found, it is returned. If no account
* is found or an error occurs during retrieval, `null` is returned.
*
* @returns {Promise<Account | null>} A promise that resolves to the stored
* authentication account, or `null` if no account is found or an error occurs.
*/
async function retrieveAuthentication(): Promise<Account | null> {
try {
const account = await getStoredAuth();
if (!account) return null;
return account;
} catch (error) {
p.log.error(
`Error retrieving stored auth: ${(error as Error).message}`
);
return null;
}
}
/**
* Fetches a pipe from Langbase using the provided login and name.
*
* @param {Object} params - The parameters for fetching the pipe.
* @param {string} params.login - The login identifier.
* @param {string} params.name - The name of the pipe.
* @param {Spinner} params.spinner - The spinner instance for displaying loading status.
* @returns {Promise<Pipe | null>} - A promise that resolves to the fetched pipe or null if an error occurs.
*/
async function getPipe({
login,
name,
spinner
}: {
login: string;
name: string;
spinner: Spinner;
}) {
spinner.start('Fetching pipe from Langbase');
try {
const account = await retrieveAuthentication();
const API_URL = `https://api.langbase.com/v1/pipes/${login}/${name}`;
const createResponse = await fetch(API_URL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(account && { Authorization: `Bearer ${account.apiKey}` })
}
});
if (createResponse.ok) {
spinner.stop('Fetched pipe from Langbase');
return (await createResponse.json()) as Pipe;
}
const errorData = (await createResponse.json()) as ErrorResponse;
if (errorData) {
spinner.stop(
`Failed to fetch pipe from Langbase: ${errorData.error?.message}`
);
process.exit(1);
}
} catch (error: any) {
spinner.stop(error);
return null;
}
}
/**
* Asynchronously creates local tools based on the provided `Pipe` object.
*
* @param {Pipe} pipe - The pipe object containing tools to be created.
*
* The function performs the following steps:
* 1. Checks if there are any tools in the `pipe`. If none, it returns immediately.
* 2. Retrieves all available tools.
* 3. Iterates over each tool in the `pipe` and checks if the tool is already present.
* 4. If the tool is present, prompts the user to confirm if they want to overwrite the existing tool.
* 5. If the user chooses not to overwrite, the tool creation is skipped.
* 6. If the tool is not present or the user chooses to overwrite, the tool's code is generated and formatted.
* 7. The formatted code is then written to a file in the `baseai/tools` directory.
* 8. If any error occurs during the process, it is caught and an appropriate message is displayed.
*/
async function createLocalTool(pipe: Pipe) {
if (!pipe.tools.length) return;
const allTools = await getAvailableTools();
try {
await pMap(
pipe.tools,
async tool => {
const hasTool = isToolPresent({
name: tool.function.name,
allTools
});
if (hasTool) {
const toolInfo = await p.group(
{
overwrite: () =>
p.confirm({
message: `${color.dim(icons.tool)} Tool: ${color.cyan(tool.function.name)} already exists. Do you want to overwrite it?`,
initialValue: false
})
},
{
onCancel: () => {
p.cancel('Operation cancelled.');
process.exit(0);
}
}
);
if (!toolInfo.overwrite) {
p.outro(`Skipped …`);
return;
}
}
const name = tool.function.name;
const camelCaseName = camelCase(name);
const slugifiedName = slugify(name);
const code = `import { ToolI } from '@baseai/core';
export async function ${name}() {
// Your tool logic here
}
const ${camelCaseName}Tool = (): ToolI => ({
run: ${name}, // Name of the function to run
type: 'function' as const,
function: {
name: \`${name}\`,
description: \`${tool.function.description}\`,
parameters: ${JSON.stringify(tool.function.parameters || {}, null, 6)}
}
});
export default ${camelCaseName}Tool;`;
const formattedCode = await formatCode(code);
const baseDir = path.join(process.cwd(), 'baseai', 'tools');
const filePath = path.join(baseDir, `${slugifiedName}.ts`);
await fs.promises.mkdir(baseDir, { recursive: true });
await fs.promises.writeFile(filePath, formattedCode);
p.outro(`Tool created successfully at ${filePath}`);
},
{ concurrency: 1 }
);
} catch (error: any) {
p.cancel(`Error creating tool: ${error.message}`);
}
}
/**
* Transforms an array of `PipeTool` objects into an array of objects containing
* tool call strings, import paths, and tool file names.
*
* @param tools - An array of `PipeTool` objects to be transformed.
* @returns An array of objects, each containing:
* - `toolCall`: A string representing the tool call.
* - `importPath`: A string representing the import path for the tool.
* - `toolFileName`: A string representing the file name of the tool.
*/
function getToolsPipeData(tools: PipeTool[]) {
return tools.map(tool => {
const toolFileName = slugify(tool.function.name);
const toolName = `${camelCase(tool.function.name)}Tool`;
const importPath = `import ${toolName} from '../tools/${toolFileName}';`;
return {
toolCall: `${toolName}()`,
importPath,
toolFileName
};
});
}
/**
* Asynchronously creates a local pipe configuration file.
*
* This function performs the following steps:
* 1. Retrieves all available pipes.
* 2. Slugifies the provided pipe name.
* 3. Checks if a pipe with the same name already exists.
* 4. If the pipe exists, prompts the user to confirm overwriting the existing pipe.
* 5. If the user chooses not to overwrite, the operation is skipped.
* 6. Retrieves tool data for the provided pipe tools.
* 7. Constructs the pipe content with the provided pipe details and tool data.
* 8. Formats the generated pipe content.
* 9. Creates the necessary directories and writes the formatted pipe content to a file.
* 10. Outputs a success message upon successful creation of the pipe.
*
* @param {Pipe} pipe - The pipe object containing configuration details.
* @returns {Promise<void>} - A promise that resolves when the pipe is successfully created.
*/
async function createLocalPipe(pipe: Pipe) {
try {
const allPipes = await getAvailablePipes();
const pipeNameSlugified = slugify(pipe.name);
const hasPipe = allPipes.some(p => p === pipeNameSlugified);
if (hasPipe) {
const pipeInfo = await p.group(
{
overwrite: () =>
p.confirm({
message: `PIPE: ${color.cyan(pipe.name)} already exists. Do you want to overwrite it?`,
initialValue: false
})
},
{
onCancel: () => {
p.cancel('Operation cancelled.');
process.exit(0);
}
}
);
if (!pipeInfo.overwrite) {
p.outro(`Skipped …`);
return;
}
}
const toolData = await getToolsPipeData(pipe.tools);
const toolCalls = toolData.map(tool => tool.toolCall);
const pipeNameCamelCase = camelCase('pipe-' + pipe.name);
const messages = pipe.messages.map(message => ({
...(message.name && { name: message.name }),
role: message.role,
content: message.content
}));
const pipeContent = `import { PipeI } from '@baseai/core';
${toolData.map(tool => tool.importPath).join('\n')}
const ${pipeNameCamelCase} = (): PipeI => ({
// Prod only: Replace with your Langbase API key
// https://langbase.com/docs/api-reference/api-keys
apiKey: process.env.LANGBASE_API_KEY,
name: \`${pipe.name}\`,
description: \`${pipe.description}\`,
status: \`${pipe.status}\`,
model: \`${pipe.model}\`,
stream: ${pipe.stream},
json: ${pipe.json},
store: ${pipe.store},
moderate: ${pipe.moderate || true},
top_p: ${pipe.top_p},
max_tokens: ${pipe.max_tokens},
temperature: ${pipe.temperature},
presence_penalty: ${pipe.presence_penalty},
frequency_penalty: ${pipe.frequency_penalty},
stop: ${JSON.stringify(pipe.stop)},
tool_choice: ${JSON.stringify(pipe.tool_choice)},
parallel_tool_calls: ${pipe.parallel_tool_calls},
messages: ${JSON.stringify(messages || [])},
variables: ${JSON.stringify(pipe.variables)},
tools: [${toolCalls}],
memory: [],
});
export default ${pipeNameCamelCase};`;
const formattedCode = await formatCode(pipeContent);
const baseDir = path.join(process.cwd(), 'baseai', 'pipes');
const filePath = path.join(baseDir, `${pipeNameSlugified}.ts`);
await fs.promises.mkdir(baseDir, { recursive: true });
await fs.promises.writeFile(filePath, formattedCode);
p.outro(`Pipe created successfully at ${filePath}`);
p.outro(
heading({
text: pipeNameSlugified,
sub: `pipe added \n ${dim(figures.pointer)} ${dimItalic(` ${filePath}`)}`,
green: true
})
);
process.exit(0);
} catch (error: any) {
p.cancel(`Error creating pipe: ${error.message}`);
}
}
/**
* Adds a pipe by extracting login and name from the provided pipe information,
* fetching the pipe details, and creating local tool and pipe.
*
* @param {Object} params - The parameters for the function.
* @param {string} params.pipeInfo - The pipe information string.
* @returns {Promise<void>} - A promise that resolves when the pipe is added.
*/
export async function addPipe({ loginAndPipe }: { loginAndPipe: string }) {
p.intro(
heading({
text: 'PIPE',
sub: `Adding ${color.cyan(loginAndPipe)}`
})
);
const { login, name } = extractLoginName(loginAndPipe);
if (!login || !name) {
p.log.error('Invalid pipe information provided');
return;
}
const spinner = p.spinner();
try {
const pipe = await getPipe({ login, name, spinner });
if (!pipe) return;
await createLocalTool(pipe);
await createLocalPipe(pipe);
} catch (error: any) {
spinner.stop('An unexpected error occurred');
p.log.error(
`'An unexpected error occurred': ${(error as Error).message}`
);
}
}