UNPKG

@convo-lang/convo-lang

Version:
67 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convoScopeFunctionReadDoc = void 0; const common_1 = require("@iyio/common"); const ConvoDocQueryRunner_1 = require("../ConvoDocQueryRunner"); const ConvoError_1 = require("../ConvoError"); const convo_lang_doc_lib_1 = require("../convo-lang-doc-lib"); const convo_lib_1 = require("../convo-lib"); const lock = new common_1.Lock(1); exports.convoScopeFunctionReadDoc = (0, convo_lib_1.createConvoScopeFunction)({ usesLabels: true }, async (scope, ctx) => { const args = (0, convo_lib_1.convoParamsToObj)(scope, ['path', 'from', 'to']); let { path, from, to, useVision, count, cache, memoryCacheTtlMs, tagPages = true, query, salt, ...options } = args; if (!path) { throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, 'Document path required'); } if ((typeof count === 'number') && to === undefined) { to = (from ?? 0) + count - 1; } const release = await lock.waitOrCancelAsync(ctx.convo.conversation?.disposeToken); if (!release) { return null; } try { const runner = new ConvoDocQueryRunner_1.ConvoDocQueryRunner({ query: { src: path, visionPass: useVision === true, textPass: useVision !== true, salt: (typeof salt === 'string') ? salt : undefined, ...(0, common_1.deleteUndefined)(query), }, cacheConversations: cache !== false, cacheQueryResults: cache !== false, cacheTextPass: cache !== false, conversationOptions: { onTokenUsage: u => ctx.convo.conversation?.addUsage(u) }, useRunLock: true, ...(0, common_1.deleteUndefined)(options), }); const removeTask = ctx.convo.conversation?.addTask({ name: `Reading ${(0, common_1.getFileName)(path)}`, progress: runner.progress, documentUrl: path, delayMs: 750 }); try { let r = await runner.runQueryAsync(); if (from !== undefined || to !== undefined) { r = { ...r }; if (from === undefined) { from = 0; } r.pages = r.pages.filter(p => p.index >= from && (to === undefined ? true : p.index <= to)); } return (0, convo_lang_doc_lib_1.convoDoQueryOutputToMessageContent)(r, { escapeConvo: false, tagPages: Boolean(tagPages) }); } finally { runner.dispose(); removeTask?.(); } } finally { release(); } }); //# sourceMappingURL=convoScopeFunctionReadDoc.js.map