Files
jlinc-langchain-js/dist/auth/JLINCAuthBaseChatModel.js
2025-11-24 14:21:49 +00:00

97 lines
3.1 KiB
JavaScript

const { BaseChatModel } = require("@langchain/core/language_models/chat_models");
const { JLINCTracer } = require("../tracer/index.js");
const { authDecide, authInvoke, getDescription, getName } = require("./common.js");
/**
* @typedef {import('./common').JLINCConfig} JLINCConfig
*/
/**
* @typedef {Object} JLINCAuthBaseChatModelFields
* @property {JLINCConfig} config
* @property {Tool} jlincAuthDecision
* @property {BaseChatModel} targetAuthorized
* @property {BaseChatModel|null} targetNotAuthorized
*/
class JLINCAuthBaseChatModel extends BaseChatModel {
/**
* @param {JLINCAuthBaseChatModelFields} fields
*/
constructor({ config, jlincAuthDecision, targetAuthorized, targetNotAuthorized }) {
super({
name: "jlinc_auth_base_chat_model",
description: "", // overridden dynamically
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: getName(targetAuthorized, targetNotAuthorized)
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: getDescription(targetAuthorized, targetNotAuthorized, 'BaseChatModel')
});
/** @type {JLINCConfig} */
this.config = config;
/** @type {Tool} */
this.jlincAuthDecision = jlincAuthDecision;
/** @type {BaseChatModel} */
this.targetAuthorized = targetAuthorized;
/** @type {BaseChatModel|null} */
this.targetNotAuthorized = targetNotAuthorized;
/** @type {JLINCTracer} */
this.tracer = new JLINCTracer(config);
/** @type {string} */
this.authType = 'BaseChatModel';
}
/**
* @param {BindToolsInput[]} tools - The tools to bind to the underlying models.
* @param {Partial<CallOptions>} [kwargs] - Optional call options.
* @returns {Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>}
* A new JLINCAuthBaseChatModel instance whose underlying models
* have been bound with the provided tools.
*/
bindTools(tools, kwargs = {}) {
if (this.config.debug)
console.log(`[JLINCAuth] Binding tools to BaseChatModels`);
const authorizedBound = this.targetAuthorized.bindTools(tools, kwargs);
const notAuthorizedBound = this.targetNotAuthorized
? this.targetNotAuthorized.bindTools(tools, kwargs)
: null;
return new JLINCAuthBaseChatModel({
config: this.config,
jlincAuthDecision: this.jlincAuthDecision,
targetAuthorized: authorizedBound,
targetNotAuthorized: notAuthorizedBound,
});
}
/**
* @returns {string} - type
*/
_llmType() {
const type = this.targetAuthorized?._llmType?.() ?? "unknown";
if (this.config?.debug)
console.log(`[JLINCAuth] Returning LLM type: ${type}`);
return type;
}
/**
* @param {BaseLanguageModelInput} input
* @param {Partial<CallOptions>} [options] - Optional call options.
* @returns {Promise<OutputMessageType>} A promise that resolves with the output.
*/
async invoke(input, runManager) {
return await authInvoke(this, input);
}
}
module.exports = {
JLINCAuthBaseChatModel,
};