@promptbook/node 0.110.0-5 → 0.110.0-7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.es.js +1461 -920
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/openai.index.d.ts +8 -0
- package/esm/typings/src/_packages/types.index.d.ts +4 -0
- package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentsDatabaseSchema.d.ts +0 -3
- package/esm/typings/src/execution/LlmExecutionTools.d.ts +2 -1
- package/esm/typings/src/llm-providers/agent/Agent.d.ts +1 -0
- package/esm/typings/src/llm-providers/agent/AgentLlmExecutionTools.d.ts +5 -0
- package/esm/typings/src/llm-providers/agent/AgentOptions.d.ts +4 -3
- package/esm/typings/src/llm-providers/agent/CreateAgentLlmExecutionToolsOptions.d.ts +7 -5
- package/esm/typings/src/llm-providers/agent/RemoteAgent.d.ts +2 -1
- package/esm/typings/src/llm-providers/openai/OpenAiAgentKitExecutionTools.d.ts +111 -0
- package/esm/typings/src/llm-providers/openai/OpenAiAgentKitExecutionToolsOptions.d.ts +15 -0
- package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +3 -42
- package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionToolsOptions.d.ts +2 -33
- package/esm/typings/src/llm-providers/openai/OpenAiVectorStoreHandler.d.ts +135 -0
- package/esm/typings/src/llm-providers/openai/utils/mapToolsToOpenAi.d.ts +1 -1
- package/esm/typings/src/version.d.ts +1 -1
- package/package.json +7 -3
- package/umd/index.umd.js +1464 -924
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -17,6 +17,7 @@ import { Subject, BehaviorSubject } from 'rxjs';
|
|
|
17
17
|
import moment from 'moment';
|
|
18
18
|
import { lookup, extension } from 'mime-types';
|
|
19
19
|
import { parse, unparse } from 'papaparse';
|
|
20
|
+
import { Agent as Agent$1, setDefaultOpenAIClient, setDefaultOpenAIKey, fileSearchTool, tool, run } from '@openai/agents';
|
|
20
21
|
import Bottleneck from 'bottleneck';
|
|
21
22
|
import OpenAI from 'openai';
|
|
22
23
|
|
|
@@ -34,7 +35,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
|
|
|
34
35
|
* @generated
|
|
35
36
|
* @see https://github.com/webgptorg/promptbook
|
|
36
37
|
*/
|
|
37
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.110.0-
|
|
38
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.110.0-7';
|
|
38
39
|
/**
|
|
39
40
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
40
41
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -22358,16 +22359,11 @@ class OpenAiCompatibleExecutionTools {
|
|
|
22358
22359
|
const openAiOptions = { ...this.options };
|
|
22359
22360
|
delete openAiOptions.isVerbose;
|
|
22360
22361
|
delete openAiOptions.userId;
|
|
22361
|
-
// Enhanced configuration
|
|
22362
|
+
// Enhanced configuration with retries and timeouts.
|
|
22362
22363
|
const enhancedOptions = {
|
|
22363
22364
|
...openAiOptions,
|
|
22364
22365
|
timeout: API_REQUEST_TIMEOUT,
|
|
22365
22366
|
maxRetries: CONNECTION_RETRIES_LIMIT,
|
|
22366
|
-
defaultHeaders: {
|
|
22367
|
-
Connection: 'keep-alive',
|
|
22368
|
-
'Keep-Alive': 'timeout=30, max=100',
|
|
22369
|
-
...openAiOptions.defaultHeaders,
|
|
22370
|
-
},
|
|
22371
22367
|
};
|
|
22372
22368
|
this.client = new OpenAI(enhancedOptions);
|
|
22373
22369
|
}
|
|
@@ -23290,644 +23286,197 @@ class OpenAiExecutionTools extends OpenAiCompatibleExecutionTools {
|
|
|
23290
23286
|
}
|
|
23291
23287
|
}
|
|
23292
23288
|
|
|
23293
|
-
/**
|
|
23294
|
-
* Uploads files to OpenAI and returns their IDs
|
|
23295
|
-
*
|
|
23296
|
-
* @private utility for `OpenAiAssistantExecutionTools` and `OpenAiCompatibleExecutionTools`
|
|
23297
|
-
*/
|
|
23298
|
-
async function uploadFilesToOpenAi(client, files) {
|
|
23299
|
-
const fileIds = [];
|
|
23300
|
-
for (const file of files) {
|
|
23301
|
-
// Note: OpenAI API expects a File object or a ReadStream
|
|
23302
|
-
// In browser environment, we can pass the File object directly
|
|
23303
|
-
// In Node.js environment, we might need to convert it or use a different approach
|
|
23304
|
-
// But since `Prompt.files` already contains `File` objects, we try to pass them directly
|
|
23305
|
-
const uploadedFile = await client.files.create({
|
|
23306
|
-
file: file,
|
|
23307
|
-
purpose: 'assistants',
|
|
23308
|
-
});
|
|
23309
|
-
fileIds.push(uploadedFile.id);
|
|
23310
|
-
}
|
|
23311
|
-
return fileIds;
|
|
23312
|
-
}
|
|
23313
|
-
|
|
23314
23289
|
const DEFAULT_KNOWLEDGE_SOURCE_DOWNLOAD_TIMEOUT_MS = 30000;
|
|
23315
23290
|
const DEFAULT_KNOWLEDGE_SOURCE_UPLOAD_TIMEOUT_MS = 900000;
|
|
23316
23291
|
const VECTOR_STORE_PROGRESS_LOG_INTERVAL_MIN_MS = 15000;
|
|
23317
23292
|
const VECTOR_STORE_STALL_LOG_THRESHOLD_MS = 30000;
|
|
23318
23293
|
/**
|
|
23319
|
-
*
|
|
23320
|
-
*
|
|
23321
|
-
* This is useful for calling OpenAI API with a single assistant, for more wide usage use `OpenAiExecutionTools`.
|
|
23322
|
-
*
|
|
23323
|
-
* Note: [🦖] There are several different things in Promptbook:
|
|
23324
|
-
* - `Agent` - which represents an AI Agent with its source, memories, actions, etc. Agent is a higher-level abstraction which is internally using:
|
|
23325
|
-
* - `LlmExecutionTools` - which wraps one or more LLM models and provides an interface to execute them
|
|
23326
|
-
* - `AgentLlmExecutionTools` - which is a specific implementation of `LlmExecutionTools` that wraps another LlmExecutionTools and applies agent-specific system prompts and requirements
|
|
23327
|
-
* - `OpenAiAssistantExecutionTools` - which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities, recommended for usage in `Agent` or `AgentLlmExecutionTools`
|
|
23328
|
-
* - `RemoteAgent` - which is an `Agent` that connects to a Promptbook Agents Server
|
|
23294
|
+
* Base class for OpenAI execution tools that need hosted vector stores.
|
|
23329
23295
|
*
|
|
23330
23296
|
* @public exported from `@promptbook/openai`
|
|
23331
23297
|
*/
|
|
23332
|
-
class
|
|
23298
|
+
class OpenAiVectorStoreHandler extends OpenAiExecutionTools {
|
|
23333
23299
|
/**
|
|
23334
|
-
*
|
|
23335
|
-
*
|
|
23336
|
-
* @param options which are relevant are directly passed to the OpenAI client
|
|
23300
|
+
* Returns the per-knowledge-source download timeout in milliseconds.
|
|
23337
23301
|
*/
|
|
23338
|
-
|
|
23302
|
+
getKnowledgeSourceDownloadTimeoutMs() {
|
|
23339
23303
|
var _a;
|
|
23340
|
-
|
|
23341
|
-
throw new NotYetImplementedError(`Proxy mode is not yet implemented for OpenAI assistants`);
|
|
23342
|
-
}
|
|
23343
|
-
super(options);
|
|
23344
|
-
this.isCreatingNewAssistantsAllowed = false;
|
|
23345
|
-
this.assistantId = options.assistantId;
|
|
23346
|
-
this.isCreatingNewAssistantsAllowed = (_a = options.isCreatingNewAssistantsAllowed) !== null && _a !== void 0 ? _a : false;
|
|
23347
|
-
if (this.assistantId === null && !this.isCreatingNewAssistantsAllowed) {
|
|
23348
|
-
throw new NotAllowed(`Assistant ID is null and creating new assistants is not allowed - this configuration does not make sense`);
|
|
23349
|
-
}
|
|
23350
|
-
// <- TODO: !!! `OpenAiAssistantExecutionToolsOptions` - Allow `assistantId: null` together with `isCreatingNewAssistantsAllowed: true`
|
|
23351
|
-
// TODO: [👱] Make limiter same as in `OpenAiExecutionTools`
|
|
23304
|
+
return (_a = this.vectorStoreOptions.knowledgeSourceDownloadTimeoutMs) !== null && _a !== void 0 ? _a : DEFAULT_KNOWLEDGE_SOURCE_DOWNLOAD_TIMEOUT_MS;
|
|
23352
23305
|
}
|
|
23353
|
-
|
|
23354
|
-
|
|
23306
|
+
/**
|
|
23307
|
+
* Returns the max concurrency for knowledge source uploads.
|
|
23308
|
+
*/
|
|
23309
|
+
getKnowledgeSourceUploadMaxConcurrency() {
|
|
23310
|
+
var _a;
|
|
23311
|
+
return (_a = this.vectorStoreOptions.knowledgeSourceUploadMaxConcurrency) !== null && _a !== void 0 ? _a : 5;
|
|
23355
23312
|
}
|
|
23356
|
-
|
|
23357
|
-
|
|
23313
|
+
/**
|
|
23314
|
+
* Returns the polling interval in milliseconds for vector store uploads.
|
|
23315
|
+
*/
|
|
23316
|
+
getKnowledgeSourceUploadPollIntervalMs() {
|
|
23317
|
+
var _a;
|
|
23318
|
+
return (_a = this.vectorStoreOptions.knowledgeSourceUploadPollIntervalMs) !== null && _a !== void 0 ? _a : 5000;
|
|
23358
23319
|
}
|
|
23359
23320
|
/**
|
|
23360
|
-
*
|
|
23321
|
+
* Returns the overall upload timeout in milliseconds for vector store uploads.
|
|
23361
23322
|
*/
|
|
23362
|
-
|
|
23363
|
-
|
|
23323
|
+
getKnowledgeSourceUploadTimeoutMs() {
|
|
23324
|
+
var _a;
|
|
23325
|
+
return (_a = this.vectorStoreOptions.knowledgeSourceUploadTimeoutMs) !== null && _a !== void 0 ? _a : DEFAULT_KNOWLEDGE_SOURCE_UPLOAD_TIMEOUT_MS;
|
|
23364
23326
|
}
|
|
23365
23327
|
/**
|
|
23366
|
-
*
|
|
23328
|
+
* Returns true if we should continue even if vector store ingestion stalls.
|
|
23367
23329
|
*/
|
|
23368
|
-
|
|
23369
|
-
var _a
|
|
23370
|
-
|
|
23371
|
-
|
|
23330
|
+
shouldContinueOnVectorStoreStall() {
|
|
23331
|
+
var _a;
|
|
23332
|
+
return (_a = this.vectorStoreOptions.shouldContinueOnVectorStoreStall) !== null && _a !== void 0 ? _a : true;
|
|
23333
|
+
}
|
|
23334
|
+
/**
|
|
23335
|
+
* Returns vector-store-specific options with extended settings.
|
|
23336
|
+
*/
|
|
23337
|
+
get vectorStoreOptions() {
|
|
23338
|
+
return this.options;
|
|
23339
|
+
}
|
|
23340
|
+
/**
|
|
23341
|
+
* Returns the OpenAI vector stores API surface, supporting stable and beta SDKs.
|
|
23342
|
+
*/
|
|
23343
|
+
getVectorStoresApi(client) {
|
|
23344
|
+
var _a, _b;
|
|
23345
|
+
const vectorStores = (_a = client.vectorStores) !== null && _a !== void 0 ? _a : (_b = client.beta) === null || _b === void 0 ? void 0 : _b.vectorStores;
|
|
23346
|
+
if (!vectorStores) {
|
|
23347
|
+
throw new Error('OpenAI client does not support vector stores. Please ensure you are using a compatible version of the OpenAI SDK with vector store support.');
|
|
23372
23348
|
}
|
|
23373
|
-
|
|
23374
|
-
|
|
23375
|
-
|
|
23376
|
-
|
|
23377
|
-
|
|
23349
|
+
return vectorStores;
|
|
23350
|
+
}
|
|
23351
|
+
/**
|
|
23352
|
+
* Downloads a knowledge source URL into a File for vector store upload.
|
|
23353
|
+
*/
|
|
23354
|
+
async downloadKnowledgeSourceFile(options) {
|
|
23355
|
+
var _a;
|
|
23356
|
+
const { source, timeoutMs, logLabel } = options;
|
|
23357
|
+
const startedAtMs = Date.now();
|
|
23358
|
+
const controller = new AbortController();
|
|
23359
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
23360
|
+
if (this.options.isVerbose) {
|
|
23361
|
+
console.info('[🤰]', 'Downloading knowledge source', {
|
|
23362
|
+
source,
|
|
23363
|
+
timeoutMs,
|
|
23364
|
+
logLabel,
|
|
23365
|
+
});
|
|
23378
23366
|
}
|
|
23379
|
-
|
|
23380
|
-
|
|
23381
|
-
|
|
23382
|
-
|
|
23367
|
+
try {
|
|
23368
|
+
const response = await fetch(source, { signal: controller.signal });
|
|
23369
|
+
const contentType = (_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : undefined;
|
|
23370
|
+
if (!response.ok) {
|
|
23371
|
+
console.error('[🤰]', 'Failed to download knowledge source', {
|
|
23372
|
+
source,
|
|
23373
|
+
status: response.status,
|
|
23374
|
+
statusText: response.statusText,
|
|
23375
|
+
contentType,
|
|
23376
|
+
elapsedMs: Date.now() - startedAtMs,
|
|
23377
|
+
logLabel,
|
|
23378
|
+
});
|
|
23379
|
+
return null;
|
|
23380
|
+
}
|
|
23381
|
+
const buffer = await response.arrayBuffer();
|
|
23382
|
+
let filename = source.split('/').pop() || 'downloaded-file';
|
|
23383
|
+
try {
|
|
23384
|
+
const url = new URL(source);
|
|
23385
|
+
filename = url.pathname.split('/').pop() || filename;
|
|
23386
|
+
}
|
|
23387
|
+
catch (error) {
|
|
23388
|
+
// Keep default filename
|
|
23389
|
+
}
|
|
23390
|
+
const file = new File([buffer], filename, contentType ? { type: contentType } : undefined);
|
|
23391
|
+
const elapsedMs = Date.now() - startedAtMs;
|
|
23392
|
+
const sizeBytes = buffer.byteLength;
|
|
23393
|
+
if (this.options.isVerbose) {
|
|
23394
|
+
console.info('[🤰]', 'Downloaded knowledge source', {
|
|
23395
|
+
source,
|
|
23396
|
+
filename,
|
|
23397
|
+
sizeBytes,
|
|
23398
|
+
contentType,
|
|
23399
|
+
elapsedMs,
|
|
23400
|
+
logLabel,
|
|
23401
|
+
});
|
|
23383
23402
|
}
|
|
23403
|
+
return { file, sizeBytes, filename, elapsedMs };
|
|
23384
23404
|
}
|
|
23385
|
-
|
|
23386
|
-
|
|
23387
|
-
|
|
23388
|
-
|
|
23389
|
-
|
|
23390
|
-
|
|
23391
|
-
|
|
23392
|
-
|
|
23393
|
-
|
|
23394
|
-
// <- Note: [🧆]
|
|
23395
|
-
} as OpenAI.Chat.Completions.CompletionCreateParamsNonStreaming; // <- TODO: Guard here types better
|
|
23396
|
-
|
|
23397
|
-
if (format === 'JSON') {
|
|
23398
|
-
modelSettings.response_format = {
|
|
23399
|
-
type: 'json_object',
|
|
23400
|
-
};
|
|
23405
|
+
catch (error) {
|
|
23406
|
+
assertsError(error);
|
|
23407
|
+
console.error('[🤰]', 'Error downloading knowledge source', {
|
|
23408
|
+
source,
|
|
23409
|
+
elapsedMs: Date.now() - startedAtMs,
|
|
23410
|
+
logLabel,
|
|
23411
|
+
error: serializeError(error),
|
|
23412
|
+
});
|
|
23413
|
+
return null;
|
|
23401
23414
|
}
|
|
23402
|
-
|
|
23403
|
-
|
|
23404
|
-
// > 'response_format' of type 'json_object' is not supported with this model.
|
|
23405
|
-
const rawPromptContent = templateParameters(content, {
|
|
23406
|
-
...parameters,
|
|
23407
|
-
modelName: 'assistant',
|
|
23408
|
-
// <- [🧠] What is the best value here
|
|
23409
|
-
});
|
|
23410
|
-
// Build thread messages: include previous thread messages + current user message
|
|
23411
|
-
const threadMessages = [];
|
|
23412
|
-
// TODO: [🈹] Maybe this should not be here but in other place, look at commit 39d705e75e5bcf7a818c3af36bc13e1c8475c30c
|
|
23413
|
-
// Add previous messages from thread (if any)
|
|
23414
|
-
if ('thread' in prompt && Array.isArray(prompt.thread)) {
|
|
23415
|
-
const previousMessages = prompt.thread.map((msg) => ({
|
|
23416
|
-
role: (msg.sender === 'assistant' ? 'assistant' : 'user'),
|
|
23417
|
-
content: msg.content,
|
|
23418
|
-
}));
|
|
23419
|
-
threadMessages.push(...previousMessages);
|
|
23415
|
+
finally {
|
|
23416
|
+
clearTimeout(timeoutId);
|
|
23420
23417
|
}
|
|
23421
|
-
|
|
23422
|
-
|
|
23423
|
-
|
|
23424
|
-
|
|
23425
|
-
|
|
23426
|
-
|
|
23427
|
-
|
|
23428
|
-
|
|
23429
|
-
|
|
23430
|
-
tools: [{ type: 'file_search' }, { type: 'code_interpreter' }],
|
|
23431
|
-
}));
|
|
23418
|
+
}
|
|
23419
|
+
/**
|
|
23420
|
+
* Logs vector store file batch diagnostics to help trace ingestion stalls or failures.
|
|
23421
|
+
*/
|
|
23422
|
+
async logVectorStoreFileBatchDiagnostics(options) {
|
|
23423
|
+
var _a, _b, _c, _d, _e;
|
|
23424
|
+
const { client, vectorStoreId, batchId, uploadedFiles, logLabel, reason } = options;
|
|
23425
|
+
if (reason === 'stalled' && !this.options.isVerbose) {
|
|
23426
|
+
return;
|
|
23432
23427
|
}
|
|
23433
|
-
|
|
23434
|
-
|
|
23435
|
-
|
|
23436
|
-
|
|
23437
|
-
|
|
23438
|
-
|
|
23439
|
-
// because streaming doesn't support tool execution flow properly
|
|
23440
|
-
if (hasTools) {
|
|
23441
|
-
onProgress({
|
|
23442
|
-
content: '',
|
|
23443
|
-
modelName: 'assistant',
|
|
23444
|
-
timing: { start, complete: $getCurrentDate() },
|
|
23445
|
-
usage: UNCERTAIN_USAGE,
|
|
23446
|
-
rawPromptContent,
|
|
23447
|
-
rawRequest: null,
|
|
23448
|
-
rawResponse: null,
|
|
23428
|
+
if (!batchId.startsWith('vsfb_')) {
|
|
23429
|
+
console.error('[🤰]', 'Vector store file batch diagnostics skipped (invalid batch id)', {
|
|
23430
|
+
vectorStoreId,
|
|
23431
|
+
batchId,
|
|
23432
|
+
reason,
|
|
23433
|
+
logLabel,
|
|
23449
23434
|
});
|
|
23450
|
-
|
|
23451
|
-
|
|
23452
|
-
|
|
23453
|
-
|
|
23454
|
-
|
|
23455
|
-
|
|
23456
|
-
|
|
23457
|
-
|
|
23458
|
-
|
|
23459
|
-
|
|
23460
|
-
|
|
23461
|
-
|
|
23462
|
-
|
|
23463
|
-
const
|
|
23464
|
-
const
|
|
23465
|
-
|
|
23466
|
-
|
|
23467
|
-
|
|
23468
|
-
|
|
23469
|
-
const toolCalls = run.required_action.submit_tool_outputs.tool_calls;
|
|
23470
|
-
const toolOutputs = [];
|
|
23471
|
-
for (const toolCall of toolCalls) {
|
|
23472
|
-
if (toolCall.type === 'function') {
|
|
23473
|
-
const functionName = toolCall.function.name;
|
|
23474
|
-
const functionArgs = JSON.parse(toolCall.function.arguments);
|
|
23475
|
-
const calledAt = $getCurrentDate();
|
|
23476
|
-
if (toolCall.id) {
|
|
23477
|
-
toolCallStartedAt.set(toolCall.id, calledAt);
|
|
23478
|
-
}
|
|
23479
|
-
onProgress({
|
|
23480
|
-
content: '',
|
|
23481
|
-
modelName: 'assistant',
|
|
23482
|
-
timing: { start, complete: $getCurrentDate() },
|
|
23483
|
-
usage: UNCERTAIN_USAGE,
|
|
23484
|
-
rawPromptContent,
|
|
23485
|
-
rawRequest: null,
|
|
23486
|
-
rawResponse: null,
|
|
23487
|
-
toolCalls: [
|
|
23488
|
-
{
|
|
23489
|
-
name: functionName,
|
|
23490
|
-
arguments: toolCall.function.arguments,
|
|
23491
|
-
result: '',
|
|
23492
|
-
rawToolCall: toolCall,
|
|
23493
|
-
createdAt: calledAt,
|
|
23494
|
-
},
|
|
23495
|
-
],
|
|
23496
|
-
});
|
|
23497
|
-
if (this.options.isVerbose) {
|
|
23498
|
-
console.info(`🔧 Executing tool: ${functionName}`, functionArgs);
|
|
23499
|
-
}
|
|
23500
|
-
// Get execution tools for script execution
|
|
23501
|
-
const executionTools = this.options
|
|
23502
|
-
.executionTools;
|
|
23503
|
-
if (!executionTools || !executionTools.script) {
|
|
23504
|
-
throw new PipelineExecutionError(`Model requested tool '${functionName}' but no executionTools.script were provided in OpenAiAssistantExecutionTools options`);
|
|
23505
|
-
}
|
|
23506
|
-
// TODO: [DRY] Use some common tool caller (similar to OpenAiCompatibleExecutionTools)
|
|
23507
|
-
const scriptTools = Array.isArray(executionTools.script)
|
|
23508
|
-
? executionTools.script
|
|
23509
|
-
: [executionTools.script];
|
|
23510
|
-
let functionResponse;
|
|
23511
|
-
let errors;
|
|
23512
|
-
try {
|
|
23513
|
-
const scriptTool = scriptTools[0]; // <- TODO: [🧠] Which script tool to use?
|
|
23514
|
-
functionResponse = await scriptTool.execute({
|
|
23515
|
-
scriptLanguage: 'javascript',
|
|
23516
|
-
script: `
|
|
23517
|
-
const args = ${JSON.stringify(functionArgs)};
|
|
23518
|
-
return await ${functionName}(args);
|
|
23519
|
-
`,
|
|
23520
|
-
parameters: prompt.parameters,
|
|
23521
|
-
});
|
|
23522
|
-
if (this.options.isVerbose) {
|
|
23523
|
-
console.info(`✅ Tool ${functionName} executed:`, functionResponse);
|
|
23524
|
-
}
|
|
23525
|
-
}
|
|
23526
|
-
catch (error) {
|
|
23527
|
-
assertsError(error);
|
|
23528
|
-
const serializedError = serializeError(error);
|
|
23529
|
-
errors = [serializedError];
|
|
23530
|
-
functionResponse = spaceTrim$2((block) => `
|
|
23531
|
-
|
|
23532
|
-
The invoked tool \`${functionName}\` failed with error:
|
|
23533
|
-
|
|
23534
|
-
\`\`\`json
|
|
23535
|
-
${block(JSON.stringify(serializedError, null, 4))}
|
|
23536
|
-
\`\`\`
|
|
23537
|
-
|
|
23538
|
-
`);
|
|
23539
|
-
console.error(colors.bgRed(`❌ Error executing tool ${functionName}:`));
|
|
23540
|
-
console.error(error);
|
|
23541
|
-
}
|
|
23542
|
-
toolOutputs.push({
|
|
23543
|
-
tool_call_id: toolCall.id,
|
|
23544
|
-
output: functionResponse,
|
|
23545
|
-
});
|
|
23546
|
-
completedToolCalls.push({
|
|
23547
|
-
name: functionName,
|
|
23548
|
-
arguments: toolCall.function.arguments,
|
|
23549
|
-
result: functionResponse,
|
|
23550
|
-
rawToolCall: toolCall,
|
|
23551
|
-
createdAt: toolCall.id ? toolCallStartedAt.get(toolCall.id) || calledAt : calledAt,
|
|
23552
|
-
errors,
|
|
23553
|
-
});
|
|
23554
|
-
}
|
|
23555
|
-
}
|
|
23556
|
-
// Submit tool outputs
|
|
23557
|
-
run = await client.beta.threads.runs.submitToolOutputs(run.thread_id, run.id, {
|
|
23558
|
-
tool_outputs: toolOutputs,
|
|
23559
|
-
});
|
|
23560
|
-
}
|
|
23561
|
-
else {
|
|
23562
|
-
// Wait a bit before polling again
|
|
23563
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
23564
|
-
run = await client.beta.threads.runs.retrieve(run.thread_id, run.id);
|
|
23565
|
-
}
|
|
23566
|
-
}
|
|
23567
|
-
if (run.status !== 'completed') {
|
|
23568
|
-
throw new PipelineExecutionError(`Assistant run failed with status: ${run.status}`);
|
|
23569
|
-
}
|
|
23570
|
-
// Get messages from the thread
|
|
23571
|
-
const messages = await client.beta.threads.messages.list(run.thread_id);
|
|
23572
|
-
const assistantMessages = messages.data.filter((msg) => msg.role === 'assistant');
|
|
23573
|
-
if (assistantMessages.length === 0) {
|
|
23574
|
-
throw new PipelineExecutionError('No assistant messages found after run completion');
|
|
23575
|
-
}
|
|
23576
|
-
const lastMessage = assistantMessages[0];
|
|
23577
|
-
const textContent = lastMessage.content.find((c) => c.type === 'text');
|
|
23578
|
-
if (!textContent || textContent.type !== 'text') {
|
|
23579
|
-
throw new PipelineExecutionError('No text content in assistant response');
|
|
23580
|
-
}
|
|
23581
|
-
complete = $getCurrentDate();
|
|
23582
|
-
const resultContent = textContent.text.value;
|
|
23583
|
-
const usage = UNCERTAIN_USAGE;
|
|
23584
|
-
// Progress callback with final result
|
|
23585
|
-
const finalChunk = {
|
|
23586
|
-
content: resultContent,
|
|
23587
|
-
modelName: 'assistant',
|
|
23588
|
-
timing: { start, complete },
|
|
23589
|
-
usage,
|
|
23590
|
-
rawPromptContent,
|
|
23591
|
-
rawRequest,
|
|
23592
|
-
rawResponse: { run, messages: messages.data },
|
|
23593
|
-
toolCalls: completedToolCalls.length > 0 ? completedToolCalls : undefined,
|
|
23594
|
-
};
|
|
23595
|
-
onProgress(finalChunk);
|
|
23596
|
-
return exportJson({
|
|
23597
|
-
name: 'promptResult',
|
|
23598
|
-
message: `Result of \`OpenAiAssistantExecutionTools.callChatModelStream\` (with tools)`,
|
|
23599
|
-
order: [],
|
|
23600
|
-
value: finalChunk,
|
|
23601
|
-
});
|
|
23602
|
-
}
|
|
23603
|
-
// Streaming mode (without tools)
|
|
23604
|
-
const rawRequest = {
|
|
23605
|
-
// TODO: [👨👨👧👧] ...modelSettings,
|
|
23606
|
-
// TODO: [👨👨👧👧][🧠] What about system message for assistants, does it make sense - combination of OpenAI assistants with Promptbook Personas
|
|
23607
|
-
assistant_id: this.assistantId,
|
|
23608
|
-
thread: {
|
|
23609
|
-
messages: threadMessages,
|
|
23610
|
-
},
|
|
23611
|
-
tools: modelRequirements.tools === undefined ? undefined : mapToolsToOpenAi(modelRequirements.tools),
|
|
23612
|
-
// <- TODO: Add user identification here> user: this.options.user,
|
|
23613
|
-
};
|
|
23614
|
-
if (this.options.isVerbose) {
|
|
23615
|
-
console.info(colors.bgWhite('rawRequest (streaming)'), JSON.stringify(rawRequest, null, 4));
|
|
23616
|
-
}
|
|
23617
|
-
const stream = await client.beta.threads.createAndRunStream(rawRequest);
|
|
23618
|
-
stream.on('connect', () => {
|
|
23619
|
-
if (this.options.isVerbose) {
|
|
23620
|
-
console.info('connect', stream.currentEvent);
|
|
23621
|
-
}
|
|
23622
|
-
});
|
|
23623
|
-
stream.on('textDelta', (textDelta, snapshot) => {
|
|
23624
|
-
if (this.options.isVerbose && textDelta.value) {
|
|
23625
|
-
console.info('textDelta', textDelta.value);
|
|
23626
|
-
}
|
|
23627
|
-
const chunk = {
|
|
23628
|
-
content: snapshot.value,
|
|
23629
|
-
modelName: 'assistant',
|
|
23630
|
-
timing: {
|
|
23631
|
-
start,
|
|
23632
|
-
complete: $getCurrentDate(),
|
|
23633
|
-
},
|
|
23634
|
-
usage: UNCERTAIN_USAGE,
|
|
23635
|
-
rawPromptContent,
|
|
23636
|
-
rawRequest,
|
|
23637
|
-
rawResponse: snapshot,
|
|
23638
|
-
};
|
|
23639
|
-
onProgress(chunk);
|
|
23640
|
-
});
|
|
23641
|
-
stream.on('messageCreated', (message) => {
|
|
23642
|
-
if (this.options.isVerbose) {
|
|
23643
|
-
console.info('messageCreated', message);
|
|
23644
|
-
}
|
|
23645
|
-
});
|
|
23646
|
-
stream.on('messageDone', (message) => {
|
|
23647
|
-
if (this.options.isVerbose) {
|
|
23648
|
-
console.info('messageDone', message);
|
|
23649
|
-
}
|
|
23650
|
-
});
|
|
23651
|
-
// TODO: [🐱🚀] Handle tool calls in assistants
|
|
23652
|
-
// Note: OpenAI Assistant streaming with tool calls requires special handling.
|
|
23653
|
-
// The stream will pause when a tool call is needed, and we need to:
|
|
23654
|
-
// 1. Wait for the run to reach 'requires_action' status
|
|
23655
|
-
// 2. Execute the tool calls
|
|
23656
|
-
// 3. Submit tool outputs via a separate API call (not on the stream)
|
|
23657
|
-
// 4. Continue the run
|
|
23658
|
-
// This requires switching to non-streaming mode or using the Runs API directly.
|
|
23659
|
-
// For now, tools with assistants should use the non-streaming chat completions API instead.
|
|
23660
|
-
const rawResponse = await stream.finalMessages();
|
|
23661
|
-
if (this.options.isVerbose) {
|
|
23662
|
-
console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
|
|
23663
|
-
}
|
|
23664
|
-
if (rawResponse.length !== 1) {
|
|
23665
|
-
throw new PipelineExecutionError(`There is NOT 1 BUT ${rawResponse.length} finalMessages from OpenAI`);
|
|
23666
|
-
}
|
|
23667
|
-
if (rawResponse[0].content.length !== 1) {
|
|
23668
|
-
throw new PipelineExecutionError(`There is NOT 1 BUT ${rawResponse[0].content.length} finalMessages content from OpenAI`);
|
|
23669
|
-
}
|
|
23670
|
-
if (((_b = rawResponse[0].content[0]) === null || _b === void 0 ? void 0 : _b.type) !== 'text') {
|
|
23671
|
-
throw new PipelineExecutionError(`There is NOT 'text' BUT ${(_c = rawResponse[0].content[0]) === null || _c === void 0 ? void 0 : _c.type} finalMessages content type from OpenAI`);
|
|
23672
|
-
}
|
|
23673
|
-
let resultContent = (_d = rawResponse[0].content[0]) === null || _d === void 0 ? void 0 : _d.text.value;
|
|
23674
|
-
// Process annotations to replace file IDs with filenames
|
|
23675
|
-
if ((_e = rawResponse[0].content[0]) === null || _e === void 0 ? void 0 : _e.text.annotations) {
|
|
23676
|
-
const annotations = (_f = rawResponse[0].content[0]) === null || _f === void 0 ? void 0 : _f.text.annotations;
|
|
23677
|
-
// Map to store file ID -> filename to avoid duplicate requests
|
|
23678
|
-
const fileIdToName = new Map();
|
|
23679
|
-
for (const annotation of annotations) {
|
|
23680
|
-
if (annotation.type === 'file_citation') {
|
|
23681
|
-
const fileId = annotation.file_citation.file_id;
|
|
23682
|
-
let filename = fileIdToName.get(fileId);
|
|
23683
|
-
if (!filename) {
|
|
23684
|
-
try {
|
|
23685
|
-
const file = await client.files.retrieve(fileId);
|
|
23686
|
-
filename = file.filename;
|
|
23687
|
-
fileIdToName.set(fileId, filename);
|
|
23688
|
-
}
|
|
23689
|
-
catch (error) {
|
|
23690
|
-
console.error(`Failed to retrieve file info for ${fileId}`, error);
|
|
23691
|
-
// Fallback to "Source" or keep original if fetch fails
|
|
23692
|
-
filename = 'Source';
|
|
23693
|
-
}
|
|
23694
|
-
}
|
|
23695
|
-
if (filename && resultContent) {
|
|
23696
|
-
// Replace the citation marker with filename
|
|
23697
|
-
// Regex to match the second part of the citation: 【id†source】 -> 【id†filename】
|
|
23698
|
-
// Note: annotation.text contains the exact marker like 【4:0†source】
|
|
23699
|
-
const newText = annotation.text.replace(/†.*?】/, `†${filename}】`);
|
|
23700
|
-
resultContent = resultContent.replace(annotation.text, newText);
|
|
23701
|
-
}
|
|
23702
|
-
}
|
|
23703
|
-
}
|
|
23704
|
-
}
|
|
23705
|
-
// eslint-disable-next-line prefer-const
|
|
23706
|
-
complete = $getCurrentDate();
|
|
23707
|
-
const usage = UNCERTAIN_USAGE;
|
|
23708
|
-
// <- TODO: [🥘] Compute real usage for assistant
|
|
23709
|
-
// ?> const usage = computeOpenAiUsage(content, resultContent || '', rawResponse);
|
|
23710
|
-
if (resultContent === null) {
|
|
23711
|
-
throw new PipelineExecutionError('No response message from OpenAI');
|
|
23712
|
-
}
|
|
23713
|
-
return exportJson({
|
|
23714
|
-
name: 'promptResult',
|
|
23715
|
-
message: `Result of \`OpenAiAssistantExecutionTools.callChatModelStream\``,
|
|
23716
|
-
order: [],
|
|
23717
|
-
value: {
|
|
23718
|
-
content: resultContent,
|
|
23719
|
-
modelName: 'assistant',
|
|
23720
|
-
// <- TODO: [🥘] Detect used model in assistant
|
|
23721
|
-
// ?> model: rawResponse.model || modelName,
|
|
23722
|
-
timing: {
|
|
23723
|
-
start,
|
|
23724
|
-
complete,
|
|
23725
|
-
},
|
|
23726
|
-
usage,
|
|
23727
|
-
rawPromptContent,
|
|
23728
|
-
rawRequest,
|
|
23729
|
-
rawResponse,
|
|
23730
|
-
// <- [🗯]
|
|
23731
|
-
},
|
|
23732
|
-
});
|
|
23733
|
-
}
|
|
23734
|
-
/*
|
|
23735
|
-
public async playground() {
|
|
23736
|
-
const client = await this.getClient();
|
|
23737
|
-
|
|
23738
|
-
// List all assistants
|
|
23739
|
-
const assistants = await client.beta.assistants.list();
|
|
23740
|
-
|
|
23741
|
-
// Get details of a specific assistant
|
|
23742
|
-
const assistantId = 'asst_MO8fhZf4dGloCfXSHeLcIik0';
|
|
23743
|
-
const assistant = await client.beta.assistants.retrieve(assistantId);
|
|
23744
|
-
|
|
23745
|
-
// Update an assistant
|
|
23746
|
-
const updatedAssistant = await client.beta.assistants.update(assistantId, {
|
|
23747
|
-
name: assistant.name + '(M)',
|
|
23748
|
-
description: 'Updated description via Promptbook',
|
|
23749
|
-
metadata: {
|
|
23750
|
-
[Math.random().toString(36).substring(2, 15)]: new Date().toISOString(),
|
|
23751
|
-
},
|
|
23752
|
-
});
|
|
23753
|
-
|
|
23754
|
-
await forEver();
|
|
23755
|
-
}
|
|
23756
|
-
*/
|
|
23757
|
-
/**
|
|
23758
|
-
* Get an existing assistant tool wrapper
|
|
23759
|
-
*/
|
|
23760
|
-
getAssistant(assistantId) {
|
|
23761
|
-
return new OpenAiAssistantExecutionTools({
|
|
23762
|
-
...this.options,
|
|
23763
|
-
isCreatingNewAssistantsAllowed: this.isCreatingNewAssistantsAllowed,
|
|
23764
|
-
assistantId,
|
|
23765
|
-
});
|
|
23766
|
-
}
|
|
23767
|
-
/**
|
|
23768
|
-
* Returns the per-knowledge-source download timeout in milliseconds.
|
|
23769
|
-
*/
|
|
23770
|
-
getKnowledgeSourceDownloadTimeoutMs() {
|
|
23771
|
-
var _a;
|
|
23772
|
-
return (_a = this.assistantOptions.knowledgeSourceDownloadTimeoutMs) !== null && _a !== void 0 ? _a : DEFAULT_KNOWLEDGE_SOURCE_DOWNLOAD_TIMEOUT_MS;
|
|
23773
|
-
}
|
|
23774
|
-
/**
|
|
23775
|
-
* Returns the max concurrency for knowledge source uploads.
|
|
23776
|
-
*/
|
|
23777
|
-
getKnowledgeSourceUploadMaxConcurrency() {
|
|
23778
|
-
var _a;
|
|
23779
|
-
return (_a = this.assistantOptions.knowledgeSourceUploadMaxConcurrency) !== null && _a !== void 0 ? _a : 5;
|
|
23780
|
-
}
|
|
23781
|
-
/**
|
|
23782
|
-
* Returns the polling interval in milliseconds for vector store uploads.
|
|
23783
|
-
*/
|
|
23784
|
-
getKnowledgeSourceUploadPollIntervalMs() {
|
|
23785
|
-
var _a;
|
|
23786
|
-
return (_a = this.assistantOptions.knowledgeSourceUploadPollIntervalMs) !== null && _a !== void 0 ? _a : 5000;
|
|
23787
|
-
}
|
|
23788
|
-
/**
|
|
23789
|
-
* Returns the overall upload timeout in milliseconds for vector store uploads.
|
|
23790
|
-
*/
|
|
23791
|
-
getKnowledgeSourceUploadTimeoutMs() {
|
|
23792
|
-
var _a;
|
|
23793
|
-
return (_a = this.assistantOptions.knowledgeSourceUploadTimeoutMs) !== null && _a !== void 0 ? _a : DEFAULT_KNOWLEDGE_SOURCE_UPLOAD_TIMEOUT_MS;
|
|
23794
|
-
}
|
|
23795
|
-
/**
|
|
23796
|
-
* Returns true if we should continue even if vector store ingestion stalls.
|
|
23797
|
-
*/
|
|
23798
|
-
shouldContinueOnVectorStoreStall() {
|
|
23799
|
-
var _a;
|
|
23800
|
-
return (_a = this.assistantOptions.shouldContinueOnVectorStoreStall) !== null && _a !== void 0 ? _a : true;
|
|
23801
|
-
}
|
|
23802
|
-
/**
|
|
23803
|
-
* Returns assistant-specific options with extended settings.
|
|
23804
|
-
*/
|
|
23805
|
-
get assistantOptions() {
|
|
23806
|
-
return this.options;
|
|
23807
|
-
}
|
|
23808
|
-
/**
|
|
23809
|
-
* Downloads a knowledge source URL into a File for vector store upload.
|
|
23810
|
-
*/
|
|
23811
|
-
async downloadKnowledgeSourceFile(options) {
|
|
23812
|
-
var _a;
|
|
23813
|
-
const { source, timeoutMs, logLabel } = options;
|
|
23814
|
-
const startedAtMs = Date.now();
|
|
23815
|
-
const controller = new AbortController();
|
|
23816
|
-
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
23817
|
-
if (this.options.isVerbose) {
|
|
23818
|
-
console.info('[🤰]', 'Downloading knowledge source', {
|
|
23819
|
-
source,
|
|
23820
|
-
timeoutMs,
|
|
23821
|
-
logLabel,
|
|
23822
|
-
});
|
|
23823
|
-
}
|
|
23824
|
-
try {
|
|
23825
|
-
const response = await fetch(source, { signal: controller.signal });
|
|
23826
|
-
const contentType = (_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : undefined;
|
|
23827
|
-
if (!response.ok) {
|
|
23828
|
-
console.error('[🤰]', 'Failed to download knowledge source', {
|
|
23829
|
-
source,
|
|
23830
|
-
status: response.status,
|
|
23831
|
-
statusText: response.statusText,
|
|
23832
|
-
contentType,
|
|
23833
|
-
elapsedMs: Date.now() - startedAtMs,
|
|
23834
|
-
logLabel,
|
|
23835
|
-
});
|
|
23836
|
-
return null;
|
|
23837
|
-
}
|
|
23838
|
-
const buffer = await response.arrayBuffer();
|
|
23839
|
-
let filename = source.split('/').pop() || 'downloaded-file';
|
|
23840
|
-
try {
|
|
23841
|
-
const url = new URL(source);
|
|
23842
|
-
filename = url.pathname.split('/').pop() || filename;
|
|
23843
|
-
}
|
|
23844
|
-
catch (error) {
|
|
23845
|
-
// Keep default filename
|
|
23846
|
-
}
|
|
23847
|
-
const file = new File([buffer], filename, contentType ? { type: contentType } : undefined);
|
|
23848
|
-
const elapsedMs = Date.now() - startedAtMs;
|
|
23849
|
-
const sizeBytes = buffer.byteLength;
|
|
23850
|
-
if (this.options.isVerbose) {
|
|
23851
|
-
console.info('[🤰]', 'Downloaded knowledge source', {
|
|
23852
|
-
source,
|
|
23853
|
-
filename,
|
|
23854
|
-
sizeBytes,
|
|
23855
|
-
contentType,
|
|
23856
|
-
elapsedMs,
|
|
23857
|
-
logLabel,
|
|
23858
|
-
});
|
|
23859
|
-
}
|
|
23860
|
-
return { file, sizeBytes, filename, elapsedMs };
|
|
23861
|
-
}
|
|
23862
|
-
catch (error) {
|
|
23863
|
-
assertsError(error);
|
|
23864
|
-
console.error('[🤰]', 'Error downloading knowledge source', {
|
|
23865
|
-
source,
|
|
23866
|
-
elapsedMs: Date.now() - startedAtMs,
|
|
23867
|
-
logLabel,
|
|
23868
|
-
error: serializeError(error),
|
|
23869
|
-
});
|
|
23870
|
-
return null;
|
|
23871
|
-
}
|
|
23872
|
-
finally {
|
|
23873
|
-
clearTimeout(timeoutId);
|
|
23874
|
-
}
|
|
23875
|
-
}
|
|
23876
|
-
/**
|
|
23877
|
-
* Logs vector store file batch diagnostics to help trace ingestion stalls or failures.
|
|
23878
|
-
*/
|
|
23879
|
-
async logVectorStoreFileBatchDiagnostics(options) {
|
|
23880
|
-
var _a, _b;
|
|
23881
|
-
const { client, vectorStoreId, batchId, uploadedFiles, logLabel, reason } = options;
|
|
23882
|
-
if (reason === 'stalled' && !this.options.isVerbose) {
|
|
23883
|
-
return;
|
|
23884
|
-
}
|
|
23885
|
-
if (!batchId.startsWith('vsfb_')) {
|
|
23886
|
-
console.error('[🤰]', 'Vector store file batch diagnostics skipped (invalid batch id)', {
|
|
23887
|
-
vectorStoreId,
|
|
23888
|
-
batchId,
|
|
23889
|
-
reason,
|
|
23890
|
-
logLabel,
|
|
23891
|
-
});
|
|
23892
|
-
return;
|
|
23893
|
-
}
|
|
23894
|
-
const fileIdToMetadata = new Map();
|
|
23895
|
-
for (const file of uploadedFiles) {
|
|
23896
|
-
fileIdToMetadata.set(file.fileId, file);
|
|
23897
|
-
}
|
|
23898
|
-
try {
|
|
23899
|
-
const limit = Math.min(100, Math.max(10, uploadedFiles.length));
|
|
23900
|
-
const batchFilesPage = await client.beta.vectorStores.fileBatches.listFiles(vectorStoreId, batchId, {
|
|
23901
|
-
limit,
|
|
23902
|
-
});
|
|
23903
|
-
const batchFiles = (_a = batchFilesPage.data) !== null && _a !== void 0 ? _a : [];
|
|
23904
|
-
const statusCounts = {
|
|
23905
|
-
in_progress: 0,
|
|
23906
|
-
completed: 0,
|
|
23907
|
-
failed: 0,
|
|
23908
|
-
cancelled: 0,
|
|
23435
|
+
return;
|
|
23436
|
+
}
|
|
23437
|
+
const fileIdToMetadata = new Map();
|
|
23438
|
+
for (const file of uploadedFiles) {
|
|
23439
|
+
fileIdToMetadata.set(file.fileId, file);
|
|
23440
|
+
}
|
|
23441
|
+
try {
|
|
23442
|
+
const vectorStores = this.getVectorStoresApi(client);
|
|
23443
|
+
const limit = Math.min(100, Math.max(10, uploadedFiles.length));
|
|
23444
|
+
const batchFilesPage = await vectorStores.fileBatches.listFiles(batchId, {
|
|
23445
|
+
vector_store_id: vectorStoreId,
|
|
23446
|
+
limit,
|
|
23447
|
+
});
|
|
23448
|
+
const batchFiles = (_a = batchFilesPage.data) !== null && _a !== void 0 ? _a : [];
|
|
23449
|
+
const statusCounts = {
|
|
23450
|
+
in_progress: 0,
|
|
23451
|
+
completed: 0,
|
|
23452
|
+
failed: 0,
|
|
23453
|
+
cancelled: 0,
|
|
23909
23454
|
};
|
|
23910
23455
|
const errorSamples = [];
|
|
23911
23456
|
const inProgressSamples = [];
|
|
23912
23457
|
const batchFileIds = new Set();
|
|
23913
23458
|
for (const file of batchFiles) {
|
|
23914
|
-
|
|
23915
|
-
statusCounts[
|
|
23916
|
-
const
|
|
23917
|
-
|
|
23459
|
+
const status = (_b = file.status) !== null && _b !== void 0 ? _b : 'unknown';
|
|
23460
|
+
statusCounts[status] = ((_c = statusCounts[status]) !== null && _c !== void 0 ? _c : 0) + 1;
|
|
23461
|
+
const vectorStoreFileId = file.id;
|
|
23462
|
+
const uploadedFileId = (_d = file.file_id) !== null && _d !== void 0 ? _d : file.fileId;
|
|
23463
|
+
const fileId = uploadedFileId !== null && uploadedFileId !== void 0 ? uploadedFileId : vectorStoreFileId;
|
|
23464
|
+
batchFileIds.add(fileId);
|
|
23465
|
+
const metadata = fileIdToMetadata.get(fileId);
|
|
23466
|
+
if (status === 'failed') {
|
|
23918
23467
|
errorSamples.push({
|
|
23919
|
-
fileId
|
|
23468
|
+
fileId,
|
|
23469
|
+
status,
|
|
23470
|
+
error: (_e = file.last_error) === null || _e === void 0 ? void 0 : _e.message,
|
|
23920
23471
|
filename: metadata === null || metadata === void 0 ? void 0 : metadata.filename,
|
|
23921
|
-
|
|
23922
|
-
status: file.status,
|
|
23923
|
-
lastError: file.last_error,
|
|
23472
|
+
vectorStoreFileId: uploadedFileId ? vectorStoreFileId : undefined,
|
|
23924
23473
|
});
|
|
23925
23474
|
}
|
|
23926
|
-
|
|
23475
|
+
if (status === 'in_progress') {
|
|
23927
23476
|
inProgressSamples.push({
|
|
23928
|
-
fileId
|
|
23477
|
+
fileId,
|
|
23929
23478
|
filename: metadata === null || metadata === void 0 ? void 0 : metadata.filename,
|
|
23930
|
-
|
|
23479
|
+
vectorStoreFileId: uploadedFileId ? vectorStoreFileId : undefined,
|
|
23931
23480
|
});
|
|
23932
23481
|
}
|
|
23933
23482
|
}
|
|
@@ -23939,7 +23488,7 @@ class OpenAiAssistantExecutionTools extends OpenAiExecutionTools {
|
|
|
23939
23488
|
filename: file.filename,
|
|
23940
23489
|
sizeBytes: file.sizeBytes,
|
|
23941
23490
|
}));
|
|
23942
|
-
const vectorStore = await
|
|
23491
|
+
const vectorStore = await vectorStores.retrieve(vectorStoreId);
|
|
23943
23492
|
const logPayload = {
|
|
23944
23493
|
vectorStoreId,
|
|
23945
23494
|
batchId,
|
|
@@ -23973,8 +23522,9 @@ class OpenAiAssistantExecutionTools extends OpenAiExecutionTools {
|
|
|
23973
23522
|
* Uploads knowledge source files to the vector store and polls until processing completes.
|
|
23974
23523
|
*/
|
|
23975
23524
|
async uploadKnowledgeSourceFilesToVectorStore(options) {
|
|
23976
|
-
var _a, _b, _c, _d;
|
|
23525
|
+
var _a, _b, _c, _d, _e, _f;
|
|
23977
23526
|
const { client, vectorStoreId, files, totalBytes, logLabel } = options;
|
|
23527
|
+
const vectorStores = this.getVectorStoresApi(client);
|
|
23978
23528
|
const uploadStartedAtMs = Date.now();
|
|
23979
23529
|
const maxConcurrency = Math.max(1, this.getKnowledgeSourceUploadMaxConcurrency());
|
|
23980
23530
|
const pollIntervalMs = Math.max(1000, this.getKnowledgeSourceUploadPollIntervalMs());
|
|
@@ -24091,373 +23641,1288 @@ class OpenAiAssistantExecutionTools extends OpenAiExecutionTools {
|
|
|
24091
23641
|
failedCount: failedUploads.length,
|
|
24092
23642
|
logLabel,
|
|
24093
23643
|
});
|
|
24094
|
-
return null;
|
|
23644
|
+
return null;
|
|
23645
|
+
}
|
|
23646
|
+
const batch = await vectorStores.fileBatches.create(vectorStoreId, {
|
|
23647
|
+
file_ids: fileIds,
|
|
23648
|
+
});
|
|
23649
|
+
const expectedBatchId = batch.id;
|
|
23650
|
+
const expectedBatchIdValid = expectedBatchId.startsWith('vsfb_');
|
|
23651
|
+
if (!expectedBatchIdValid) {
|
|
23652
|
+
console.error('[🤰]', 'Vector store file batch id looks invalid', {
|
|
23653
|
+
vectorStoreId,
|
|
23654
|
+
batchId: expectedBatchId,
|
|
23655
|
+
batchVectorStoreId: batch.vector_store_id,
|
|
23656
|
+
logLabel,
|
|
23657
|
+
});
|
|
23658
|
+
}
|
|
23659
|
+
else if (batch.vector_store_id !== vectorStoreId) {
|
|
23660
|
+
console.error('[🤰]', 'Vector store file batch vector store id mismatch', {
|
|
23661
|
+
vectorStoreId,
|
|
23662
|
+
batchId: expectedBatchId,
|
|
23663
|
+
batchVectorStoreId: batch.vector_store_id,
|
|
23664
|
+
logLabel,
|
|
23665
|
+
});
|
|
23666
|
+
}
|
|
23667
|
+
if (this.options.isVerbose) {
|
|
23668
|
+
console.info('[🤰]', 'Created vector store file batch', {
|
|
23669
|
+
vectorStoreId,
|
|
23670
|
+
batchId: expectedBatchId,
|
|
23671
|
+
fileCount: fileIds.length,
|
|
23672
|
+
logLabel,
|
|
23673
|
+
});
|
|
23674
|
+
}
|
|
23675
|
+
const pollStartedAtMs = Date.now();
|
|
23676
|
+
const progressLogIntervalMs = Math.max(VECTOR_STORE_PROGRESS_LOG_INTERVAL_MIN_MS, pollIntervalMs);
|
|
23677
|
+
const diagnosticsIntervalMs = Math.max(60000, pollIntervalMs * 5);
|
|
23678
|
+
// let lastStatus: string | undefined;
|
|
23679
|
+
let lastCountsKey = '';
|
|
23680
|
+
let lastProgressKey = '';
|
|
23681
|
+
let lastLogAtMs = 0;
|
|
23682
|
+
let lastProgressAtMs = pollStartedAtMs;
|
|
23683
|
+
let lastDiagnosticsAtMs = pollStartedAtMs;
|
|
23684
|
+
let latestBatch = batch;
|
|
23685
|
+
let loggedBatchIdMismatch = false;
|
|
23686
|
+
let loggedBatchIdFallback = false;
|
|
23687
|
+
let loggedBatchIdInvalid = false;
|
|
23688
|
+
let shouldPoll = true;
|
|
23689
|
+
while (shouldPoll) {
|
|
23690
|
+
const nowMs = Date.now();
|
|
23691
|
+
// [🤰] Note: Sometimes OpenAI returns Vector Store object instead of Batch object, or IDs get swapped.
|
|
23692
|
+
const rawBatchId = typeof latestBatch.id === 'string' ? latestBatch.id : '';
|
|
23693
|
+
const rawVectorStoreId = latestBatch.vector_store_id;
|
|
23694
|
+
let returnedBatchId = rawBatchId;
|
|
23695
|
+
let returnedBatchIdValid = typeof returnedBatchId === 'string' && returnedBatchId.startsWith('vsfb_');
|
|
23696
|
+
if (!returnedBatchIdValid && expectedBatchIdValid) {
|
|
23697
|
+
if (!loggedBatchIdFallback) {
|
|
23698
|
+
console.error('[🤰]', 'Vector store file batch id missing from response; falling back to expected', {
|
|
23699
|
+
vectorStoreId,
|
|
23700
|
+
expectedBatchId,
|
|
23701
|
+
returnedBatchId,
|
|
23702
|
+
rawVectorStoreId,
|
|
23703
|
+
logLabel,
|
|
23704
|
+
});
|
|
23705
|
+
loggedBatchIdFallback = true;
|
|
23706
|
+
}
|
|
23707
|
+
returnedBatchId = expectedBatchId;
|
|
23708
|
+
returnedBatchIdValid = true;
|
|
23709
|
+
}
|
|
23710
|
+
if (!returnedBatchIdValid && !loggedBatchIdInvalid) {
|
|
23711
|
+
console.error('[🤰]', 'Vector store file batch id is invalid; stopping polling', {
|
|
23712
|
+
vectorStoreId,
|
|
23713
|
+
expectedBatchId,
|
|
23714
|
+
returnedBatchId,
|
|
23715
|
+
rawVectorStoreId,
|
|
23716
|
+
logLabel,
|
|
23717
|
+
});
|
|
23718
|
+
loggedBatchIdInvalid = true;
|
|
23719
|
+
}
|
|
23720
|
+
const batchIdMismatch = expectedBatchIdValid && returnedBatchIdValid && returnedBatchId !== expectedBatchId;
|
|
23721
|
+
if (batchIdMismatch && !loggedBatchIdMismatch) {
|
|
23722
|
+
console.error('[🤰]', 'Vector store file batch id mismatch', {
|
|
23723
|
+
vectorStoreId,
|
|
23724
|
+
expectedBatchId,
|
|
23725
|
+
returnedBatchId,
|
|
23726
|
+
logLabel,
|
|
23727
|
+
});
|
|
23728
|
+
loggedBatchIdMismatch = true;
|
|
23729
|
+
}
|
|
23730
|
+
if (returnedBatchIdValid) {
|
|
23731
|
+
latestBatch = await vectorStores.fileBatches.retrieve(returnedBatchId, {
|
|
23732
|
+
vector_store_id: vectorStoreId,
|
|
23733
|
+
});
|
|
23734
|
+
}
|
|
23735
|
+
else {
|
|
23736
|
+
shouldPoll = false;
|
|
23737
|
+
continue;
|
|
23738
|
+
}
|
|
23739
|
+
const status = (_e = latestBatch.status) !== null && _e !== void 0 ? _e : 'unknown';
|
|
23740
|
+
const fileCounts = (_f = latestBatch.file_counts) !== null && _f !== void 0 ? _f : {};
|
|
23741
|
+
const progressKey = JSON.stringify(fileCounts);
|
|
23742
|
+
const statusCountsKey = `${status}-${progressKey}`;
|
|
23743
|
+
const isProgressing = progressKey !== lastProgressKey;
|
|
23744
|
+
if (isProgressing) {
|
|
23745
|
+
lastProgressAtMs = nowMs;
|
|
23746
|
+
lastProgressKey = progressKey;
|
|
23747
|
+
}
|
|
23748
|
+
if (this.options.isVerbose &&
|
|
23749
|
+
(statusCountsKey !== lastCountsKey || nowMs - lastLogAtMs >= progressLogIntervalMs)) {
|
|
23750
|
+
console.info('[🤰]', 'Vector store file batch status', {
|
|
23751
|
+
vectorStoreId,
|
|
23752
|
+
batchId: returnedBatchId,
|
|
23753
|
+
status,
|
|
23754
|
+
fileCounts,
|
|
23755
|
+
elapsedMs: nowMs - pollStartedAtMs,
|
|
23756
|
+
logLabel,
|
|
23757
|
+
});
|
|
23758
|
+
lastCountsKey = statusCountsKey;
|
|
23759
|
+
lastLogAtMs = nowMs;
|
|
23760
|
+
}
|
|
23761
|
+
if (status === 'in_progress' &&
|
|
23762
|
+
nowMs - lastProgressAtMs >= VECTOR_STORE_STALL_LOG_THRESHOLD_MS &&
|
|
23763
|
+
nowMs - lastDiagnosticsAtMs >= diagnosticsIntervalMs) {
|
|
23764
|
+
lastDiagnosticsAtMs = nowMs;
|
|
23765
|
+
await this.logVectorStoreFileBatchDiagnostics({
|
|
23766
|
+
client,
|
|
23767
|
+
vectorStoreId,
|
|
23768
|
+
batchId: returnedBatchId,
|
|
23769
|
+
uploadedFiles,
|
|
23770
|
+
logLabel,
|
|
23771
|
+
reason: 'stalled',
|
|
23772
|
+
});
|
|
23773
|
+
}
|
|
23774
|
+
if (status === 'completed') {
|
|
23775
|
+
if (this.options.isVerbose) {
|
|
23776
|
+
console.info('[🤰]', 'Vector store file batch completed', {
|
|
23777
|
+
vectorStoreId,
|
|
23778
|
+
batchId: returnedBatchId,
|
|
23779
|
+
fileCounts,
|
|
23780
|
+
elapsedMs: nowMs - pollStartedAtMs,
|
|
23781
|
+
logLabel,
|
|
23782
|
+
});
|
|
23783
|
+
}
|
|
23784
|
+
shouldPoll = false;
|
|
23785
|
+
continue;
|
|
23786
|
+
}
|
|
23787
|
+
if (status === 'failed') {
|
|
23788
|
+
console.error('[🤰]', 'Vector store file batch completed with failures', {
|
|
23789
|
+
vectorStoreId,
|
|
23790
|
+
batchId: returnedBatchId,
|
|
23791
|
+
fileCounts,
|
|
23792
|
+
elapsedMs: nowMs - pollStartedAtMs,
|
|
23793
|
+
logLabel,
|
|
23794
|
+
});
|
|
23795
|
+
await this.logVectorStoreFileBatchDiagnostics({
|
|
23796
|
+
client,
|
|
23797
|
+
vectorStoreId,
|
|
23798
|
+
batchId: returnedBatchId,
|
|
23799
|
+
uploadedFiles,
|
|
23800
|
+
logLabel,
|
|
23801
|
+
reason: 'failed',
|
|
23802
|
+
});
|
|
23803
|
+
shouldPoll = false;
|
|
23804
|
+
continue;
|
|
23805
|
+
}
|
|
23806
|
+
if (status === 'cancelled') {
|
|
23807
|
+
console.error('[🤰]', 'Vector store file batch did not complete', {
|
|
23808
|
+
vectorStoreId,
|
|
23809
|
+
batchId: returnedBatchId,
|
|
23810
|
+
status,
|
|
23811
|
+
fileCounts,
|
|
23812
|
+
elapsedMs: nowMs - pollStartedAtMs,
|
|
23813
|
+
logLabel,
|
|
23814
|
+
});
|
|
23815
|
+
await this.logVectorStoreFileBatchDiagnostics({
|
|
23816
|
+
client,
|
|
23817
|
+
vectorStoreId,
|
|
23818
|
+
batchId: returnedBatchId,
|
|
23819
|
+
uploadedFiles,
|
|
23820
|
+
logLabel,
|
|
23821
|
+
reason: 'failed',
|
|
23822
|
+
});
|
|
23823
|
+
shouldPoll = false;
|
|
23824
|
+
continue;
|
|
23825
|
+
}
|
|
23826
|
+
if (nowMs - pollStartedAtMs >= uploadTimeoutMs) {
|
|
23827
|
+
console.error('[🤰]', 'Timed out waiting for vector store file batch', {
|
|
23828
|
+
vectorStoreId,
|
|
23829
|
+
batchId: returnedBatchId,
|
|
23830
|
+
fileCounts,
|
|
23831
|
+
elapsedMs: nowMs - pollStartedAtMs,
|
|
23832
|
+
uploadTimeoutMs,
|
|
23833
|
+
logLabel,
|
|
23834
|
+
});
|
|
23835
|
+
await this.logVectorStoreFileBatchDiagnostics({
|
|
23836
|
+
client,
|
|
23837
|
+
vectorStoreId,
|
|
23838
|
+
batchId: returnedBatchId,
|
|
23839
|
+
uploadedFiles,
|
|
23840
|
+
logLabel,
|
|
23841
|
+
reason: 'timeout',
|
|
23842
|
+
});
|
|
23843
|
+
if (this.shouldContinueOnVectorStoreStall()) {
|
|
23844
|
+
console.warn('[🤰]', 'Continuing despite vector store timeout as requested', {
|
|
23845
|
+
vectorStoreId,
|
|
23846
|
+
logLabel,
|
|
23847
|
+
});
|
|
23848
|
+
shouldPoll = false;
|
|
23849
|
+
continue;
|
|
23850
|
+
}
|
|
23851
|
+
try {
|
|
23852
|
+
const cancelBatchId = batchIdMismatch && returnedBatchId.startsWith('vsfb_') ? returnedBatchId : expectedBatchId;
|
|
23853
|
+
if (!cancelBatchId.startsWith('vsfb_')) {
|
|
23854
|
+
console.error('[🤰]', 'Skipping vector store file batch cancel (invalid batch id)', {
|
|
23855
|
+
vectorStoreId,
|
|
23856
|
+
batchId: cancelBatchId,
|
|
23857
|
+
logLabel,
|
|
23858
|
+
});
|
|
23859
|
+
}
|
|
23860
|
+
else {
|
|
23861
|
+
await vectorStores.fileBatches.cancel(cancelBatchId, {
|
|
23862
|
+
vector_store_id: vectorStoreId,
|
|
23863
|
+
});
|
|
23864
|
+
}
|
|
23865
|
+
if (this.options.isVerbose) {
|
|
23866
|
+
console.info('[🤰]', 'Cancelled vector store file batch after timeout', {
|
|
23867
|
+
vectorStoreId,
|
|
23868
|
+
batchId: batchIdMismatch && returnedBatchId.startsWith('vsfb_')
|
|
23869
|
+
? returnedBatchId
|
|
23870
|
+
: expectedBatchId,
|
|
23871
|
+
...(batchIdMismatch ? { returnedBatchId } : {}),
|
|
23872
|
+
logLabel,
|
|
23873
|
+
});
|
|
23874
|
+
}
|
|
23875
|
+
}
|
|
23876
|
+
catch (error) {
|
|
23877
|
+
assertsError(error);
|
|
23878
|
+
console.error('[🤰]', 'Failed to cancel vector store file batch after timeout', {
|
|
23879
|
+
vectorStoreId,
|
|
23880
|
+
batchId: expectedBatchId,
|
|
23881
|
+
...(batchIdMismatch ? { returnedBatchId } : {}),
|
|
23882
|
+
logLabel,
|
|
23883
|
+
error: serializeError(error),
|
|
23884
|
+
});
|
|
23885
|
+
}
|
|
23886
|
+
shouldPoll = false;
|
|
23887
|
+
continue;
|
|
23888
|
+
}
|
|
23889
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
23890
|
+
}
|
|
23891
|
+
return latestBatch;
|
|
23892
|
+
}
|
|
23893
|
+
/**
|
|
23894
|
+
* Creates a vector store and uploads knowledge sources, returning its ID.
|
|
23895
|
+
*/
|
|
23896
|
+
async createVectorStoreWithKnowledgeSources(options) {
|
|
23897
|
+
const { client, name, knowledgeSources, logLabel } = options;
|
|
23898
|
+
const vectorStores = this.getVectorStoresApi(client);
|
|
23899
|
+
const knowledgeSourcesCount = knowledgeSources.length;
|
|
23900
|
+
const downloadTimeoutMs = this.getKnowledgeSourceDownloadTimeoutMs();
|
|
23901
|
+
if (this.options.isVerbose) {
|
|
23902
|
+
console.info('[🤰]', 'Creating vector store with knowledge sources', {
|
|
23903
|
+
name,
|
|
23904
|
+
knowledgeSourcesCount,
|
|
23905
|
+
downloadTimeoutMs,
|
|
23906
|
+
logLabel,
|
|
23907
|
+
});
|
|
24095
23908
|
}
|
|
24096
|
-
const
|
|
24097
|
-
|
|
23909
|
+
const vectorStore = await vectorStores.create({
|
|
23910
|
+
name: `${name} Knowledge Base`,
|
|
24098
23911
|
});
|
|
24099
|
-
const
|
|
24100
|
-
|
|
24101
|
-
|
|
24102
|
-
console.error('[🤰]', 'Vector store file batch id looks invalid', {
|
|
23912
|
+
const vectorStoreId = vectorStore.id;
|
|
23913
|
+
if (this.options.isVerbose) {
|
|
23914
|
+
console.info('[🤰]', 'Vector store created', {
|
|
24103
23915
|
vectorStoreId,
|
|
24104
|
-
batchId: expectedBatchId,
|
|
24105
|
-
batchVectorStoreId: batch.vector_store_id,
|
|
24106
23916
|
logLabel,
|
|
24107
23917
|
});
|
|
24108
23918
|
}
|
|
24109
|
-
|
|
24110
|
-
|
|
24111
|
-
|
|
24112
|
-
|
|
24113
|
-
|
|
24114
|
-
|
|
24115
|
-
|
|
23919
|
+
const fileStreams = [];
|
|
23920
|
+
const skippedSources = [];
|
|
23921
|
+
let totalBytes = 0;
|
|
23922
|
+
const processingStartedAtMs = Date.now();
|
|
23923
|
+
for (const [index, source] of knowledgeSources.entries()) {
|
|
23924
|
+
try {
|
|
23925
|
+
const sourceType = source.startsWith('http') || source.startsWith('https') ? 'url' : 'file';
|
|
23926
|
+
if (this.options.isVerbose) {
|
|
23927
|
+
console.info('[🤰]', 'Processing knowledge source', {
|
|
23928
|
+
index: index + 1,
|
|
23929
|
+
total: knowledgeSourcesCount,
|
|
23930
|
+
source,
|
|
23931
|
+
sourceType,
|
|
23932
|
+
logLabel,
|
|
23933
|
+
});
|
|
23934
|
+
}
|
|
23935
|
+
// Check if it's a URL
|
|
23936
|
+
if (source.startsWith('http://') || source.startsWith('https://')) {
|
|
23937
|
+
const downloadResult = await this.downloadKnowledgeSourceFile({
|
|
23938
|
+
source,
|
|
23939
|
+
timeoutMs: downloadTimeoutMs,
|
|
23940
|
+
logLabel,
|
|
23941
|
+
});
|
|
23942
|
+
if (downloadResult) {
|
|
23943
|
+
fileStreams.push(downloadResult.file);
|
|
23944
|
+
totalBytes += downloadResult.sizeBytes;
|
|
23945
|
+
}
|
|
23946
|
+
else {
|
|
23947
|
+
skippedSources.push({ source, reason: 'download_failed' });
|
|
23948
|
+
}
|
|
23949
|
+
}
|
|
23950
|
+
else {
|
|
23951
|
+
skippedSources.push({ source, reason: 'unsupported_source_type' });
|
|
23952
|
+
if (this.options.isVerbose) {
|
|
23953
|
+
console.info('[🤰]', 'Skipping knowledge source (unsupported type)', {
|
|
23954
|
+
source,
|
|
23955
|
+
sourceType,
|
|
23956
|
+
logLabel,
|
|
23957
|
+
});
|
|
23958
|
+
}
|
|
23959
|
+
/*
|
|
23960
|
+
TODO: [🤰] Resolve problem with browser environment
|
|
23961
|
+
// Assume it's a local file path
|
|
23962
|
+
// Note: This will work in Node.js environment
|
|
23963
|
+
// For browser environments, this would need different handling
|
|
23964
|
+
const fs = await import('fs');
|
|
23965
|
+
const fileStream = fs.createReadStream(source);
|
|
23966
|
+
fileStreams.push(fileStream);
|
|
23967
|
+
*/
|
|
23968
|
+
}
|
|
23969
|
+
}
|
|
23970
|
+
catch (error) {
|
|
23971
|
+
assertsError(error);
|
|
23972
|
+
skippedSources.push({ source, reason: 'processing_error' });
|
|
23973
|
+
console.error('[🤰]', 'Error processing knowledge source', {
|
|
23974
|
+
source,
|
|
23975
|
+
logLabel,
|
|
23976
|
+
error: serializeError(error),
|
|
23977
|
+
});
|
|
23978
|
+
}
|
|
24116
23979
|
}
|
|
24117
23980
|
if (this.options.isVerbose) {
|
|
24118
|
-
console.info('[🤰]', '
|
|
24119
|
-
|
|
24120
|
-
|
|
24121
|
-
|
|
23981
|
+
console.info('[🤰]', 'Finished processing knowledge sources', {
|
|
23982
|
+
total: knowledgeSourcesCount,
|
|
23983
|
+
downloadedCount: fileStreams.length,
|
|
23984
|
+
skippedCount: skippedSources.length,
|
|
23985
|
+
totalBytes,
|
|
23986
|
+
elapsedMs: Date.now() - processingStartedAtMs,
|
|
23987
|
+
skippedSamples: skippedSources.slice(0, 3),
|
|
24122
23988
|
logLabel,
|
|
24123
23989
|
});
|
|
24124
23990
|
}
|
|
24125
|
-
|
|
24126
|
-
|
|
24127
|
-
|
|
24128
|
-
let lastStatus;
|
|
24129
|
-
let lastCountsKey = '';
|
|
24130
|
-
let lastProgressKey = '';
|
|
24131
|
-
let lastLogAtMs = 0;
|
|
24132
|
-
let lastProgressAtMs = pollStartedAtMs;
|
|
24133
|
-
let lastDiagnosticsAtMs = pollStartedAtMs;
|
|
24134
|
-
let latestBatch = batch;
|
|
24135
|
-
let loggedBatchIdMismatch = false;
|
|
24136
|
-
let shouldPoll = true;
|
|
24137
|
-
while (shouldPoll) {
|
|
24138
|
-
latestBatch = await client.beta.vectorStores.fileBatches.retrieve(vectorStoreId, expectedBatchId);
|
|
24139
|
-
const counts = latestBatch.file_counts;
|
|
24140
|
-
const countsKey = `${counts.completed}/${counts.failed}/${counts.in_progress}/${counts.cancelled}/${counts.total}`;
|
|
24141
|
-
const nowMs = Date.now();
|
|
24142
|
-
const returnedBatchId = latestBatch.id;
|
|
24143
|
-
// [🤰] Note: Sometimes OpenAI returns Vector Store object instead of Batch object, or IDs get swapped.
|
|
24144
|
-
// We only consider it a mismatch if the returned ID looks like a Batch ID.
|
|
24145
|
-
const batchIdMismatch = returnedBatchId !== expectedBatchId && returnedBatchId.startsWith('vsfb_');
|
|
24146
|
-
const diagnosticsBatchId = batchIdMismatch && returnedBatchId.startsWith('vsfb_') ? returnedBatchId : expectedBatchId;
|
|
24147
|
-
const shouldLog = this.options.isVerbose &&
|
|
24148
|
-
(latestBatch.status !== lastStatus ||
|
|
24149
|
-
countsKey !== lastCountsKey ||
|
|
24150
|
-
nowMs - lastLogAtMs >= progressLogIntervalMs);
|
|
24151
|
-
if (batchIdMismatch && !loggedBatchIdMismatch) {
|
|
24152
|
-
console.error('[🤰]', 'Vector store file batch id mismatch', {
|
|
23991
|
+
if (fileStreams.length > 0) {
|
|
23992
|
+
if (this.options.isVerbose) {
|
|
23993
|
+
console.info('[🤰]', 'Uploading files to vector store', {
|
|
24153
23994
|
vectorStoreId,
|
|
24154
|
-
|
|
24155
|
-
|
|
24156
|
-
|
|
24157
|
-
|
|
23995
|
+
fileCount: fileStreams.length,
|
|
23996
|
+
totalBytes,
|
|
23997
|
+
maxConcurrency: this.getKnowledgeSourceUploadMaxConcurrency(),
|
|
23998
|
+
pollIntervalMs: this.getKnowledgeSourceUploadPollIntervalMs(),
|
|
23999
|
+
uploadTimeoutMs: this.getKnowledgeSourceUploadTimeoutMs(),
|
|
24158
24000
|
logLabel,
|
|
24159
24001
|
});
|
|
24160
|
-
loggedBatchIdMismatch = true;
|
|
24161
|
-
}
|
|
24162
|
-
if (countsKey !== lastProgressKey) {
|
|
24163
|
-
lastProgressKey = countsKey;
|
|
24164
|
-
lastProgressAtMs = nowMs;
|
|
24165
24002
|
}
|
|
24166
|
-
|
|
24167
|
-
|
|
24003
|
+
try {
|
|
24004
|
+
await this.uploadKnowledgeSourceFilesToVectorStore({
|
|
24005
|
+
client,
|
|
24168
24006
|
vectorStoreId,
|
|
24169
|
-
|
|
24170
|
-
|
|
24171
|
-
status: latestBatch.status,
|
|
24172
|
-
fileCounts: counts,
|
|
24173
|
-
elapsedMs: nowMs - pollStartedAtMs,
|
|
24007
|
+
files: fileStreams,
|
|
24008
|
+
totalBytes,
|
|
24174
24009
|
logLabel,
|
|
24175
24010
|
});
|
|
24176
|
-
// [🤰] If there are in-progress files for a long time, log their details
|
|
24177
|
-
if (counts.in_progress > 0 && nowMs - lastProgressAtMs > VECTOR_STORE_STALL_LOG_THRESHOLD_MS) {
|
|
24178
|
-
await this.logVectorStoreFileBatchDiagnostics({
|
|
24179
|
-
client,
|
|
24180
|
-
vectorStoreId,
|
|
24181
|
-
batchId: diagnosticsBatchId,
|
|
24182
|
-
uploadedFiles,
|
|
24183
|
-
logLabel,
|
|
24184
|
-
reason: 'stalled',
|
|
24185
|
-
});
|
|
24186
|
-
}
|
|
24187
|
-
lastStatus = latestBatch.status;
|
|
24188
|
-
lastCountsKey = countsKey;
|
|
24189
|
-
lastLogAtMs = nowMs;
|
|
24190
24011
|
}
|
|
24191
|
-
|
|
24192
|
-
|
|
24193
|
-
|
|
24194
|
-
await this.logVectorStoreFileBatchDiagnostics({
|
|
24195
|
-
client,
|
|
24012
|
+
catch (error) {
|
|
24013
|
+
assertsError(error);
|
|
24014
|
+
console.error('[🤰]', 'Error uploading files to vector store', {
|
|
24196
24015
|
vectorStoreId,
|
|
24197
|
-
batchId: diagnosticsBatchId,
|
|
24198
|
-
uploadedFiles,
|
|
24199
24016
|
logLabel,
|
|
24200
|
-
|
|
24017
|
+
error: serializeError(error),
|
|
24201
24018
|
});
|
|
24202
24019
|
}
|
|
24203
|
-
|
|
24204
|
-
|
|
24205
|
-
|
|
24206
|
-
|
|
24207
|
-
|
|
24208
|
-
|
|
24209
|
-
|
|
24210
|
-
|
|
24211
|
-
|
|
24212
|
-
|
|
24213
|
-
|
|
24214
|
-
|
|
24215
|
-
|
|
24216
|
-
|
|
24217
|
-
|
|
24218
|
-
|
|
24219
|
-
|
|
24220
|
-
|
|
24221
|
-
|
|
24222
|
-
|
|
24223
|
-
|
|
24224
|
-
|
|
24225
|
-
|
|
24226
|
-
|
|
24227
|
-
|
|
24228
|
-
|
|
24229
|
-
|
|
24230
|
-
|
|
24231
|
-
|
|
24232
|
-
|
|
24020
|
+
}
|
|
24021
|
+
else if (this.options.isVerbose) {
|
|
24022
|
+
console.info('[🤰]', 'No knowledge source files to upload', {
|
|
24023
|
+
vectorStoreId,
|
|
24024
|
+
skippedCount: skippedSources.length,
|
|
24025
|
+
logLabel,
|
|
24026
|
+
});
|
|
24027
|
+
}
|
|
24028
|
+
return {
|
|
24029
|
+
vectorStoreId,
|
|
24030
|
+
uploadedFileCount: fileStreams.length,
|
|
24031
|
+
skippedCount: skippedSources.length,
|
|
24032
|
+
totalBytes,
|
|
24033
|
+
};
|
|
24034
|
+
}
|
|
24035
|
+
}
|
|
24036
|
+
|
|
24037
|
+
const DEFAULT_AGENT_KIT_MODEL_NAME = 'gpt-5.2';
|
|
24038
|
+
/**
|
|
24039
|
+
* Execution tools for OpenAI AgentKit (Agents SDK).
|
|
24040
|
+
*
|
|
24041
|
+
* @public exported from `@promptbook/openai`
|
|
24042
|
+
*/
|
|
24043
|
+
class OpenAiAgentKitExecutionTools extends OpenAiVectorStoreHandler {
|
|
24044
|
+
/**
|
|
24045
|
+
* Creates OpenAI AgentKit execution tools.
|
|
24046
|
+
*/
|
|
24047
|
+
constructor(options) {
|
|
24048
|
+
var _a;
|
|
24049
|
+
if (options.isProxied) {
|
|
24050
|
+
throw new NotYetImplementedError(`Proxy mode is not yet implemented for OpenAI AgentKit`);
|
|
24051
|
+
}
|
|
24052
|
+
super(options);
|
|
24053
|
+
this.preparedAgentKitAgent = null;
|
|
24054
|
+
this.agentKitModelName = (_a = options.agentKitModelName) !== null && _a !== void 0 ? _a : DEFAULT_AGENT_KIT_MODEL_NAME;
|
|
24055
|
+
}
|
|
24056
|
+
get title() {
|
|
24057
|
+
return 'OpenAI AgentKit';
|
|
24058
|
+
}
|
|
24059
|
+
get description() {
|
|
24060
|
+
return 'Use OpenAI AgentKit for agent-style chat with tools and knowledge';
|
|
24061
|
+
}
|
|
24062
|
+
/**
|
|
24063
|
+
* Calls OpenAI AgentKit with a chat prompt (non-streaming).
|
|
24064
|
+
*/
|
|
24065
|
+
async callChatModel(prompt) {
|
|
24066
|
+
return this.callChatModelStream(prompt, () => { });
|
|
24067
|
+
}
|
|
24068
|
+
/**
|
|
24069
|
+
* Calls OpenAI AgentKit with a chat prompt (streaming).
|
|
24070
|
+
*/
|
|
24071
|
+
async callChatModelStream(prompt, onProgress) {
|
|
24072
|
+
const { content, parameters, modelRequirements } = prompt;
|
|
24073
|
+
if (modelRequirements.modelVariant !== 'CHAT') {
|
|
24074
|
+
throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
|
|
24075
|
+
}
|
|
24076
|
+
for (const key of ['maxTokens', 'modelName', 'seed', 'temperature']) {
|
|
24077
|
+
if (modelRequirements[key] !== undefined) {
|
|
24078
|
+
throw new NotYetImplementedError(`In \`OpenAiAgentKitExecutionTools\` you cannot specify \`${key}\``);
|
|
24079
|
+
}
|
|
24080
|
+
}
|
|
24081
|
+
const rawPromptContent = templateParameters(content, {
|
|
24082
|
+
...parameters,
|
|
24083
|
+
modelName: this.agentKitModelName,
|
|
24084
|
+
});
|
|
24085
|
+
const preparedAgentKitAgent = await this.prepareAgentKitAgent({
|
|
24086
|
+
name: (prompt.title || 'Agent'),
|
|
24087
|
+
instructions: modelRequirements.systemMessage || '',
|
|
24088
|
+
knowledgeSources: modelRequirements.knowledgeSources,
|
|
24089
|
+
tools: 'tools' in prompt && Array.isArray(prompt.tools) ? prompt.tools : modelRequirements.tools,
|
|
24090
|
+
});
|
|
24091
|
+
return this.callChatModelStreamWithPreparedAgent({
|
|
24092
|
+
openAiAgentKitAgent: preparedAgentKitAgent.agent,
|
|
24093
|
+
prompt,
|
|
24094
|
+
rawPromptContent,
|
|
24095
|
+
onProgress,
|
|
24096
|
+
});
|
|
24097
|
+
}
|
|
24098
|
+
/**
|
|
24099
|
+
* Returns a prepared AgentKit agent when the server wants to manage caching externally.
|
|
24100
|
+
*/
|
|
24101
|
+
getPreparedAgentKitAgent() {
|
|
24102
|
+
return this.preparedAgentKitAgent;
|
|
24103
|
+
}
|
|
24104
|
+
/**
|
|
24105
|
+
* Stores a prepared AgentKit agent for later reuse by external cache managers.
|
|
24106
|
+
*/
|
|
24107
|
+
setPreparedAgentKitAgent(preparedAgent) {
|
|
24108
|
+
this.preparedAgentKitAgent = preparedAgent;
|
|
24109
|
+
}
|
|
24110
|
+
/**
|
|
24111
|
+
* Creates a new tools instance bound to a prepared AgentKit agent.
|
|
24112
|
+
*/
|
|
24113
|
+
getPreparedAgentTools(preparedAgent) {
|
|
24114
|
+
const tools = new OpenAiAgentKitExecutionTools(this.agentKitOptions);
|
|
24115
|
+
tools.setPreparedAgentKitAgent(preparedAgent);
|
|
24116
|
+
return tools;
|
|
24117
|
+
}
|
|
24118
|
+
/**
|
|
24119
|
+
* Prepares an AgentKit agent with optional knowledge sources and tool definitions.
|
|
24120
|
+
*/
|
|
24121
|
+
async prepareAgentKitAgent(options) {
|
|
24122
|
+
var _a, _b;
|
|
24123
|
+
const { name, instructions, knowledgeSources, tools, vectorStoreId: cachedVectorStoreId, storeAsPrepared, } = options;
|
|
24124
|
+
await this.ensureAgentKitDefaults();
|
|
24125
|
+
if (this.options.isVerbose) {
|
|
24126
|
+
console.info('[🤰]', 'Preparing OpenAI AgentKit agent', {
|
|
24127
|
+
name,
|
|
24128
|
+
instructionsLength: instructions.length,
|
|
24129
|
+
knowledgeSourcesCount: (_a = knowledgeSources === null || knowledgeSources === void 0 ? void 0 : knowledgeSources.length) !== null && _a !== void 0 ? _a : 0,
|
|
24130
|
+
toolsCount: (_b = tools === null || tools === void 0 ? void 0 : tools.length) !== null && _b !== void 0 ? _b : 0,
|
|
24131
|
+
});
|
|
24132
|
+
}
|
|
24133
|
+
let vectorStoreId = cachedVectorStoreId;
|
|
24134
|
+
if (!vectorStoreId && knowledgeSources && knowledgeSources.length > 0) {
|
|
24135
|
+
const vectorStoreResult = await this.createVectorStoreWithKnowledgeSources({
|
|
24136
|
+
client: await this.getClient(),
|
|
24137
|
+
name,
|
|
24138
|
+
knowledgeSources,
|
|
24139
|
+
logLabel: 'agentkit preparation',
|
|
24140
|
+
});
|
|
24141
|
+
vectorStoreId = vectorStoreResult.vectorStoreId;
|
|
24142
|
+
}
|
|
24143
|
+
else if (vectorStoreId && this.options.isVerbose) {
|
|
24144
|
+
console.info('[🤰]', 'Using cached vector store for AgentKit agent', {
|
|
24145
|
+
name,
|
|
24146
|
+
vectorStoreId,
|
|
24147
|
+
});
|
|
24148
|
+
}
|
|
24149
|
+
const agentKitTools = this.buildAgentKitTools({ tools, vectorStoreId });
|
|
24150
|
+
const openAiAgentKitAgent = new Agent$1({
|
|
24151
|
+
name,
|
|
24152
|
+
model: this.agentKitModelName,
|
|
24153
|
+
instructions: instructions || 'You are a helpful assistant.',
|
|
24154
|
+
tools: agentKitTools,
|
|
24155
|
+
});
|
|
24156
|
+
const preparedAgent = {
|
|
24157
|
+
agent: openAiAgentKitAgent,
|
|
24158
|
+
vectorStoreId,
|
|
24159
|
+
};
|
|
24160
|
+
if (storeAsPrepared) {
|
|
24161
|
+
this.setPreparedAgentKitAgent(preparedAgent);
|
|
24162
|
+
}
|
|
24163
|
+
if (this.options.isVerbose) {
|
|
24164
|
+
console.info('[🤰]', 'OpenAI AgentKit agent ready', {
|
|
24165
|
+
name,
|
|
24166
|
+
model: this.agentKitModelName,
|
|
24167
|
+
toolCount: agentKitTools.length,
|
|
24168
|
+
hasVectorStore: Boolean(vectorStoreId),
|
|
24169
|
+
});
|
|
24170
|
+
}
|
|
24171
|
+
return preparedAgent;
|
|
24172
|
+
}
|
|
24173
|
+
/**
|
|
24174
|
+
* Ensures the AgentKit SDK is wired to the OpenAI client and API key.
|
|
24175
|
+
*/
|
|
24176
|
+
async ensureAgentKitDefaults() {
|
|
24177
|
+
const client = await this.getClient();
|
|
24178
|
+
setDefaultOpenAIClient(client);
|
|
24179
|
+
const apiKey = this.agentKitOptions.apiKey;
|
|
24180
|
+
if (apiKey && typeof apiKey === 'string') {
|
|
24181
|
+
setDefaultOpenAIKey(apiKey);
|
|
24182
|
+
}
|
|
24183
|
+
}
|
|
24184
|
+
/**
|
|
24185
|
+
* Builds the tool list for AgentKit, including hosted file search when applicable.
|
|
24186
|
+
*/
|
|
24187
|
+
buildAgentKitTools(options) {
|
|
24188
|
+
var _a;
|
|
24189
|
+
const { tools, vectorStoreId } = options;
|
|
24190
|
+
const agentKitTools = [];
|
|
24191
|
+
if (vectorStoreId) {
|
|
24192
|
+
agentKitTools.push(fileSearchTool(vectorStoreId));
|
|
24193
|
+
}
|
|
24194
|
+
if (tools && tools.length > 0) {
|
|
24195
|
+
const scriptTools = this.resolveScriptTools();
|
|
24196
|
+
for (const toolDefinition of tools) {
|
|
24197
|
+
agentKitTools.push(tool({
|
|
24198
|
+
name: toolDefinition.name,
|
|
24199
|
+
description: toolDefinition.description,
|
|
24200
|
+
parameters: toolDefinition.parameters
|
|
24201
|
+
? {
|
|
24202
|
+
...toolDefinition.parameters,
|
|
24203
|
+
additionalProperties: false,
|
|
24204
|
+
required: (_a = toolDefinition.parameters.required) !== null && _a !== void 0 ? _a : [],
|
|
24205
|
+
}
|
|
24206
|
+
: undefined,
|
|
24207
|
+
strict: false,
|
|
24208
|
+
execute: async (input, runContext, details) => {
|
|
24209
|
+
var _a, _b, _c;
|
|
24210
|
+
const scriptTool = scriptTools[0];
|
|
24211
|
+
const functionName = toolDefinition.name;
|
|
24212
|
+
const calledAt = $getCurrentDate();
|
|
24213
|
+
const callId = (_a = details === null || details === void 0 ? void 0 : details.toolCall) === null || _a === void 0 ? void 0 : _a.callId;
|
|
24214
|
+
const functionArgs = input !== null && input !== void 0 ? input : {};
|
|
24215
|
+
if (this.options.isVerbose) {
|
|
24216
|
+
console.info('[🤰]', 'Executing AgentKit tool', {
|
|
24217
|
+
functionName,
|
|
24218
|
+
callId,
|
|
24219
|
+
calledAt,
|
|
24220
|
+
});
|
|
24221
|
+
}
|
|
24222
|
+
try {
|
|
24223
|
+
return await scriptTool.execute({
|
|
24224
|
+
scriptLanguage: 'javascript',
|
|
24225
|
+
script: `
|
|
24226
|
+
const args = ${JSON.stringify(functionArgs)};
|
|
24227
|
+
return await ${functionName}(args);
|
|
24228
|
+
`,
|
|
24229
|
+
parameters: (_c = (_b = runContext === null || runContext === void 0 ? void 0 : runContext.context) === null || _b === void 0 ? void 0 : _b.parameters) !== null && _c !== void 0 ? _c : {},
|
|
24230
|
+
});
|
|
24231
|
+
}
|
|
24232
|
+
catch (error) {
|
|
24233
|
+
assertsError(error);
|
|
24234
|
+
const serializedError = serializeError(error);
|
|
24235
|
+
const errorMessage = spaceTrim$2((block) => `
|
|
24236
|
+
|
|
24237
|
+
The invoked tool \`${functionName}\` failed with error:
|
|
24238
|
+
|
|
24239
|
+
\`\`\`json
|
|
24240
|
+
${block(JSON.stringify(serializedError, null, 4))}
|
|
24241
|
+
\`\`\`
|
|
24242
|
+
|
|
24243
|
+
`);
|
|
24244
|
+
console.error('[🤰]', 'AgentKit tool execution failed', {
|
|
24245
|
+
functionName,
|
|
24246
|
+
callId,
|
|
24247
|
+
error: serializedError,
|
|
24248
|
+
});
|
|
24249
|
+
return errorMessage;
|
|
24250
|
+
}
|
|
24251
|
+
},
|
|
24252
|
+
}));
|
|
24233
24253
|
}
|
|
24234
|
-
|
|
24235
|
-
|
|
24236
|
-
|
|
24237
|
-
|
|
24238
|
-
|
|
24239
|
-
|
|
24240
|
-
|
|
24241
|
-
|
|
24242
|
-
|
|
24243
|
-
|
|
24244
|
-
|
|
24245
|
-
|
|
24246
|
-
|
|
24247
|
-
|
|
24248
|
-
|
|
24249
|
-
|
|
24250
|
-
|
|
24254
|
+
}
|
|
24255
|
+
return agentKitTools;
|
|
24256
|
+
}
|
|
24257
|
+
/**
|
|
24258
|
+
* Resolves the configured script tools for tool execution.
|
|
24259
|
+
*/
|
|
24260
|
+
resolveScriptTools() {
|
|
24261
|
+
const executionTools = this.options.executionTools;
|
|
24262
|
+
if (!executionTools || !executionTools.script) {
|
|
24263
|
+
throw new PipelineExecutionError(`Model requested tools but no executionTools.script were provided in OpenAiAgentKitExecutionTools options`);
|
|
24264
|
+
}
|
|
24265
|
+
return Array.isArray(executionTools.script) ? executionTools.script : [executionTools.script];
|
|
24266
|
+
}
|
|
24267
|
+
/**
|
|
24268
|
+
* Runs a prepared AgentKit agent and streams results back to the caller.
|
|
24269
|
+
*/
|
|
24270
|
+
async callChatModelStreamWithPreparedAgent(options) {
|
|
24271
|
+
var _a, _b, _c, _d;
|
|
24272
|
+
const { openAiAgentKitAgent, prompt, onProgress } = options;
|
|
24273
|
+
const rawPromptContent = (_a = options.rawPromptContent) !== null && _a !== void 0 ? _a : templateParameters(prompt.content, {
|
|
24274
|
+
...prompt.parameters,
|
|
24275
|
+
modelName: this.agentKitModelName,
|
|
24276
|
+
});
|
|
24277
|
+
const start = $getCurrentDate();
|
|
24278
|
+
let latestContent = '';
|
|
24279
|
+
const toolCalls = [];
|
|
24280
|
+
const toolCallIndexById = new Map();
|
|
24281
|
+
const inputItems = await this.buildAgentKitInputItems(prompt, rawPromptContent);
|
|
24282
|
+
const rawRequest = {
|
|
24283
|
+
agentName: openAiAgentKitAgent.name,
|
|
24284
|
+
input: inputItems,
|
|
24285
|
+
};
|
|
24286
|
+
const streamResult = await run(openAiAgentKitAgent, inputItems, {
|
|
24287
|
+
stream: true,
|
|
24288
|
+
context: { parameters: prompt.parameters },
|
|
24289
|
+
});
|
|
24290
|
+
for await (const event of streamResult) {
|
|
24291
|
+
if (event.type === 'raw_model_stream_event' && ((_b = event.data) === null || _b === void 0 ? void 0 : _b.type) === 'output_text_delta') {
|
|
24292
|
+
latestContent += event.data.delta;
|
|
24293
|
+
onProgress({
|
|
24294
|
+
content: latestContent,
|
|
24295
|
+
modelName: this.agentKitModelName,
|
|
24296
|
+
timing: { start, complete: $getCurrentDate() },
|
|
24297
|
+
usage: UNCERTAIN_USAGE,
|
|
24298
|
+
rawPromptContent: rawPromptContent,
|
|
24299
|
+
rawRequest: null,
|
|
24300
|
+
rawResponse: {},
|
|
24251
24301
|
});
|
|
24252
|
-
shouldPoll = false;
|
|
24253
24302
|
continue;
|
|
24254
24303
|
}
|
|
24255
|
-
if (
|
|
24256
|
-
|
|
24257
|
-
|
|
24258
|
-
|
|
24259
|
-
|
|
24260
|
-
|
|
24261
|
-
|
|
24262
|
-
|
|
24263
|
-
|
|
24264
|
-
|
|
24265
|
-
|
|
24266
|
-
|
|
24267
|
-
|
|
24268
|
-
|
|
24269
|
-
|
|
24270
|
-
|
|
24271
|
-
|
|
24272
|
-
|
|
24273
|
-
|
|
24274
|
-
|
|
24275
|
-
vectorStoreId,
|
|
24276
|
-
logLabel,
|
|
24304
|
+
if (event.type === 'run_item_stream_event') {
|
|
24305
|
+
const rawItem = (_c = event.item) === null || _c === void 0 ? void 0 : _c.rawItem;
|
|
24306
|
+
if (event.name === 'tool_called' && (rawItem === null || rawItem === void 0 ? void 0 : rawItem.type) === 'function_call') {
|
|
24307
|
+
const toolCall = {
|
|
24308
|
+
name: rawItem.name,
|
|
24309
|
+
arguments: rawItem.arguments,
|
|
24310
|
+
rawToolCall: rawItem,
|
|
24311
|
+
createdAt: $getCurrentDate(),
|
|
24312
|
+
};
|
|
24313
|
+
toolCallIndexById.set(rawItem.callId, toolCalls.length);
|
|
24314
|
+
toolCalls.push(toolCall);
|
|
24315
|
+
onProgress({
|
|
24316
|
+
content: latestContent,
|
|
24317
|
+
modelName: this.agentKitModelName,
|
|
24318
|
+
timing: { start, complete: $getCurrentDate() },
|
|
24319
|
+
usage: UNCERTAIN_USAGE,
|
|
24320
|
+
rawPromptContent: rawPromptContent,
|
|
24321
|
+
rawRequest: null,
|
|
24322
|
+
rawResponse: {},
|
|
24323
|
+
toolCalls: [toolCall],
|
|
24277
24324
|
});
|
|
24278
|
-
shouldPoll = false;
|
|
24279
|
-
continue;
|
|
24280
24325
|
}
|
|
24281
|
-
|
|
24282
|
-
const
|
|
24283
|
-
|
|
24284
|
-
|
|
24285
|
-
|
|
24286
|
-
|
|
24287
|
-
|
|
24288
|
-
|
|
24289
|
-
|
|
24290
|
-
|
|
24291
|
-
|
|
24292
|
-
|
|
24293
|
-
|
|
24294
|
-
|
|
24295
|
-
|
|
24296
|
-
|
|
24297
|
-
|
|
24298
|
-
|
|
24299
|
-
|
|
24300
|
-
|
|
24326
|
+
if (event.name === 'tool_output' && (rawItem === null || rawItem === void 0 ? void 0 : rawItem.type) === 'function_call_result') {
|
|
24327
|
+
const index = toolCallIndexById.get(rawItem.callId);
|
|
24328
|
+
const result = this.formatAgentKitToolOutput(rawItem.output);
|
|
24329
|
+
if (index !== undefined) {
|
|
24330
|
+
const existingToolCall = toolCalls[index];
|
|
24331
|
+
const completedToolCall = {
|
|
24332
|
+
...existingToolCall,
|
|
24333
|
+
result,
|
|
24334
|
+
rawToolCall: rawItem,
|
|
24335
|
+
};
|
|
24336
|
+
toolCalls[index] = completedToolCall;
|
|
24337
|
+
onProgress({
|
|
24338
|
+
content: latestContent,
|
|
24339
|
+
modelName: this.agentKitModelName,
|
|
24340
|
+
timing: { start, complete: $getCurrentDate() },
|
|
24341
|
+
usage: UNCERTAIN_USAGE,
|
|
24342
|
+
rawPromptContent: rawPromptContent,
|
|
24343
|
+
rawRequest: null,
|
|
24344
|
+
rawResponse: {},
|
|
24345
|
+
toolCalls: [completedToolCall],
|
|
24301
24346
|
});
|
|
24302
24347
|
}
|
|
24303
24348
|
}
|
|
24304
|
-
|
|
24305
|
-
|
|
24306
|
-
|
|
24307
|
-
|
|
24308
|
-
|
|
24309
|
-
|
|
24310
|
-
|
|
24311
|
-
|
|
24349
|
+
}
|
|
24350
|
+
}
|
|
24351
|
+
await streamResult.completed;
|
|
24352
|
+
const complete = $getCurrentDate();
|
|
24353
|
+
const finalContent = ((_d = streamResult.finalOutput) !== null && _d !== void 0 ? _d : latestContent);
|
|
24354
|
+
const finalResult = {
|
|
24355
|
+
content: finalContent,
|
|
24356
|
+
modelName: this.agentKitModelName,
|
|
24357
|
+
timing: { start, complete },
|
|
24358
|
+
usage: UNCERTAIN_USAGE,
|
|
24359
|
+
rawPromptContent: rawPromptContent,
|
|
24360
|
+
rawRequest,
|
|
24361
|
+
rawResponse: { runResult: streamResult },
|
|
24362
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
24363
|
+
};
|
|
24364
|
+
onProgress(finalResult);
|
|
24365
|
+
return finalResult;
|
|
24366
|
+
}
|
|
24367
|
+
/**
|
|
24368
|
+
* Builds AgentKit input items from the prompt and optional thread.
|
|
24369
|
+
*/
|
|
24370
|
+
async buildAgentKitInputItems(prompt, rawPromptContent) {
|
|
24371
|
+
var _a;
|
|
24372
|
+
const inputItems = [];
|
|
24373
|
+
if ('thread' in prompt && Array.isArray(prompt.thread)) {
|
|
24374
|
+
for (const message of prompt.thread) {
|
|
24375
|
+
const sender = message.sender;
|
|
24376
|
+
const content = (_a = message.content) !== null && _a !== void 0 ? _a : '';
|
|
24377
|
+
if (sender === 'assistant' || sender === 'agent') {
|
|
24378
|
+
inputItems.push({
|
|
24379
|
+
role: 'assistant',
|
|
24380
|
+
status: 'completed',
|
|
24381
|
+
content: [{ type: 'output_text', text: content }],
|
|
24382
|
+
});
|
|
24383
|
+
}
|
|
24384
|
+
else {
|
|
24385
|
+
inputItems.push({
|
|
24386
|
+
role: 'user',
|
|
24387
|
+
content,
|
|
24312
24388
|
});
|
|
24313
24389
|
}
|
|
24314
|
-
shouldPoll = false;
|
|
24315
|
-
continue;
|
|
24316
24390
|
}
|
|
24317
|
-
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
24318
24391
|
}
|
|
24319
|
-
|
|
24392
|
+
const userContent = await this.buildAgentKitUserContent(prompt, rawPromptContent);
|
|
24393
|
+
inputItems.push({
|
|
24394
|
+
role: 'user',
|
|
24395
|
+
content: userContent,
|
|
24396
|
+
});
|
|
24397
|
+
return inputItems;
|
|
24320
24398
|
}
|
|
24321
24399
|
/**
|
|
24322
|
-
*
|
|
24400
|
+
* Builds the user message content for AgentKit runs, including file inputs when provided.
|
|
24323
24401
|
*/
|
|
24324
|
-
async
|
|
24325
|
-
|
|
24326
|
-
|
|
24327
|
-
|
|
24402
|
+
async buildAgentKitUserContent(prompt, rawPromptContent) {
|
|
24403
|
+
if ('files' in prompt && Array.isArray(prompt.files) && prompt.files.length > 0) {
|
|
24404
|
+
const fileItems = await Promise.all(prompt.files.map(async (file) => {
|
|
24405
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
24406
|
+
const base64 = Buffer.from(arrayBuffer).toString('base64');
|
|
24407
|
+
return {
|
|
24408
|
+
type: 'input_image',
|
|
24409
|
+
image: `data:${file.type};base64,${base64}`,
|
|
24410
|
+
};
|
|
24411
|
+
}));
|
|
24412
|
+
return [{ type: 'input_text', text: rawPromptContent }, ...fileItems];
|
|
24413
|
+
}
|
|
24414
|
+
return rawPromptContent;
|
|
24415
|
+
}
|
|
24416
|
+
/**
|
|
24417
|
+
* Normalizes AgentKit tool outputs into a string for Promptbook tool call results.
|
|
24418
|
+
*/
|
|
24419
|
+
formatAgentKitToolOutput(output) {
|
|
24420
|
+
if (typeof output === 'string') {
|
|
24421
|
+
return output;
|
|
24422
|
+
}
|
|
24423
|
+
if (output && typeof output === 'object') {
|
|
24424
|
+
const textOutput = output;
|
|
24425
|
+
if (textOutput.type === 'text' && typeof textOutput.text === 'string') {
|
|
24426
|
+
return textOutput.text;
|
|
24427
|
+
}
|
|
24428
|
+
}
|
|
24429
|
+
return JSON.stringify(output !== null && output !== void 0 ? output : null);
|
|
24430
|
+
}
|
|
24431
|
+
/**
|
|
24432
|
+
* Returns AgentKit-specific options.
|
|
24433
|
+
*/
|
|
24434
|
+
get agentKitOptions() {
|
|
24435
|
+
return this.options;
|
|
24436
|
+
}
|
|
24437
|
+
/**
|
|
24438
|
+
* Discriminant for type guards.
|
|
24439
|
+
*/
|
|
24440
|
+
get discriminant() {
|
|
24441
|
+
return DISCRIMINANT$1;
|
|
24442
|
+
}
|
|
24443
|
+
/**
|
|
24444
|
+
* Type guard to check if given `LlmExecutionTools` are instanceof `OpenAiAgentKitExecutionTools`.
|
|
24445
|
+
*/
|
|
24446
|
+
static isOpenAiAgentKitExecutionTools(llmExecutionTools) {
|
|
24447
|
+
return llmExecutionTools.discriminant === DISCRIMINANT$1;
|
|
24448
|
+
}
|
|
24449
|
+
}
|
|
24450
|
+
/**
|
|
24451
|
+
* Discriminant for type guards.
|
|
24452
|
+
*
|
|
24453
|
+
* @private const of `OpenAiAgentKitExecutionTools`
|
|
24454
|
+
*/
|
|
24455
|
+
const DISCRIMINANT$1 = 'OPEN_AI_AGENT_KIT_V1';
|
|
24456
|
+
|
|
24457
|
+
/**
|
|
24458
|
+
* Uploads files to OpenAI and returns their IDs
|
|
24459
|
+
*
|
|
24460
|
+
* @private utility for `OpenAiAssistantExecutionTools` and `OpenAiCompatibleExecutionTools`
|
|
24461
|
+
*/
|
|
24462
|
+
async function uploadFilesToOpenAi(client, files) {
|
|
24463
|
+
const fileIds = [];
|
|
24464
|
+
for (const file of files) {
|
|
24465
|
+
// Note: OpenAI API expects a File object or a ReadStream
|
|
24466
|
+
// In browser environment, we can pass the File object directly
|
|
24467
|
+
// In Node.js environment, we might need to convert it or use a different approach
|
|
24468
|
+
// But since `Prompt.files` already contains `File` objects, we try to pass them directly
|
|
24469
|
+
const uploadedFile = await client.files.create({
|
|
24470
|
+
file: file,
|
|
24471
|
+
purpose: 'assistants',
|
|
24472
|
+
});
|
|
24473
|
+
fileIds.push(uploadedFile.id);
|
|
24474
|
+
}
|
|
24475
|
+
return fileIds;
|
|
24476
|
+
}
|
|
24477
|
+
|
|
24478
|
+
/**
|
|
24479
|
+
* Execution Tools for calling OpenAI API Assistants
|
|
24480
|
+
*
|
|
24481
|
+
* This is useful for calling OpenAI API with a single assistant, for more wide usage use `OpenAiExecutionTools`.
|
|
24482
|
+
*
|
|
24483
|
+
* Note: [🦖] There are several different things in Promptbook:
|
|
24484
|
+
* - `Agent` - which represents an AI Agent with its source, memories, actions, etc. Agent is a higher-level abstraction which is internally using:
|
|
24485
|
+
* - `LlmExecutionTools` - which wraps one or more LLM models and provides an interface to execute them
|
|
24486
|
+
* - `AgentLlmExecutionTools` - which is a specific implementation of `LlmExecutionTools` that wraps another LlmExecutionTools and applies agent-specific system prompts and requirements
|
|
24487
|
+
* - `OpenAiAssistantExecutionTools` - which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities, recommended for usage in `Agent` or `AgentLlmExecutionTools`
|
|
24488
|
+
* - `RemoteAgent` - which is an `Agent` that connects to a Promptbook Agents Server
|
|
24489
|
+
*
|
|
24490
|
+
* @deprecated Use `OpenAiAgentKitExecutionTools` instead.
|
|
24491
|
+
* @public exported from `@promptbook/openai`
|
|
24492
|
+
*/
|
|
24493
|
+
class OpenAiAssistantExecutionTools extends OpenAiVectorStoreHandler {
|
|
24494
|
+
/**
|
|
24495
|
+
* Creates OpenAI Execution Tools.
|
|
24496
|
+
*
|
|
24497
|
+
* @param options which are relevant are directly passed to the OpenAI client
|
|
24498
|
+
*/
|
|
24499
|
+
constructor(options) {
|
|
24500
|
+
var _a;
|
|
24501
|
+
if (options.isProxied) {
|
|
24502
|
+
throw new NotYetImplementedError(`Proxy mode is not yet implemented for OpenAI assistants`);
|
|
24503
|
+
}
|
|
24504
|
+
super(options);
|
|
24505
|
+
this.isCreatingNewAssistantsAllowed = false;
|
|
24506
|
+
this.assistantId = options.assistantId;
|
|
24507
|
+
this.isCreatingNewAssistantsAllowed = (_a = options.isCreatingNewAssistantsAllowed) !== null && _a !== void 0 ? _a : false;
|
|
24508
|
+
if (this.assistantId === null && !this.isCreatingNewAssistantsAllowed) {
|
|
24509
|
+
throw new NotAllowed(`Assistant ID is null and creating new assistants is not allowed - this configuration does not make sense`);
|
|
24510
|
+
}
|
|
24511
|
+
// <- TODO: !!! `OpenAiAssistantExecutionToolsOptions` - Allow `assistantId: null` together with `isCreatingNewAssistantsAllowed: true`
|
|
24512
|
+
// TODO: [👱] Make limiter same as in `OpenAiExecutionTools`
|
|
24513
|
+
}
|
|
24514
|
+
get title() {
|
|
24515
|
+
return 'OpenAI Assistant';
|
|
24516
|
+
}
|
|
24517
|
+
get description() {
|
|
24518
|
+
return 'Use single assistant provided by OpenAI';
|
|
24519
|
+
}
|
|
24520
|
+
/**
|
|
24521
|
+
* Calls OpenAI API to use a chat model.
|
|
24522
|
+
*/
|
|
24523
|
+
async callChatModel(prompt) {
|
|
24524
|
+
return this.callChatModelStream(prompt, () => { });
|
|
24525
|
+
}
|
|
24526
|
+
/**
|
|
24527
|
+
* Calls OpenAI API to use a chat model with streaming.
|
|
24528
|
+
*/
|
|
24529
|
+
async callChatModelStream(prompt, onProgress) {
|
|
24530
|
+
var _a, _b, _c, _d, _e, _f;
|
|
24328
24531
|
if (this.options.isVerbose) {
|
|
24329
|
-
console.info('
|
|
24330
|
-
|
|
24331
|
-
|
|
24332
|
-
|
|
24333
|
-
|
|
24334
|
-
|
|
24532
|
+
console.info('💬 OpenAI callChatModel call', { prompt });
|
|
24533
|
+
}
|
|
24534
|
+
const { content, parameters, modelRequirements /*, format*/ } = prompt;
|
|
24535
|
+
const client = await this.getClient();
|
|
24536
|
+
// TODO: [☂] Use here more modelRequirements
|
|
24537
|
+
if (modelRequirements.modelVariant !== 'CHAT') {
|
|
24538
|
+
throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
|
|
24335
24539
|
}
|
|
24336
|
-
|
|
24337
|
-
|
|
24540
|
+
// TODO: [👨👨👧👧] Remove:
|
|
24541
|
+
for (const key of ['maxTokens', 'modelName', 'seed', 'temperature']) {
|
|
24542
|
+
if (modelRequirements[key] !== undefined) {
|
|
24543
|
+
throw new NotYetImplementedError(`In \`OpenAiAssistantExecutionTools\` you cannot specify \`${key}\``);
|
|
24544
|
+
}
|
|
24545
|
+
}
|
|
24546
|
+
/*
|
|
24547
|
+
TODO: [👨👨👧👧] Implement all of this for Assistants
|
|
24548
|
+
const modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
|
|
24549
|
+
const modelSettings = {
|
|
24550
|
+
model: modelName,
|
|
24551
|
+
|
|
24552
|
+
temperature: modelRequirements.temperature,
|
|
24553
|
+
|
|
24554
|
+
// <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
|
|
24555
|
+
// <- Note: [🧆]
|
|
24556
|
+
} as OpenAI.Chat.Completions.CompletionCreateParamsNonStreaming; // <- TODO: Guard here types better
|
|
24557
|
+
|
|
24558
|
+
if (format === 'JSON') {
|
|
24559
|
+
modelSettings.response_format = {
|
|
24560
|
+
type: 'json_object',
|
|
24561
|
+
};
|
|
24562
|
+
}
|
|
24563
|
+
*/
|
|
24564
|
+
// <- TODO: [🚸] Not all models are compatible with JSON mode
|
|
24565
|
+
// > 'response_format' of type 'json_object' is not supported with this model.
|
|
24566
|
+
const rawPromptContent = templateParameters(content, {
|
|
24567
|
+
...parameters,
|
|
24568
|
+
modelName: 'assistant',
|
|
24569
|
+
// <- [🧠] What is the best value here
|
|
24338
24570
|
});
|
|
24339
|
-
|
|
24340
|
-
|
|
24341
|
-
|
|
24342
|
-
|
|
24343
|
-
|
|
24344
|
-
|
|
24571
|
+
// Build thread messages: include previous thread messages + current user message
|
|
24572
|
+
const threadMessages = [];
|
|
24573
|
+
// TODO: [🈹] Maybe this should not be here but in other place, look at commit 39d705e75e5bcf7a818c3af36bc13e1c8475c30c
|
|
24574
|
+
// Add previous messages from thread (if any)
|
|
24575
|
+
if ('thread' in prompt && Array.isArray(prompt.thread)) {
|
|
24576
|
+
const previousMessages = prompt.thread.map((msg) => ({
|
|
24577
|
+
role: (msg.sender === 'assistant' ? 'assistant' : 'user'),
|
|
24578
|
+
content: msg.content,
|
|
24579
|
+
}));
|
|
24580
|
+
threadMessages.push(...previousMessages);
|
|
24345
24581
|
}
|
|
24346
|
-
|
|
24347
|
-
const
|
|
24348
|
-
|
|
24349
|
-
|
|
24350
|
-
|
|
24351
|
-
|
|
24352
|
-
|
|
24353
|
-
|
|
24354
|
-
|
|
24355
|
-
|
|
24356
|
-
|
|
24357
|
-
|
|
24358
|
-
|
|
24359
|
-
|
|
24360
|
-
|
|
24361
|
-
|
|
24362
|
-
|
|
24363
|
-
|
|
24364
|
-
|
|
24365
|
-
|
|
24366
|
-
|
|
24367
|
-
|
|
24368
|
-
|
|
24369
|
-
|
|
24370
|
-
|
|
24371
|
-
|
|
24372
|
-
|
|
24373
|
-
|
|
24374
|
-
|
|
24582
|
+
// Always add the current user message
|
|
24583
|
+
const currentUserMessage = {
|
|
24584
|
+
role: 'user',
|
|
24585
|
+
content: rawPromptContent,
|
|
24586
|
+
};
|
|
24587
|
+
if ('files' in prompt && Array.isArray(prompt.files) && prompt.files.length > 0) {
|
|
24588
|
+
const fileIds = await uploadFilesToOpenAi(client, prompt.files);
|
|
24589
|
+
currentUserMessage.attachments = fileIds.map((fileId) => ({
|
|
24590
|
+
file_id: fileId,
|
|
24591
|
+
tools: [{ type: 'file_search' }, { type: 'code_interpreter' }],
|
|
24592
|
+
}));
|
|
24593
|
+
}
|
|
24594
|
+
threadMessages.push(currentUserMessage);
|
|
24595
|
+
// Check if tools are being used - if so, use non-streaming mode
|
|
24596
|
+
const hasTools = modelRequirements.tools !== undefined && modelRequirements.tools.length > 0;
|
|
24597
|
+
const start = $getCurrentDate();
|
|
24598
|
+
let complete;
|
|
24599
|
+
// [🐱🚀] When tools are present, we need to use the non-streaming Runs API
|
|
24600
|
+
// because streaming doesn't support tool execution flow properly
|
|
24601
|
+
if (hasTools) {
|
|
24602
|
+
onProgress({
|
|
24603
|
+
content: '',
|
|
24604
|
+
modelName: 'assistant',
|
|
24605
|
+
timing: { start, complete: $getCurrentDate() },
|
|
24606
|
+
usage: UNCERTAIN_USAGE,
|
|
24607
|
+
rawPromptContent,
|
|
24608
|
+
rawRequest: null,
|
|
24609
|
+
rawResponse: null,
|
|
24610
|
+
});
|
|
24611
|
+
const rawRequest = {
|
|
24612
|
+
assistant_id: this.assistantId,
|
|
24613
|
+
thread: {
|
|
24614
|
+
messages: threadMessages,
|
|
24615
|
+
},
|
|
24616
|
+
tools: mapToolsToOpenAi(modelRequirements.tools),
|
|
24617
|
+
};
|
|
24618
|
+
if (this.options.isVerbose) {
|
|
24619
|
+
console.info(colors.bgWhite('rawRequest (non-streaming with tools)'), JSON.stringify(rawRequest, null, 4));
|
|
24620
|
+
}
|
|
24621
|
+
// Create thread and run
|
|
24622
|
+
let run = (await client.beta.threads.createAndRun(rawRequest));
|
|
24623
|
+
const completedToolCalls = [];
|
|
24624
|
+
const toolCallStartedAt = new Map();
|
|
24625
|
+
// Poll until run completes or requires action
|
|
24626
|
+
while (run.status === 'queued' || run.status === 'in_progress' || run.status === 'requires_action') {
|
|
24627
|
+
if (run.status === 'requires_action' && ((_a = run.required_action) === null || _a === void 0 ? void 0 : _a.type) === 'submit_tool_outputs') {
|
|
24628
|
+
// Execute tools
|
|
24629
|
+
const toolCalls = run.required_action.submit_tool_outputs.tool_calls;
|
|
24630
|
+
const toolOutputs = [];
|
|
24631
|
+
for (const toolCall of toolCalls) {
|
|
24632
|
+
if (toolCall.type === 'function') {
|
|
24633
|
+
const functionName = toolCall.function.name;
|
|
24634
|
+
const functionArgs = JSON.parse(toolCall.function.arguments);
|
|
24635
|
+
const calledAt = $getCurrentDate();
|
|
24636
|
+
if (toolCall.id) {
|
|
24637
|
+
toolCallStartedAt.set(toolCall.id, calledAt);
|
|
24638
|
+
}
|
|
24639
|
+
onProgress({
|
|
24640
|
+
content: '',
|
|
24641
|
+
modelName: 'assistant',
|
|
24642
|
+
timing: { start, complete: $getCurrentDate() },
|
|
24643
|
+
usage: UNCERTAIN_USAGE,
|
|
24644
|
+
rawPromptContent,
|
|
24645
|
+
rawRequest: null,
|
|
24646
|
+
rawResponse: null,
|
|
24647
|
+
toolCalls: [
|
|
24648
|
+
{
|
|
24649
|
+
name: functionName,
|
|
24650
|
+
arguments: toolCall.function.arguments,
|
|
24651
|
+
result: '',
|
|
24652
|
+
rawToolCall: toolCall,
|
|
24653
|
+
createdAt: calledAt,
|
|
24654
|
+
},
|
|
24655
|
+
],
|
|
24656
|
+
});
|
|
24657
|
+
if (this.options.isVerbose) {
|
|
24658
|
+
console.info(`🔧 Executing tool: ${functionName}`, functionArgs);
|
|
24659
|
+
}
|
|
24660
|
+
// Get execution tools for script execution
|
|
24661
|
+
const executionTools = this.options
|
|
24662
|
+
.executionTools;
|
|
24663
|
+
if (!executionTools || !executionTools.script) {
|
|
24664
|
+
throw new PipelineExecutionError(`Model requested tool '${functionName}' but no executionTools.script were provided in OpenAiAssistantExecutionTools options`);
|
|
24665
|
+
}
|
|
24666
|
+
// TODO: [DRY] Use some common tool caller (similar to OpenAiCompatibleExecutionTools)
|
|
24667
|
+
const scriptTools = Array.isArray(executionTools.script)
|
|
24668
|
+
? executionTools.script
|
|
24669
|
+
: [executionTools.script];
|
|
24670
|
+
let functionResponse;
|
|
24671
|
+
let errors;
|
|
24672
|
+
try {
|
|
24673
|
+
const scriptTool = scriptTools[0]; // <- TODO: [🧠] Which script tool to use?
|
|
24674
|
+
functionResponse = await scriptTool.execute({
|
|
24675
|
+
scriptLanguage: 'javascript',
|
|
24676
|
+
script: `
|
|
24677
|
+
const args = ${JSON.stringify(functionArgs)};
|
|
24678
|
+
return await ${functionName}(args);
|
|
24679
|
+
`,
|
|
24680
|
+
parameters: prompt.parameters,
|
|
24681
|
+
});
|
|
24682
|
+
if (this.options.isVerbose) {
|
|
24683
|
+
console.info(`✅ Tool ${functionName} executed:`, functionResponse);
|
|
24684
|
+
}
|
|
24685
|
+
}
|
|
24686
|
+
catch (error) {
|
|
24687
|
+
assertsError(error);
|
|
24688
|
+
const serializedError = serializeError(error);
|
|
24689
|
+
errors = [serializedError];
|
|
24690
|
+
functionResponse = spaceTrim$2((block) => `
|
|
24691
|
+
|
|
24692
|
+
The invoked tool \`${functionName}\` failed with error:
|
|
24693
|
+
|
|
24694
|
+
\`\`\`json
|
|
24695
|
+
${block(JSON.stringify(serializedError, null, 4))}
|
|
24696
|
+
\`\`\`
|
|
24697
|
+
|
|
24698
|
+
`);
|
|
24699
|
+
console.error(colors.bgRed(`❌ Error executing tool ${functionName}:`));
|
|
24700
|
+
console.error(error);
|
|
24701
|
+
}
|
|
24702
|
+
toolOutputs.push({
|
|
24703
|
+
tool_call_id: toolCall.id,
|
|
24704
|
+
output: functionResponse,
|
|
24705
|
+
});
|
|
24706
|
+
completedToolCalls.push({
|
|
24707
|
+
name: functionName,
|
|
24708
|
+
arguments: toolCall.function.arguments,
|
|
24709
|
+
result: functionResponse,
|
|
24710
|
+
rawToolCall: toolCall,
|
|
24711
|
+
createdAt: toolCall.id ? toolCallStartedAt.get(toolCall.id) || calledAt : calledAt,
|
|
24712
|
+
errors,
|
|
24713
|
+
});
|
|
24714
|
+
}
|
|
24375
24715
|
}
|
|
24716
|
+
// Submit tool outputs
|
|
24717
|
+
run = (await client.beta.threads.runs.submitToolOutputs(run.thread_id, run.id, {
|
|
24718
|
+
tool_outputs: toolOutputs,
|
|
24719
|
+
}));
|
|
24376
24720
|
}
|
|
24377
24721
|
else {
|
|
24378
|
-
|
|
24379
|
-
|
|
24380
|
-
|
|
24381
|
-
source,
|
|
24382
|
-
sourceType,
|
|
24383
|
-
logLabel,
|
|
24384
|
-
});
|
|
24385
|
-
}
|
|
24386
|
-
/*
|
|
24387
|
-
TODO: [?????] Resolve problem with browser environment
|
|
24388
|
-
// Assume it's a local file path
|
|
24389
|
-
// Note: This will work in Node.js environment
|
|
24390
|
-
// For browser environments, this would need different handling
|
|
24391
|
-
const fs = await import('fs');
|
|
24392
|
-
const fileStream = fs.createReadStream(source);
|
|
24393
|
-
fileStreams.push(fileStream);
|
|
24394
|
-
*/
|
|
24722
|
+
// Wait a bit before polling again
|
|
24723
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
24724
|
+
run = (await client.beta.threads.runs.retrieve(run.thread_id, run.id));
|
|
24395
24725
|
}
|
|
24396
24726
|
}
|
|
24397
|
-
|
|
24398
|
-
|
|
24399
|
-
|
|
24400
|
-
|
|
24401
|
-
|
|
24402
|
-
|
|
24403
|
-
|
|
24404
|
-
|
|
24727
|
+
if (run.status !== 'completed') {
|
|
24728
|
+
throw new PipelineExecutionError(`Assistant run failed with status: ${run.status}`);
|
|
24729
|
+
}
|
|
24730
|
+
// Get messages from the thread
|
|
24731
|
+
const messages = await client.beta.threads.messages.list(run.thread_id);
|
|
24732
|
+
const assistantMessages = messages.data.filter((msg) => msg.role === 'assistant');
|
|
24733
|
+
if (assistantMessages.length === 0) {
|
|
24734
|
+
throw new PipelineExecutionError('No assistant messages found after run completion');
|
|
24735
|
+
}
|
|
24736
|
+
const lastMessage = assistantMessages[0];
|
|
24737
|
+
const textContent = lastMessage.content.find((c) => c.type === 'text');
|
|
24738
|
+
if (!textContent || textContent.type !== 'text') {
|
|
24739
|
+
throw new PipelineExecutionError('No text content in assistant response');
|
|
24405
24740
|
}
|
|
24741
|
+
complete = $getCurrentDate();
|
|
24742
|
+
const resultContent = textContent.text.value;
|
|
24743
|
+
const usage = UNCERTAIN_USAGE;
|
|
24744
|
+
// Progress callback with final result
|
|
24745
|
+
const finalChunk = {
|
|
24746
|
+
content: resultContent,
|
|
24747
|
+
modelName: 'assistant',
|
|
24748
|
+
timing: { start, complete },
|
|
24749
|
+
usage,
|
|
24750
|
+
rawPromptContent,
|
|
24751
|
+
rawRequest,
|
|
24752
|
+
rawResponse: { run, messages: messages.data },
|
|
24753
|
+
toolCalls: completedToolCalls.length > 0 ? completedToolCalls : undefined,
|
|
24754
|
+
};
|
|
24755
|
+
onProgress(finalChunk);
|
|
24756
|
+
return exportJson({
|
|
24757
|
+
name: 'promptResult',
|
|
24758
|
+
message: `Result of \`OpenAiAssistantExecutionTools.callChatModelStream\` (with tools)`,
|
|
24759
|
+
order: [],
|
|
24760
|
+
value: finalChunk,
|
|
24761
|
+
});
|
|
24406
24762
|
}
|
|
24763
|
+
// Streaming mode (without tools)
|
|
24764
|
+
const rawRequest = {
|
|
24765
|
+
// TODO: [👨👨👧👧] ...modelSettings,
|
|
24766
|
+
// TODO: [👨👨👧👧][🧠] What about system message for assistants, does it make sense - combination of OpenAI assistants with Promptbook Personas
|
|
24767
|
+
assistant_id: this.assistantId,
|
|
24768
|
+
thread: {
|
|
24769
|
+
messages: threadMessages,
|
|
24770
|
+
},
|
|
24771
|
+
tools: modelRequirements.tools === undefined ? undefined : mapToolsToOpenAi(modelRequirements.tools),
|
|
24772
|
+
// <- TODO: Add user identification here> user: this.options.user,
|
|
24773
|
+
};
|
|
24407
24774
|
if (this.options.isVerbose) {
|
|
24408
|
-
console.info('
|
|
24409
|
-
total: knowledgeSourcesCount,
|
|
24410
|
-
downloadedCount: fileStreams.length,
|
|
24411
|
-
skippedCount: skippedSources.length,
|
|
24412
|
-
totalBytes,
|
|
24413
|
-
elapsedMs: Date.now() - processingStartedAtMs,
|
|
24414
|
-
skippedSamples: skippedSources.slice(0, 3),
|
|
24415
|
-
logLabel,
|
|
24416
|
-
});
|
|
24775
|
+
console.info(colors.bgWhite('rawRequest (streaming)'), JSON.stringify(rawRequest, null, 4));
|
|
24417
24776
|
}
|
|
24418
|
-
|
|
24777
|
+
const stream = await client.beta.threads.createAndRunStream(rawRequest);
|
|
24778
|
+
stream.on('connect', () => {
|
|
24419
24779
|
if (this.options.isVerbose) {
|
|
24420
|
-
console.info('
|
|
24421
|
-
vectorStoreId,
|
|
24422
|
-
fileCount: fileStreams.length,
|
|
24423
|
-
totalBytes,
|
|
24424
|
-
maxConcurrency: this.getKnowledgeSourceUploadMaxConcurrency(),
|
|
24425
|
-
pollIntervalMs: this.getKnowledgeSourceUploadPollIntervalMs(),
|
|
24426
|
-
uploadTimeoutMs: this.getKnowledgeSourceUploadTimeoutMs(),
|
|
24427
|
-
logLabel,
|
|
24428
|
-
});
|
|
24780
|
+
console.info('connect', stream.currentEvent);
|
|
24429
24781
|
}
|
|
24430
|
-
|
|
24431
|
-
|
|
24432
|
-
|
|
24433
|
-
|
|
24434
|
-
files: fileStreams,
|
|
24435
|
-
totalBytes,
|
|
24436
|
-
logLabel,
|
|
24437
|
-
});
|
|
24782
|
+
});
|
|
24783
|
+
stream.on('textDelta', (textDelta, snapshot) => {
|
|
24784
|
+
if (this.options.isVerbose && textDelta.value) {
|
|
24785
|
+
console.info('textDelta', textDelta.value);
|
|
24438
24786
|
}
|
|
24439
|
-
|
|
24440
|
-
|
|
24441
|
-
|
|
24442
|
-
|
|
24443
|
-
|
|
24444
|
-
|
|
24445
|
-
}
|
|
24787
|
+
const chunk = {
|
|
24788
|
+
content: snapshot.value,
|
|
24789
|
+
modelName: 'assistant',
|
|
24790
|
+
timing: {
|
|
24791
|
+
start,
|
|
24792
|
+
complete: $getCurrentDate(),
|
|
24793
|
+
},
|
|
24794
|
+
usage: UNCERTAIN_USAGE,
|
|
24795
|
+
rawPromptContent,
|
|
24796
|
+
rawRequest,
|
|
24797
|
+
rawResponse: snapshot,
|
|
24798
|
+
};
|
|
24799
|
+
onProgress(chunk);
|
|
24800
|
+
});
|
|
24801
|
+
stream.on('messageCreated', (message) => {
|
|
24802
|
+
if (this.options.isVerbose) {
|
|
24803
|
+
console.info('messageCreated', message);
|
|
24804
|
+
}
|
|
24805
|
+
});
|
|
24806
|
+
stream.on('messageDone', (message) => {
|
|
24807
|
+
if (this.options.isVerbose) {
|
|
24808
|
+
console.info('messageDone', message);
|
|
24446
24809
|
}
|
|
24810
|
+
});
|
|
24811
|
+
// TODO: [🐱🚀] Handle tool calls in assistants
|
|
24812
|
+
// Note: OpenAI Assistant streaming with tool calls requires special handling.
|
|
24813
|
+
// The stream will pause when a tool call is needed, and we need to:
|
|
24814
|
+
// 1. Wait for the run to reach 'requires_action' status
|
|
24815
|
+
// 2. Execute the tool calls
|
|
24816
|
+
// 3. Submit tool outputs via a separate API call (not on the stream)
|
|
24817
|
+
// 4. Continue the run
|
|
24818
|
+
// This requires switching to non-streaming mode or using the Runs API directly.
|
|
24819
|
+
// For now, tools with assistants should use the non-streaming chat completions API instead.
|
|
24820
|
+
const rawResponse = await stream.finalMessages();
|
|
24821
|
+
if (this.options.isVerbose) {
|
|
24822
|
+
console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
|
|
24447
24823
|
}
|
|
24448
|
-
|
|
24449
|
-
|
|
24450
|
-
vectorStoreId,
|
|
24451
|
-
skippedCount: skippedSources.length,
|
|
24452
|
-
logLabel,
|
|
24453
|
-
});
|
|
24824
|
+
if (rawResponse.length !== 1) {
|
|
24825
|
+
throw new PipelineExecutionError(`There is NOT 1 BUT ${rawResponse.length} finalMessages from OpenAI`);
|
|
24454
24826
|
}
|
|
24455
|
-
|
|
24456
|
-
|
|
24457
|
-
|
|
24458
|
-
|
|
24459
|
-
|
|
24460
|
-
}
|
|
24827
|
+
if (rawResponse[0].content.length !== 1) {
|
|
24828
|
+
throw new PipelineExecutionError(`There is NOT 1 BUT ${rawResponse[0].content.length} finalMessages content from OpenAI`);
|
|
24829
|
+
}
|
|
24830
|
+
if (((_b = rawResponse[0].content[0]) === null || _b === void 0 ? void 0 : _b.type) !== 'text') {
|
|
24831
|
+
throw new PipelineExecutionError(`There is NOT 'text' BUT ${(_c = rawResponse[0].content[0]) === null || _c === void 0 ? void 0 : _c.type} finalMessages content type from OpenAI`);
|
|
24832
|
+
}
|
|
24833
|
+
let resultContent = (_d = rawResponse[0].content[0]) === null || _d === void 0 ? void 0 : _d.text.value;
|
|
24834
|
+
// Process annotations to replace file IDs with filenames
|
|
24835
|
+
if ((_e = rawResponse[0].content[0]) === null || _e === void 0 ? void 0 : _e.text.annotations) {
|
|
24836
|
+
const annotations = (_f = rawResponse[0].content[0]) === null || _f === void 0 ? void 0 : _f.text.annotations;
|
|
24837
|
+
// Map to store file ID -> filename to avoid duplicate requests
|
|
24838
|
+
const fileIdToName = new Map();
|
|
24839
|
+
for (const annotation of annotations) {
|
|
24840
|
+
if (annotation.type === 'file_citation') {
|
|
24841
|
+
const fileId = annotation.file_citation.file_id;
|
|
24842
|
+
let filename = fileIdToName.get(fileId);
|
|
24843
|
+
if (!filename) {
|
|
24844
|
+
try {
|
|
24845
|
+
const file = await client.files.retrieve(fileId);
|
|
24846
|
+
filename = file.filename;
|
|
24847
|
+
fileIdToName.set(fileId, filename);
|
|
24848
|
+
}
|
|
24849
|
+
catch (error) {
|
|
24850
|
+
console.error(`Failed to retrieve file info for ${fileId}`, error);
|
|
24851
|
+
// Fallback to "Source" or keep original if fetch fails
|
|
24852
|
+
filename = 'Source';
|
|
24853
|
+
}
|
|
24854
|
+
}
|
|
24855
|
+
if (filename && resultContent) {
|
|
24856
|
+
// Replace the citation marker with filename
|
|
24857
|
+
// Regex to match the second part of the citation: 【id†source】 -> 【id†filename】
|
|
24858
|
+
// Note: annotation.text contains the exact marker like 【4:0†source】
|
|
24859
|
+
const newText = annotation.text.replace(/†.*?】/, `†${filename}】`);
|
|
24860
|
+
resultContent = resultContent.replace(annotation.text, newText);
|
|
24861
|
+
}
|
|
24862
|
+
}
|
|
24863
|
+
}
|
|
24864
|
+
}
|
|
24865
|
+
// eslint-disable-next-line prefer-const
|
|
24866
|
+
complete = $getCurrentDate();
|
|
24867
|
+
const usage = UNCERTAIN_USAGE;
|
|
24868
|
+
// <- TODO: [🥘] Compute real usage for assistant
|
|
24869
|
+
// ?> const usage = computeOpenAiUsage(content, resultContent || '', rawResponse);
|
|
24870
|
+
if (resultContent === null) {
|
|
24871
|
+
throw new PipelineExecutionError('No response message from OpenAI');
|
|
24872
|
+
}
|
|
24873
|
+
return exportJson({
|
|
24874
|
+
name: 'promptResult',
|
|
24875
|
+
message: `Result of \`OpenAiAssistantExecutionTools.callChatModelStream\``,
|
|
24876
|
+
order: [],
|
|
24877
|
+
value: {
|
|
24878
|
+
content: resultContent,
|
|
24879
|
+
modelName: 'assistant',
|
|
24880
|
+
// <- TODO: [🥘] Detect used model in assistant
|
|
24881
|
+
// ?> model: rawResponse.model || modelName,
|
|
24882
|
+
timing: {
|
|
24883
|
+
start,
|
|
24884
|
+
complete,
|
|
24885
|
+
},
|
|
24886
|
+
usage,
|
|
24887
|
+
rawPromptContent,
|
|
24888
|
+
rawRequest,
|
|
24889
|
+
rawResponse,
|
|
24890
|
+
// <- [🗯]
|
|
24891
|
+
},
|
|
24892
|
+
});
|
|
24893
|
+
}
|
|
24894
|
+
/*
|
|
24895
|
+
public async playground() {
|
|
24896
|
+
const client = await this.getClient();
|
|
24897
|
+
|
|
24898
|
+
// List all assistants
|
|
24899
|
+
const assistants = await client.beta.assistants.list();
|
|
24900
|
+
|
|
24901
|
+
// Get details of a specific assistant
|
|
24902
|
+
const assistantId = 'asst_MO8fhZf4dGloCfXSHeLcIik0';
|
|
24903
|
+
const assistant = await client.beta.assistants.retrieve(assistantId);
|
|
24904
|
+
|
|
24905
|
+
// Update an assistant
|
|
24906
|
+
const updatedAssistant = await client.beta.assistants.update(assistantId, {
|
|
24907
|
+
name: assistant.name + '(M)',
|
|
24908
|
+
description: 'Updated description via Promptbook',
|
|
24909
|
+
metadata: {
|
|
24910
|
+
[Math.random().toString(36).substring(2, 15)]: new Date().toISOString(),
|
|
24911
|
+
},
|
|
24912
|
+
});
|
|
24913
|
+
|
|
24914
|
+
await forEver();
|
|
24915
|
+
}
|
|
24916
|
+
*/
|
|
24917
|
+
/**
|
|
24918
|
+
* Get an existing assistant tool wrapper
|
|
24919
|
+
*/
|
|
24920
|
+
getAssistant(assistantId) {
|
|
24921
|
+
return new OpenAiAssistantExecutionTools({
|
|
24922
|
+
...this.options,
|
|
24923
|
+
isCreatingNewAssistantsAllowed: this.isCreatingNewAssistantsAllowed,
|
|
24924
|
+
assistantId,
|
|
24925
|
+
});
|
|
24461
24926
|
}
|
|
24462
24927
|
async createNewAssistant(options) {
|
|
24463
24928
|
var _a, _b, _c;
|
|
@@ -24666,6 +25131,7 @@ function emitAssistantPreparationProgress(options) {
|
|
|
24666
25131
|
* - `LlmExecutionTools` - which wraps one or more LLM models and provides an interface to execute them
|
|
24667
25132
|
* - `AgentLlmExecutionTools` - which is a specific implementation of `LlmExecutionTools` that wraps another LlmExecutionTools and applies agent-specific system prompts and requirements
|
|
24668
25133
|
* - `OpenAiAssistantExecutionTools` - (Deprecated) which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities
|
|
25134
|
+
* - `OpenAiAgentKitExecutionTools` - which is a specific implementation of `LlmExecutionTools` backed by OpenAI AgentKit
|
|
24669
25135
|
* - `RemoteAgent` - which is an `Agent` that connects to a Promptbook Agents Server
|
|
24670
25136
|
*
|
|
24671
25137
|
* @public exported from `@promptbook/core`
|
|
@@ -24800,6 +25266,7 @@ class AgentLlmExecutionTools {
|
|
|
24800
25266
|
* Calls the chat model with agent-specific system prompt and requirements with streaming
|
|
24801
25267
|
*/
|
|
24802
25268
|
async callChatModelStream(prompt, onProgress) {
|
|
25269
|
+
var _a, _b;
|
|
24803
25270
|
// Ensure we're working with a chat prompt
|
|
24804
25271
|
if (prompt.modelRequirements.modelVariant !== 'CHAT') {
|
|
24805
25272
|
throw new Error('AgentLlmExecutionTools only supports chat prompts');
|
|
@@ -24827,7 +25294,75 @@ class AgentLlmExecutionTools {
|
|
|
24827
25294
|
}, // Cast to avoid readonly mismatch from spread
|
|
24828
25295
|
};
|
|
24829
25296
|
console.log('!!!! promptWithAgentModelRequirements:', promptWithAgentModelRequirements);
|
|
24830
|
-
if (
|
|
25297
|
+
if (OpenAiAgentKitExecutionTools.isOpenAiAgentKitExecutionTools(this.options.llmTools)) {
|
|
25298
|
+
const requirementsHash = SHA256(JSON.stringify(modelRequirements)).toString();
|
|
25299
|
+
const vectorStoreHash = SHA256(JSON.stringify((_a = modelRequirements.knowledgeSources) !== null && _a !== void 0 ? _a : [])).toString();
|
|
25300
|
+
const cachedVectorStore = AgentLlmExecutionTools.vectorStoreCache.get(this.title);
|
|
25301
|
+
const cachedAgentKit = AgentLlmExecutionTools.agentKitAgentCache.get(this.title);
|
|
25302
|
+
let preparedAgentKit = this.options.assistantPreparationMode === 'external'
|
|
25303
|
+
? this.options.llmTools.getPreparedAgentKitAgent()
|
|
25304
|
+
: null;
|
|
25305
|
+
const vectorStoreId = (preparedAgentKit === null || preparedAgentKit === void 0 ? void 0 : preparedAgentKit.vectorStoreId) ||
|
|
25306
|
+
(cachedVectorStore && cachedVectorStore.requirementsHash === vectorStoreHash
|
|
25307
|
+
? cachedVectorStore.vectorStoreId
|
|
25308
|
+
: undefined);
|
|
25309
|
+
if (!preparedAgentKit && cachedAgentKit && cachedAgentKit.requirementsHash === requirementsHash) {
|
|
25310
|
+
if (this.options.isVerbose) {
|
|
25311
|
+
console.info('[🤰]', 'Using cached OpenAI AgentKit agent', {
|
|
25312
|
+
agent: this.title,
|
|
25313
|
+
});
|
|
25314
|
+
}
|
|
25315
|
+
preparedAgentKit = {
|
|
25316
|
+
agent: cachedAgentKit.agent,
|
|
25317
|
+
vectorStoreId: cachedAgentKit.vectorStoreId,
|
|
25318
|
+
};
|
|
25319
|
+
}
|
|
25320
|
+
if (!preparedAgentKit) {
|
|
25321
|
+
if (this.options.isVerbose) {
|
|
25322
|
+
console.info('[🤰]', 'Preparing OpenAI AgentKit agent', {
|
|
25323
|
+
agent: this.title,
|
|
25324
|
+
});
|
|
25325
|
+
}
|
|
25326
|
+
if (!vectorStoreId && ((_b = modelRequirements.knowledgeSources) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
25327
|
+
emitAssistantPreparationProgress({
|
|
25328
|
+
onProgress,
|
|
25329
|
+
prompt,
|
|
25330
|
+
modelName: this.modelName,
|
|
25331
|
+
phase: 'Creating knowledge base',
|
|
25332
|
+
});
|
|
25333
|
+
}
|
|
25334
|
+
emitAssistantPreparationProgress({
|
|
25335
|
+
onProgress,
|
|
25336
|
+
prompt,
|
|
25337
|
+
modelName: this.modelName,
|
|
25338
|
+
phase: 'Preparing AgentKit agent',
|
|
25339
|
+
});
|
|
25340
|
+
preparedAgentKit = await this.options.llmTools.prepareAgentKitAgent({
|
|
25341
|
+
name: this.title,
|
|
25342
|
+
instructions: modelRequirements.systemMessage || '',
|
|
25343
|
+
knowledgeSources: modelRequirements.knowledgeSources,
|
|
25344
|
+
tools: modelRequirements.tools ? [...modelRequirements.tools] : undefined,
|
|
25345
|
+
vectorStoreId,
|
|
25346
|
+
});
|
|
25347
|
+
}
|
|
25348
|
+
if (preparedAgentKit.vectorStoreId) {
|
|
25349
|
+
AgentLlmExecutionTools.vectorStoreCache.set(this.title, {
|
|
25350
|
+
vectorStoreId: preparedAgentKit.vectorStoreId,
|
|
25351
|
+
requirementsHash: vectorStoreHash,
|
|
25352
|
+
});
|
|
25353
|
+
}
|
|
25354
|
+
AgentLlmExecutionTools.agentKitAgentCache.set(this.title, {
|
|
25355
|
+
agent: preparedAgentKit.agent,
|
|
25356
|
+
requirementsHash,
|
|
25357
|
+
vectorStoreId: preparedAgentKit.vectorStoreId,
|
|
25358
|
+
});
|
|
25359
|
+
underlyingLlmResult = await this.options.llmTools.callChatModelStreamWithPreparedAgent({
|
|
25360
|
+
openAiAgentKitAgent: preparedAgentKit.agent,
|
|
25361
|
+
prompt: promptWithAgentModelRequirements,
|
|
25362
|
+
onProgress,
|
|
25363
|
+
});
|
|
25364
|
+
}
|
|
25365
|
+
else if (OpenAiAssistantExecutionTools.isOpenAiAssistantExecutionTools(this.options.llmTools)) {
|
|
24831
25366
|
// ... deprecated path ...
|
|
24832
25367
|
const requirementsHash = SHA256(JSON.stringify(modelRequirements)).toString();
|
|
24833
25368
|
const cached = AgentLlmExecutionTools.assistantCache.get(this.title);
|
|
@@ -24952,6 +25487,10 @@ class AgentLlmExecutionTools {
|
|
|
24952
25487
|
return agentResult;
|
|
24953
25488
|
}
|
|
24954
25489
|
}
|
|
25490
|
+
/**
|
|
25491
|
+
* Cached AgentKit agents to avoid rebuilding identical instances.
|
|
25492
|
+
*/
|
|
25493
|
+
AgentLlmExecutionTools.agentKitAgentCache = new Map();
|
|
24955
25494
|
/**
|
|
24956
25495
|
* Cache of OpenAI assistants to avoid creating duplicates
|
|
24957
25496
|
*/
|
|
@@ -25033,6 +25572,7 @@ function buildTeacherSummary(commitments, used) {
|
|
|
25033
25572
|
* - `LlmExecutionTools` - which wraps one or more LLM models and provides an interface to execute them
|
|
25034
25573
|
* - `AgentLlmExecutionTools` - which is a specific implementation of `LlmExecutionTools` that wraps another LlmExecutionTools and applies agent-specific system prompts and requirements
|
|
25035
25574
|
* - `OpenAiAssistantExecutionTools` - (Deprecated) which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities
|
|
25575
|
+
* - `OpenAiAgentKitExecutionTools` - which is a specific implementation of `LlmExecutionTools` backed by OpenAI AgentKit
|
|
25036
25576
|
* - `RemoteAgent` - which is an `Agent` that connects to a Promptbook Agents Server
|
|
25037
25577
|
*
|
|
25038
25578
|
* @public exported from `@promptbook/core`
|
|
@@ -25403,7 +25943,8 @@ function buildRemoteAgentSource(profile, meta) {
|
|
|
25403
25943
|
* - `Agent` - which represents an AI Agent with its source, memories, actions, etc. Agent is a higher-level abstraction which is internally using:
|
|
25404
25944
|
* - `LlmExecutionTools` - which wraps one or more LLM models and provides an interface to execute them
|
|
25405
25945
|
* - `AgentLlmExecutionTools` - which is a specific implementation of `LlmExecutionTools` that wraps another LlmExecutionTools and applies agent-specific system prompts and requirements
|
|
25406
|
-
* - `OpenAiAssistantExecutionTools` - which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities
|
|
25946
|
+
* - `OpenAiAssistantExecutionTools` - (Deprecated) which is a specific implementation of `LlmExecutionTools` for OpenAI models with assistant capabilities
|
|
25947
|
+
* - `OpenAiAgentKitExecutionTools` - which is a specific implementation of `LlmExecutionTools` backed by OpenAI AgentKit
|
|
25407
25948
|
* - `RemoteAgent` - which is an `Agent` that connects to a Promptbook Agents Server
|
|
25408
25949
|
*
|
|
25409
25950
|
* @public exported from `@promptbook/core`
|