movie-agent 1.0.0 → 1.1.1
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/README.md +319 -39
- package/bin/movie-agent +33 -8
- package/dist/agent.d.ts +31 -4
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +109 -16
- package/dist/agent.js.map +1 -1
- package/dist/cache.d.ts +40 -5
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +95 -11
- package/dist/cache.js.map +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +23 -1
- package/dist/factory.js.map +1 -1
- package/dist/format.d.ts +14 -1
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +22 -1
- package/dist/format.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/llm.d.ts +33 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +221 -0
- package/dist/llm.js.map +1 -0
- package/dist/providers.d.ts +9 -1
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +27 -0
- package/dist/providers.js.map +1 -1
- package/dist/rateLimiter.d.ts +59 -0
- package/dist/rateLimiter.d.ts.map +1 -0
- package/dist/rateLimiter.js +99 -0
- package/dist/rateLimiter.js.map +1 -0
- package/dist/sanitize.d.ts +24 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +137 -0
- package/dist/sanitize.js.map +1 -0
- package/dist/tmdbApi.d.ts +37 -1
- package/dist/tmdbApi.d.ts.map +1 -1
- package/dist/tmdbApi.js +70 -22
- package/dist/tmdbApi.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +5 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +38 -1
- package/dist/validate.js.map +1 -1
- package/package.json +9 -3
package/dist/format.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TMDB_IMAGE_BASE_URL = void 0;
|
|
3
4
|
exports.buildDescription = buildDescription;
|
|
5
|
+
exports.buildPosterUrl = buildPosterUrl;
|
|
4
6
|
exports.toRecommendation = toRecommendation;
|
|
5
7
|
exports.formatResponse = formatResponse;
|
|
6
8
|
/**
|
|
@@ -40,9 +42,26 @@ function buildDescription(overview) {
|
|
|
40
42
|
// If we can't find a good sentence boundary, add ellipsis
|
|
41
43
|
return truncated + '...';
|
|
42
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* TMDb image base URL for constructing poster URLs
|
|
47
|
+
* @see https://developer.themoviedb.org/docs/image-basics
|
|
48
|
+
*/
|
|
49
|
+
exports.TMDB_IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/';
|
|
50
|
+
/**
|
|
51
|
+
* Builds a full poster URL from a TMDb poster_path
|
|
52
|
+
* @param posterPath - The poster_path from TMDb API (e.g., "/abc123.jpg")
|
|
53
|
+
* @param size - Image size (default: 'w500')
|
|
54
|
+
* @returns Full URL to the poster image, or null if no poster available
|
|
55
|
+
*/
|
|
56
|
+
function buildPosterUrl(posterPath, size = 'w500') {
|
|
57
|
+
if (!posterPath) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return `${exports.TMDB_IMAGE_BASE_URL}${size}${posterPath}`;
|
|
61
|
+
}
|
|
43
62
|
/**
|
|
44
63
|
* Formats a movie with streaming providers into a MovieRecommendation
|
|
45
|
-
* @param movie - TMDb movie object with id, title, release_date, runtime, overview, genres
|
|
64
|
+
* @param movie - TMDb movie object with id, title, release_date, runtime, overview, genres, poster_path
|
|
46
65
|
* @param providers - Streaming provider information
|
|
47
66
|
* @param reason - Why this movie was recommended
|
|
48
67
|
* @returns Formatted MovieRecommendation
|
|
@@ -51,6 +70,7 @@ function toRecommendation(movie, providers, reason) {
|
|
|
51
70
|
const releaseYear = new Date(movie.release_date).getFullYear();
|
|
52
71
|
const description = buildDescription(movie.overview);
|
|
53
72
|
const genres = movie.genres.map(g => g.name);
|
|
73
|
+
const posterUrl = buildPosterUrl(movie.poster_path);
|
|
54
74
|
return {
|
|
55
75
|
title: movie.title,
|
|
56
76
|
releaseYear,
|
|
@@ -59,6 +79,7 @@ function toRecommendation(movie, providers, reason) {
|
|
|
59
79
|
genres,
|
|
60
80
|
streamingPlatforms: providers,
|
|
61
81
|
matchReason: reason,
|
|
82
|
+
posterUrl,
|
|
62
83
|
};
|
|
63
84
|
}
|
|
64
85
|
/**
|
package/dist/format.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":";;;AAYA,4CAuCC;AAcD,wCAQC;AASD,4CA4BC;AAQD,wCAqBC;AApID;;;;GAIG;AACH,SAAgB,gBAAgB,CAAC,QAAgB;IAC/C,kDAAkD;IAClD,6BAA6B;IAC7B,gDAAgD;IAChD,8BAA8B;IAE9B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,2BAA2B,CAAC;IACrC,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACpC,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,MAAM,QAAQ,GAAG,GAAG,CAAC;IAErB,IAAI,KAAK,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC7B,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,8DAA8D;IAC9D,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAErD,6DAA6D;IAC7D,MAAM,UAAU,GAAG,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;I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AC7C,MAAM,SAAS,GAAG,cAAc,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAEpD,OAAO;QACL,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,WAAW;QACX,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,WAAW;QACX,MAAM;QACN,kBAAkB,EAAE,SAAS;QAC7B,WAAW,EAAE,MAAM;QACnB,SAAS;KACV,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAC5B,eAAsC,EACtC,QAAqE;IAErE,8BAA8B;IAC9B,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,KAAK,CACb,qCAAqC,eAAe,CAAC,MAAM,EAAE,CAC9D,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,GAAkB;QAC9B,eAAe;QACf,QAAQ,EAAE;YACR,gBAAgB,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YAC1C,YAAY,EAAE,eAAe,CAAC,MAAM;YACpC,eAAe,EAAE,QAAQ,CAAC,eAAe;SAC1C;KACF,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ export { MovieAgent } from './agent';
|
|
|
2
2
|
export { MovieAgentFactory, MovieAgentConfig } from './factory';
|
|
3
3
|
export { UserInput, AgentResponse, MovieRecommendation, StreamingPlatform, ErrorResponse, } from './types';
|
|
4
4
|
export { default as TmdbApiClient } from './tmdbApi';
|
|
5
|
+
export { LLMService, getLLMService } from './llm';
|
|
6
|
+
export { buildPosterUrl, TMDB_IMAGE_BASE_URL } from './format';
|
|
5
7
|
export { MovieAgentFactory as default } from './factory';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAGhE,OAAO,EACL,SAAS,EACT,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,aAAa,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,WAAW,CAAC;AAGrD,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAGhE,OAAO,EACL,SAAS,EACT,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,aAAa,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,WAAW,CAAC;AAGrD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAGlD,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAG/D,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.default = exports.TmdbApiClient = exports.MovieAgentFactory = exports.MovieAgent = void 0;
|
|
7
|
+
exports.default = exports.TMDB_IMAGE_BASE_URL = exports.buildPosterUrl = exports.getLLMService = exports.LLMService = exports.TmdbApiClient = exports.MovieAgentFactory = exports.MovieAgent = void 0;
|
|
8
8
|
// Export the main MovieAgent class
|
|
9
9
|
var agent_1 = require("./agent");
|
|
10
10
|
Object.defineProperty(exports, "MovieAgent", { enumerable: true, get: function () { return agent_1.MovieAgent; } });
|
|
@@ -14,6 +14,14 @@ Object.defineProperty(exports, "MovieAgentFactory", { enumerable: true, get: fun
|
|
|
14
14
|
// Export TMDb API client for advanced use cases
|
|
15
15
|
var tmdbApi_1 = require("./tmdbApi");
|
|
16
16
|
Object.defineProperty(exports, "TmdbApiClient", { enumerable: true, get: function () { return __importDefault(tmdbApi_1).default; } });
|
|
17
|
+
// Export LLM service for advanced use cases
|
|
18
|
+
var llm_1 = require("./llm");
|
|
19
|
+
Object.defineProperty(exports, "LLMService", { enumerable: true, get: function () { return llm_1.LLMService; } });
|
|
20
|
+
Object.defineProperty(exports, "getLLMService", { enumerable: true, get: function () { return llm_1.getLLMService; } });
|
|
21
|
+
// Export poster URL utilities
|
|
22
|
+
var format_1 = require("./format");
|
|
23
|
+
Object.defineProperty(exports, "buildPosterUrl", { enumerable: true, get: function () { return format_1.buildPosterUrl; } });
|
|
24
|
+
Object.defineProperty(exports, "TMDB_IMAGE_BASE_URL", { enumerable: true, get: function () { return format_1.TMDB_IMAGE_BASE_URL; } });
|
|
17
25
|
// Default export is the factory for convenience
|
|
18
26
|
var factory_2 = require("./factory");
|
|
19
27
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return factory_2.MovieAgentFactory; } });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,8DAA8D;;;;;;AAE9D,mCAAmC;AACnC,iCAAqC;AAA5B,mGAAA,UAAU,OAAA;AAEnB,4CAA4C;AAC5C,qCAAgE;AAAvD,4GAAA,iBAAiB,OAAA;AAW1B,gDAAgD;AAChD,qCAAqD;AAA5C,yHAAA,OAAO,OAAiB;AAEjC,gDAAgD;AAChD,qCAAyD;AAAhD,kGAAA,iBAAiB,OAAW"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,8DAA8D;;;;;;AAE9D,mCAAmC;AACnC,iCAAqC;AAA5B,mGAAA,UAAU,OAAA;AAEnB,4CAA4C;AAC5C,qCAAgE;AAAvD,4GAAA,iBAAiB,OAAA;AAW1B,gDAAgD;AAChD,qCAAqD;AAA5C,yHAAA,OAAO,OAAiB;AAEjC,4CAA4C;AAC5C,6BAAkD;AAAzC,iGAAA,UAAU,OAAA;AAAE,oGAAA,aAAa,OAAA;AAElC,8BAA8B;AAC9B,mCAA+D;AAAtD,wGAAA,cAAc,OAAA;AAAE,6GAAA,mBAAmB,OAAA;AAE5C,gDAAgD;AAChD,qCAAyD;AAAhD,kGAAA,iBAAiB,OAAW"}
|
package/dist/llm.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AgentResponse } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* LLM service for generating formatted movie recommendation output
|
|
4
|
+
*/
|
|
5
|
+
export declare class LLMService {
|
|
6
|
+
private model;
|
|
7
|
+
private chain;
|
|
8
|
+
constructor(apiKey?: string, provider?: 'gemini' | 'azure', azureConfig?: {
|
|
9
|
+
endpoint?: string;
|
|
10
|
+
deployment?: string;
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Format movie recommendations using LLM (non-streaming)
|
|
14
|
+
* @param response - Agent response with recommendations
|
|
15
|
+
* @param userInput - Original user input for context
|
|
16
|
+
* @returns Formatted markdown string
|
|
17
|
+
*/
|
|
18
|
+
formatRecommendations(response: AgentResponse, userInput: any): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Format movie recommendations using LLM with streaming
|
|
21
|
+
* @param response - Agent response with recommendations
|
|
22
|
+
* @param userInput - Original user input for context
|
|
23
|
+
* @param onChunk - Callback function called for each chunk of streamed content
|
|
24
|
+
* @returns Promise that resolves when streaming is complete
|
|
25
|
+
*/
|
|
26
|
+
formatRecommendationsStream(response: AgentResponse, userInput: any, onChunk: (chunk: string) => void): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Fallback formatting if LLM fails
|
|
29
|
+
*/
|
|
30
|
+
private fallbackFormat;
|
|
31
|
+
}
|
|
32
|
+
export declare function getLLMService(provider?: 'gemini' | 'azure'): LLMService;
|
|
33
|
+
//# sourceMappingURL=llm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxC;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,KAAK,CAA2C;IACxD,OAAO,CAAC,KAAK,CAAM;gBAGjB,MAAM,CAAC,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,QAAQ,GAAG,OAAO,EAC7B,WAAW,CAAC,EAAE;QACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB;IAuEH;;;;;OAKG;IACG,qBAAqB,CACzB,QAAQ,EAAE,aAAa,EACvB,SAAS,EAAE,GAAG,GACb,OAAO,CAAC,MAAM,CAAC;IA0DlB;;;;;;OAMG;IACG,2BAA2B,CAC/B,QAAQ,EAAE,aAAa,EACvB,SAAS,EAAE,GAAG,EACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAC/B,OAAO,CAAC,IAAI,CAAC;IAqEhB;;OAEG;IACH,OAAO,CAAC,cAAc;CAqBvB;AAOD,wBAAgB,aAAa,CAAC,QAAQ,CAAC,EAAE,QAAQ,GAAG,OAAO,GAAG,UAAU,CAKvE"}
|
package/dist/llm.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LLMService = void 0;
|
|
7
|
+
exports.getLLMService = getLLMService;
|
|
8
|
+
// src/llm.ts
|
|
9
|
+
const google_genai_1 = require("@langchain/google-genai");
|
|
10
|
+
const openai_1 = require("@langchain/openai");
|
|
11
|
+
const prompts_1 = require("@langchain/core/prompts");
|
|
12
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
13
|
+
const config_1 = __importDefault(require("./config"));
|
|
14
|
+
const sanitize_1 = require("./sanitize");
|
|
15
|
+
/**
|
|
16
|
+
* LLM service for generating formatted movie recommendation output
|
|
17
|
+
*/
|
|
18
|
+
class LLMService {
|
|
19
|
+
constructor(apiKey, provider, azureConfig) {
|
|
20
|
+
const llmProvider = provider || config_1.default.LLM_PROVIDER;
|
|
21
|
+
if (llmProvider === 'azure') {
|
|
22
|
+
// Azure OpenAI Configuration
|
|
23
|
+
const azureApiKey = apiKey || config_1.default.AZURE_OPENAI_API_KEY;
|
|
24
|
+
const azureEndpoint = azureConfig?.endpoint || config_1.default.AZURE_OPENAI_ENDPOINT;
|
|
25
|
+
const azureDeployment = azureConfig?.deployment || config_1.default.AZURE_OPENAI_DEPLOYMENT;
|
|
26
|
+
if (!azureApiKey || !azureEndpoint || !azureDeployment) {
|
|
27
|
+
throw new Error('AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_DEPLOYMENT are required for Azure OpenAI');
|
|
28
|
+
}
|
|
29
|
+
this.model = new openai_1.AzureChatOpenAI({
|
|
30
|
+
azureOpenAIApiKey: azureApiKey,
|
|
31
|
+
azureOpenAIApiInstanceName: azureEndpoint
|
|
32
|
+
.replace(/^https?:\/\//, '')
|
|
33
|
+
.replace(/\.openai\.azure\.com\/?$/, ''),
|
|
34
|
+
azureOpenAIApiDeploymentName: azureDeployment,
|
|
35
|
+
azureOpenAIApiVersion: '2024-08-01-preview',
|
|
36
|
+
temperature: 0.7,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Gemini Configuration (default)
|
|
41
|
+
const geminiApiKey = apiKey || config_1.default.GEMINI_API_KEY;
|
|
42
|
+
if (!geminiApiKey) {
|
|
43
|
+
throw new Error('GEMINI_API_KEY is required for LLM service');
|
|
44
|
+
}
|
|
45
|
+
this.model = new google_genai_1.ChatGoogleGenerativeAI({
|
|
46
|
+
apiKey: geminiApiKey,
|
|
47
|
+
model: 'gemini-2.5-flash',
|
|
48
|
+
temperature: 0.7,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
// Create the prompt template
|
|
52
|
+
const promptTemplate = prompts_1.PromptTemplate.fromTemplate(`
|
|
53
|
+
You are a friendly movie recommendation assistant. Format the following movie recommendations in an engaging, readable way.
|
|
54
|
+
|
|
55
|
+
User Input: {userInput}
|
|
56
|
+
|
|
57
|
+
Movie Recommendations:
|
|
58
|
+
{recommendations}
|
|
59
|
+
|
|
60
|
+
Instructions:
|
|
61
|
+
- Create a warm, conversational introduction
|
|
62
|
+
- For each movie, include:
|
|
63
|
+
* Title, year, and runtime
|
|
64
|
+
* Genres
|
|
65
|
+
* A compelling description
|
|
66
|
+
* Available streaming platforms with emoji 📺
|
|
67
|
+
* A brief explanation of why it matches the user's preferences (✨ Why:)
|
|
68
|
+
- Use markdown formatting (bold, lists, etc.)
|
|
69
|
+
- Add visual separators between movies
|
|
70
|
+
- Keep it concise but engaging
|
|
71
|
+
- Use emojis to make it more visually appealing
|
|
72
|
+
|
|
73
|
+
Generate the formatted output:
|
|
74
|
+
`);
|
|
75
|
+
// Create the chain: prompt -> model -> output parser
|
|
76
|
+
this.chain = promptTemplate.pipe(this.model).pipe(new output_parsers_1.StringOutputParser());
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Format movie recommendations using LLM (non-streaming)
|
|
80
|
+
* @param response - Agent response with recommendations
|
|
81
|
+
* @param userInput - Original user input for context
|
|
82
|
+
* @returns Formatted markdown string
|
|
83
|
+
*/
|
|
84
|
+
async formatRecommendations(response, userInput) {
|
|
85
|
+
// Prepare the recommendations data
|
|
86
|
+
const recommendationsText = response.recommendations
|
|
87
|
+
.map((movie, index) => {
|
|
88
|
+
const platforms = movie.streamingPlatforms
|
|
89
|
+
.filter(p => p.available)
|
|
90
|
+
.map(p => p.name)
|
|
91
|
+
.join(', ');
|
|
92
|
+
return `
|
|
93
|
+
${index + 1}. ${movie.title} (${movie.releaseYear})
|
|
94
|
+
- Runtime: ${movie.runtime} minutes
|
|
95
|
+
- Genres: ${movie.genres.join(', ')}
|
|
96
|
+
- Description: ${movie.description}
|
|
97
|
+
- Available on: ${platforms || 'No streaming availability'}
|
|
98
|
+
- Match reason: ${movie.matchReason}
|
|
99
|
+
`;
|
|
100
|
+
})
|
|
101
|
+
.join('\n');
|
|
102
|
+
// Sanitize and format user input to prevent prompt injection
|
|
103
|
+
const sanitizedInput = (0, sanitize_1.sanitizeForLLMPrompt)(userInput);
|
|
104
|
+
// Log potential injection attempts for security monitoring
|
|
105
|
+
if ((0, sanitize_1.detectPromptInjection)(userInput)) {
|
|
106
|
+
console.warn('⚠️ Potential prompt injection detected in user input. Input has been sanitized.');
|
|
107
|
+
}
|
|
108
|
+
const userInputText = JSON.stringify(sanitizedInput, null, 2);
|
|
109
|
+
// Generate formatted output using LLM with timeout
|
|
110
|
+
try {
|
|
111
|
+
const timeoutMs = 15000; // 15 second timeout
|
|
112
|
+
const startTime = Date.now();
|
|
113
|
+
const formattedOutput = await Promise.race([
|
|
114
|
+
this.chain.invoke({
|
|
115
|
+
userInput: userInputText,
|
|
116
|
+
recommendations: recommendationsText,
|
|
117
|
+
}),
|
|
118
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('LLM request timeout')), timeoutMs)),
|
|
119
|
+
]);
|
|
120
|
+
const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
|
|
121
|
+
console.log(`✓ LLM formatting completed in ${elapsedTime}s\n`);
|
|
122
|
+
return formattedOutput;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
console.error('LLM formatting error:', error);
|
|
126
|
+
// Fallback to basic formatting if LLM fails
|
|
127
|
+
return this.fallbackFormat(response);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Format movie recommendations using LLM with streaming
|
|
132
|
+
* @param response - Agent response with recommendations
|
|
133
|
+
* @param userInput - Original user input for context
|
|
134
|
+
* @param onChunk - Callback function called for each chunk of streamed content
|
|
135
|
+
* @returns Promise that resolves when streaming is complete
|
|
136
|
+
*/
|
|
137
|
+
async formatRecommendationsStream(response, userInput, onChunk) {
|
|
138
|
+
// Prepare the recommendations data
|
|
139
|
+
const recommendationsText = response.recommendations
|
|
140
|
+
.map((movie, index) => {
|
|
141
|
+
const platforms = movie.streamingPlatforms
|
|
142
|
+
.filter(p => p.available)
|
|
143
|
+
.map(p => p.name)
|
|
144
|
+
.join(', ');
|
|
145
|
+
return `
|
|
146
|
+
${index + 1}. ${movie.title} (${movie.releaseYear})
|
|
147
|
+
- Runtime: ${movie.runtime} minutes
|
|
148
|
+
- Genres: ${movie.genres.join(', ')}
|
|
149
|
+
- Description: ${movie.description}
|
|
150
|
+
- Available on: ${platforms || 'No streaming availability'}
|
|
151
|
+
- Match reason: ${movie.matchReason}
|
|
152
|
+
`;
|
|
153
|
+
})
|
|
154
|
+
.join('\n');
|
|
155
|
+
// Sanitize and format user input to prevent prompt injection
|
|
156
|
+
const sanitizedInput = (0, sanitize_1.sanitizeForLLMPrompt)(userInput);
|
|
157
|
+
// Log potential injection attempts for security monitoring
|
|
158
|
+
if ((0, sanitize_1.detectPromptInjection)(userInput)) {
|
|
159
|
+
console.warn('⚠️ Potential prompt injection detected in user input. Input has been sanitized.');
|
|
160
|
+
}
|
|
161
|
+
const userInputText = JSON.stringify(sanitizedInput, null, 2);
|
|
162
|
+
try {
|
|
163
|
+
const timeoutMs = 15000; // 15 second timeout
|
|
164
|
+
const startTime = Date.now();
|
|
165
|
+
// Create a promise that will handle the streaming
|
|
166
|
+
const streamPromise = (async () => {
|
|
167
|
+
const stream = await this.chain.stream({
|
|
168
|
+
userInput: userInputText,
|
|
169
|
+
recommendations: recommendationsText,
|
|
170
|
+
});
|
|
171
|
+
for await (const chunk of stream) {
|
|
172
|
+
onChunk(chunk);
|
|
173
|
+
}
|
|
174
|
+
})();
|
|
175
|
+
// Race between streaming and timeout
|
|
176
|
+
await Promise.race([
|
|
177
|
+
streamPromise,
|
|
178
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('LLM streaming timeout')), timeoutMs)),
|
|
179
|
+
]);
|
|
180
|
+
const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
|
|
181
|
+
console.log(`\n✓ LLM streaming completed in ${elapsedTime}s\n`);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error('\nLLM streaming error:', error);
|
|
185
|
+
// Fallback to basic formatting if LLM fails
|
|
186
|
+
const fallback = this.fallbackFormat(response);
|
|
187
|
+
onChunk(fallback);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Fallback formatting if LLM fails
|
|
192
|
+
*/
|
|
193
|
+
fallbackFormat(response) {
|
|
194
|
+
let output = '\n🎬 Movie Recommendations\n\n';
|
|
195
|
+
response.recommendations.forEach((movie, index) => {
|
|
196
|
+
output += `${index + 1}. **${movie.title}** (${movie.releaseYear}) • ${movie.runtime} min\n`;
|
|
197
|
+
output += ` Genres: ${movie.genres.join(', ')}\n\n`;
|
|
198
|
+
output += ` ${movie.description}\n\n`;
|
|
199
|
+
const platforms = movie.streamingPlatforms
|
|
200
|
+
.filter(p => p.available)
|
|
201
|
+
.map(p => p.name);
|
|
202
|
+
if (platforms.length > 0) {
|
|
203
|
+
output += ` 📺 Available on: ${platforms.join(', ')}\n`;
|
|
204
|
+
}
|
|
205
|
+
output += ` ✨ Why: ${movie.matchReason}\n\n`;
|
|
206
|
+
});
|
|
207
|
+
return output;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.LLMService = LLMService;
|
|
211
|
+
/**
|
|
212
|
+
* Create and export a singleton instance
|
|
213
|
+
*/
|
|
214
|
+
let llmServiceInstance = null;
|
|
215
|
+
function getLLMService(provider) {
|
|
216
|
+
if (!llmServiceInstance) {
|
|
217
|
+
llmServiceInstance = new LLMService(undefined, provider);
|
|
218
|
+
}
|
|
219
|
+
return llmServiceInstance;
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":";;;;;;AA+QA,sCAKC;AApRD,aAAa;AACb,0DAAiE;AACjE,8CAAoD;AACpD,qDAAyD;AACzD,mEAAoE;AAEpE,sDAA8B;AAC9B,yCAAyE;AAEzE;;GAEG;AACH,MAAa,UAAU;IAIrB,YACE,MAAe,EACf,QAA6B,EAC7B,WAGC;QAED,MAAM,WAAW,GAAG,QAAQ,IAAI,gBAAM,CAAC,YAAY,CAAC;QAEpD,IAAI,WAAW,KAAK,OAAO,EAAE,CAAC;YAC5B,6BAA6B;YAC7B,MAAM,WAAW,GAAG,MAAM,IAAI,gBAAM,CAAC,oBAAoB,CAAC;YAC1D,MAAM,aAAa,GACjB,WAAW,EAAE,QAAQ,IAAI,gBAAM,CAAC,qBAAqB,CAAC;YACxD,MAAM,eAAe,GACnB,WAAW,EAAE,UAAU,IAAI,gBAAM,CAAC,uBAAuB,CAAC;YAE5D,IAAI,CAAC,WAAW,IAAI,CAAC,aAAa,IAAI,CAAC,eAAe,EAAE,CAAC;gBACvD,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;YACJ,CAAC;YAED,IAAI,CAAC,KAAK,GAAG,IAAI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
|
|
1
|
-
import TmdbApiClient from './tmdbApi';
|
|
1
|
+
import TmdbApiClient, { WatchProvidersResponse } from './tmdbApi';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts and normalizes platform names from a WatchProvidersResponse.
|
|
4
|
+
* This is used when watch providers are fetched via append_to_response.
|
|
5
|
+
* @param providersData WatchProvidersResponse data (can be from append_to_response)
|
|
6
|
+
* @param region Region code (default: "CA")
|
|
7
|
+
* @returns Array of normalized platform names
|
|
8
|
+
*/
|
|
9
|
+
export declare function extractPlatformsFromProviders(providersData: WatchProvidersResponse | undefined, region?: string): string[];
|
|
2
10
|
/**
|
|
3
11
|
* Fetch and normalize Canadian watch providers for a movie.
|
|
4
12
|
* @param movieId TMDb movie ID
|
package/dist/providers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,EAAE,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAmBlE;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,sBAAsB,GAAG,SAAS,EACjD,MAAM,SAAO,GACZ,MAAM,EAAE,CAoBV;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,MAAM,SAAO,EACb,MAAM,CAAC,EAAE,aAAa,GACrB,OAAO,CAAC,MAAM,EAAE,CAAC,CAwCnB"}
|
package/dist/providers.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.extractPlatformsFromProviders = extractPlatformsFromProviders;
|
|
6
7
|
exports.getCanadianProviders = getCanadianProviders;
|
|
7
8
|
// src/providers.ts
|
|
8
9
|
const tmdbApi_1 = __importDefault(require("./tmdbApi"));
|
|
@@ -22,6 +23,32 @@ const PLATFORM_MAP = {
|
|
|
22
23
|
fuboTV: 'fuboTV',
|
|
23
24
|
// Add more mappings as needed
|
|
24
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Extracts and normalizes platform names from a WatchProvidersResponse.
|
|
28
|
+
* This is used when watch providers are fetched via append_to_response.
|
|
29
|
+
* @param providersData WatchProvidersResponse data (can be from append_to_response)
|
|
30
|
+
* @param region Region code (default: "CA")
|
|
31
|
+
* @returns Array of normalized platform names
|
|
32
|
+
*/
|
|
33
|
+
function extractPlatformsFromProviders(providersData, region = 'CA') {
|
|
34
|
+
if (!providersData ||
|
|
35
|
+
!providersData.results ||
|
|
36
|
+
!providersData.results[region]) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
const regionData = providersData.results[region];
|
|
40
|
+
// Only consider 'flatrate' (subscription) providers
|
|
41
|
+
const flatrate = regionData.flatrate || [];
|
|
42
|
+
if (!Array.isArray(flatrate) || flatrate.length === 0) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
// Map provider names to allowed platforms
|
|
46
|
+
const platforms = flatrate
|
|
47
|
+
.map((p) => PLATFORM_MAP[p.provider_name])
|
|
48
|
+
.filter(Boolean);
|
|
49
|
+
// Remove duplicates
|
|
50
|
+
return Array.from(new Set(platforms));
|
|
51
|
+
}
|
|
25
52
|
/**
|
|
26
53
|
* Fetch and normalize Canadian watch providers for a movie.
|
|
27
54
|
* @param movieId TMDb movie ID
|
package/dist/providers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":";;;;;AA2BA,oDA4CC;
|
|
1
|
+
{"version":3,"file":"providers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CAAM,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC;aAC9C,MAAM,CAAC,OAAO,CAAC,CAAC;QACnB,oBAAoB;QACpB,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;QAE9C,iBAAiB;QACjB,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAE5B,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import pLimit from 'p-limit';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for rate limiting
|
|
4
|
+
*/
|
|
5
|
+
export interface RateLimiterConfig {
|
|
6
|
+
/** Maximum concurrent requests (default: 10) */
|
|
7
|
+
concurrency: number;
|
|
8
|
+
/** Maximum retry attempts for rate limit errors (default: 3) */
|
|
9
|
+
maxRetries: number;
|
|
10
|
+
/** Base delay in milliseconds for exponential backoff (default: 1000) */
|
|
11
|
+
baseDelayMs: number;
|
|
12
|
+
/** Maximum delay in milliseconds for exponential backoff (default: 30000) */
|
|
13
|
+
maxDelayMs: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Default rate limiter configuration
|
|
17
|
+
* TMDb allows 40 requests per 10 seconds, so we limit to 10 concurrent requests
|
|
18
|
+
* to stay well under the limit
|
|
19
|
+
*/
|
|
20
|
+
export declare const DEFAULT_RATE_LIMITER_CONFIG: RateLimiterConfig;
|
|
21
|
+
/**
|
|
22
|
+
* Rate limiter instance type
|
|
23
|
+
*/
|
|
24
|
+
export type RateLimiter = ReturnType<typeof pLimit>;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a rate limiter with the specified concurrency
|
|
27
|
+
* @param concurrency - Maximum number of concurrent operations
|
|
28
|
+
* @returns A rate limiter function
|
|
29
|
+
*/
|
|
30
|
+
export declare function createRateLimiter(concurrency?: number): RateLimiter;
|
|
31
|
+
/**
|
|
32
|
+
* Calculates exponential backoff delay with jitter
|
|
33
|
+
* @param attempt - Current retry attempt (0-based)
|
|
34
|
+
* @param baseDelayMs - Base delay in milliseconds
|
|
35
|
+
* @param maxDelayMs - Maximum delay cap in milliseconds
|
|
36
|
+
* @returns Delay in milliseconds
|
|
37
|
+
*/
|
|
38
|
+
export declare function calculateBackoffDelay(attempt: number, baseDelayMs?: number, maxDelayMs?: number): number;
|
|
39
|
+
/**
|
|
40
|
+
* Delays execution for the specified duration
|
|
41
|
+
* @param ms - Delay in milliseconds
|
|
42
|
+
* @returns Promise that resolves after the delay
|
|
43
|
+
*/
|
|
44
|
+
export declare function delay(ms: number): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if an error is a rate limit error (HTTP 429)
|
|
47
|
+
* @param error - The error to check
|
|
48
|
+
* @returns True if the error is a rate limit error
|
|
49
|
+
*/
|
|
50
|
+
export declare function isRateLimitError(error: unknown): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Executes an async function with exponential backoff retry on rate limit errors
|
|
53
|
+
* @param fn - The async function to execute
|
|
54
|
+
* @param config - Rate limiter configuration
|
|
55
|
+
* @returns Promise resolving to the function result
|
|
56
|
+
* @throws The last error if all retries are exhausted
|
|
57
|
+
*/
|
|
58
|
+
export declare function withRetry<T>(fn: () => Promise<T>, config?: Partial<RateLimiterConfig>): Promise<T>;
|
|
59
|
+
//# sourceMappingURL=rateLimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rateLimiter.d.ts","sourceRoot":"","sources":["../src/rateLimiter.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,SAAS,CAAC;AAE7B;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,gDAAgD;IAChD,WAAW,EAAE,MAAM,CAAC;IACpB,gEAAgE;IAChE,UAAU,EAAE,MAAM,CAAC;IACnB,yEAAyE;IACzE,WAAW,EAAE,MAAM,CAAC;IACpB,6EAA6E;IAC7E,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,EAAE,iBAKzC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC;AAEpD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,WAAW,GAAE,MAAgD,GAC5D,WAAW,CAEb;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EACf,WAAW,GAAE,MAAgD,EAC7D,UAAU,GAAE,MAA+C,GAC1D,MAAM,CAOR;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOxD;AAED;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,GAAE,OAAO,CAAC,iBAAiB,CAAM,GACtC,OAAO,CAAC,CAAC,CAAC,CA4BZ"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DEFAULT_RATE_LIMITER_CONFIG = void 0;
|
|
7
|
+
exports.createRateLimiter = createRateLimiter;
|
|
8
|
+
exports.calculateBackoffDelay = calculateBackoffDelay;
|
|
9
|
+
exports.delay = delay;
|
|
10
|
+
exports.isRateLimitError = isRateLimitError;
|
|
11
|
+
exports.withRetry = withRetry;
|
|
12
|
+
// src/rateLimiter.ts
|
|
13
|
+
const p_limit_1 = __importDefault(require("p-limit"));
|
|
14
|
+
/**
|
|
15
|
+
* Default rate limiter configuration
|
|
16
|
+
* TMDb allows 40 requests per 10 seconds, so we limit to 10 concurrent requests
|
|
17
|
+
* to stay well under the limit
|
|
18
|
+
*/
|
|
19
|
+
exports.DEFAULT_RATE_LIMITER_CONFIG = {
|
|
20
|
+
concurrency: 10,
|
|
21
|
+
maxRetries: 3,
|
|
22
|
+
baseDelayMs: 1000,
|
|
23
|
+
maxDelayMs: 30000,
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Creates a rate limiter with the specified concurrency
|
|
27
|
+
* @param concurrency - Maximum number of concurrent operations
|
|
28
|
+
* @returns A rate limiter function
|
|
29
|
+
*/
|
|
30
|
+
function createRateLimiter(concurrency = exports.DEFAULT_RATE_LIMITER_CONFIG.concurrency) {
|
|
31
|
+
return (0, p_limit_1.default)(concurrency);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Calculates exponential backoff delay with jitter
|
|
35
|
+
* @param attempt - Current retry attempt (0-based)
|
|
36
|
+
* @param baseDelayMs - Base delay in milliseconds
|
|
37
|
+
* @param maxDelayMs - Maximum delay cap in milliseconds
|
|
38
|
+
* @returns Delay in milliseconds
|
|
39
|
+
*/
|
|
40
|
+
function calculateBackoffDelay(attempt, baseDelayMs = exports.DEFAULT_RATE_LIMITER_CONFIG.baseDelayMs, maxDelayMs = exports.DEFAULT_RATE_LIMITER_CONFIG.maxDelayMs) {
|
|
41
|
+
// Exponential backoff: baseDelay * 2^attempt
|
|
42
|
+
const exponentialDelay = baseDelayMs * Math.pow(2, attempt);
|
|
43
|
+
// Add jitter (0-25% of the delay) to avoid thundering herd
|
|
44
|
+
const jitter = exponentialDelay * Math.random() * 0.25;
|
|
45
|
+
// Cap at maxDelayMs
|
|
46
|
+
return Math.min(exponentialDelay + jitter, maxDelayMs);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Delays execution for the specified duration
|
|
50
|
+
* @param ms - Delay in milliseconds
|
|
51
|
+
* @returns Promise that resolves after the delay
|
|
52
|
+
*/
|
|
53
|
+
function delay(ms) {
|
|
54
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Checks if an error is a rate limit error (HTTP 429)
|
|
58
|
+
* @param error - The error to check
|
|
59
|
+
* @returns True if the error is a rate limit error
|
|
60
|
+
*/
|
|
61
|
+
function isRateLimitError(error) {
|
|
62
|
+
if (error instanceof Error) {
|
|
63
|
+
return (error.message.includes('429') || error.message.includes('rate limit'));
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Executes an async function with exponential backoff retry on rate limit errors
|
|
69
|
+
* @param fn - The async function to execute
|
|
70
|
+
* @param config - Rate limiter configuration
|
|
71
|
+
* @returns Promise resolving to the function result
|
|
72
|
+
* @throws The last error if all retries are exhausted
|
|
73
|
+
*/
|
|
74
|
+
async function withRetry(fn, config = {}) {
|
|
75
|
+
const { maxRetries, baseDelayMs, maxDelayMs } = {
|
|
76
|
+
...exports.DEFAULT_RATE_LIMITER_CONFIG,
|
|
77
|
+
...config,
|
|
78
|
+
};
|
|
79
|
+
let lastError;
|
|
80
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
81
|
+
try {
|
|
82
|
+
return await fn();
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
lastError = error;
|
|
86
|
+
// Only retry on rate limit errors
|
|
87
|
+
if (!isRateLimitError(error)) {
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
// Don't delay after the last attempt
|
|
91
|
+
if (attempt < maxRetries) {
|
|
92
|
+
const delayMs = calculateBackoffDelay(attempt, baseDelayMs, maxDelayMs);
|
|
93
|
+
await delay(delayMs);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
throw lastError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C3B,OAAO,CACL,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CACtE,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,SAAS,CAC7B,EAAoB,EACpB,SAAqC,EAAE;IAEvC,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG;QAC9C,GAAG,mCAA2B;QAC9B,GAAG,MAAM;KACV,CAAC;IAEF,IAAI,SAAkB,CAAC;IAEvB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC;QACvD,IAAI,CAAC;YACH,OAAO,MAAM,EAAE,EAAE,CAAC;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,CAAC;YAElB,kCAAkC;YAClC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC7B,MAAM,KAAK,CAAC;YACd,CAAC;YAED,qCAAqC;YACrC,IAAI,OAAO,GAAG,UAAU,EAAE,CAAC;gBACzB,MAAM,OAAO,GAAG,qBAAqB,CAAC,OAAO,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;gBACxE,MAAM,KAAK,CAAC,OAAO,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,SAAS,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sanitizes user input to prevent prompt injection attacks
|
|
3
|
+
* This is specifically for content that will be passed to LLM prompts
|
|
4
|
+
*
|
|
5
|
+
* @param input - Raw user input object
|
|
6
|
+
* @returns Sanitized version safe for LLM prompts
|
|
7
|
+
*/
|
|
8
|
+
export declare function sanitizeForLLMPrompt(input: any): any;
|
|
9
|
+
/**
|
|
10
|
+
* Sanitizes a string to remove potential prompt injection attacks
|
|
11
|
+
*
|
|
12
|
+
* @param str - Input string to sanitize
|
|
13
|
+
* @returns Sanitized string
|
|
14
|
+
*/
|
|
15
|
+
export declare function sanitizeString(str: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Detects if input contains potential prompt injection attempts
|
|
18
|
+
* This is for logging/monitoring purposes
|
|
19
|
+
*
|
|
20
|
+
* @param input - Input to check
|
|
21
|
+
* @returns True if potential injection detected
|
|
22
|
+
*/
|
|
23
|
+
export declare function detectPromptInjection(input: any): boolean;
|
|
24
|
+
//# sourceMappingURL=sanitize.d.ts.map
|