adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
107 lines (106 loc) • 3.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvocationContext = exports.LlmCallsLimitExceededError = void 0;
const uuid_1 = require("uuid");
const Session_1 = require("../sessions/Session");
/**
* Error thrown when the number of LLM calls exceed the limit.
*/
class LlmCallsLimitExceededError extends Error {
constructor(message) {
super(message);
this.name = 'LlmCallsLimitExceededError';
}
}
exports.LlmCallsLimitExceededError = LlmCallsLimitExceededError;
/**
* A container to keep track of the cost of invocation.
*/
class InvocationCostManager {
constructor() {
this.numberOfLlmCalls = 0;
}
/**
* Increments number of LLM calls made and enforces the limit.
*/
incrementAndEnforceLlmCallsLimit(runConfig) {
this.numberOfLlmCalls += 1;
if (runConfig &&
runConfig.maxLlmCalls !== undefined &&
runConfig.maxLlmCalls > 0 &&
this.numberOfLlmCalls > runConfig.maxLlmCalls) {
throw new LlmCallsLimitExceededError(`Max number of llm calls limit of ${runConfig.maxLlmCalls} exceeded`);
}
}
}
/**
* An invocation context represents the data of a single invocation of an agent.
*/
class InvocationContext {
/**
* Creates a new invocation context.
*
* @param options Options for the context
*/
constructor(options = {}) {
/**
* Whether to end this invocation.
*
* Set to True in callbacks or tools to terminate this invocation.
*/
this.endInvocation = false;
/** Whether this is a live invocation */
this.live = false;
this.invocationCostManager = new InvocationCostManager();
this.artifactService = options.artifactService;
this.sessionService = options.sessionService;
this.memoryService = options.memoryService;
this.invocationId = options.invocationId || (0, uuid_1.v4)();
this.branch = options.branch;
this.agent = options.agent;
this.userContent = options.userContent;
this.session = options.session || new Session_1.Session();
this.endInvocation = options.endInvocation || false;
this.liveRequestQueue = options.liveRequestQueue;
this.activeStreamingTools = options.activeStreamingTools;
this.transcriptionCache = options.transcriptionCache;
this.runConfig = options.runConfig;
this.llm = options.llm;
this.live = options.live || false;
// Copy any additional properties
for (const [key, value] of Object.entries(options)) {
if (!Object.prototype.hasOwnProperty.call(this, key)) {
this[key] = value;
}
}
}
/**
* Tracks number of llm calls made.
*
* @throws LlmCallsLimitExceededError If number of llm calls made exceed the set threshold.
*/
incrementLlmCallCount() {
this.invocationCostManager.incrementAndEnforceLlmCallsLimit(this.runConfig);
}
/** The app name from the session */
get appName() {
return this.session.appName;
}
/** The user ID from the session */
get userId() {
return this.session.userId;
}
}
exports.InvocationContext = InvocationContext;
/**
* Generates a UUID.
*
* @returns A UUID string
*/
function generateUuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}