@lapage/ai-agent 1.0.0
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 +487 -0
- package/dist/index.d.ts +119 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +349 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +6 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/memory-manager.d.ts +27 -0
- package/dist/memory/memory-manager.d.ts.map +1 -0
- package/dist/memory/memory-manager.js +175 -0
- package/dist/memory/memory-manager.js.map +1 -0
- package/dist/models/embeddings-factory.d.ts +6 -0
- package/dist/models/embeddings-factory.d.ts.map +1 -0
- package/dist/models/embeddings-factory.js +84 -0
- package/dist/models/embeddings-factory.js.map +1 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +8 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/model-factory.d.ts +6 -0
- package/dist/models/model-factory.d.ts.map +1 -0
- package/dist/models/model-factory.js +75 -0
- package/dist/models/model-factory.js.map +1 -0
- package/dist/tools/index.d.ts +34 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +106 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/json-schema-to-zod.d.ts +8 -0
- package/dist/tools/json-schema-to-zod.d.ts.map +1 -0
- package/dist/tools/json-schema-to-zod.js +191 -0
- package/dist/tools/json-schema-to-zod.js.map +1 -0
- package/dist/tools/mcp-client.d.ts +8 -0
- package/dist/tools/mcp-client.d.ts.map +1 -0
- package/dist/tools/mcp-client.js +125 -0
- package/dist/tools/mcp-client.js.map +1 -0
- package/dist/tools/vector-tools.d.ts +44 -0
- package/dist/tools/vector-tools.d.ts.map +1 -0
- package/dist/tools/vector-tools.js +174 -0
- package/dist/tools/vector-tools.js.map +1 -0
- package/dist/types/index.d.ts +158 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMcpServerTools = getMcpServerTools;
|
|
4
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
5
|
+
const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
6
|
+
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
7
|
+
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
8
|
+
const tools_1 = require("@langchain/core/tools");
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const json_schema_to_zod_1 = require("./json-schema-to-zod");
|
|
11
|
+
/**
|
|
12
|
+
* Recursively fetch all tools from the MCP server, following pagination cursors.
|
|
13
|
+
*/
|
|
14
|
+
async function getAllMcpTools(client, cursor) {
|
|
15
|
+
const { tools, nextCursor } = await client.listTools({ cursor });
|
|
16
|
+
const mcpTools = tools;
|
|
17
|
+
if (nextCursor) {
|
|
18
|
+
return mcpTools.concat(await getAllMcpTools(client, nextCursor));
|
|
19
|
+
}
|
|
20
|
+
return mcpTools;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convert an MCP tool definition to a LangChain DynamicStructuredTool.
|
|
24
|
+
* The JSON Schema inputSchema is converted to a Zod object schema so the
|
|
25
|
+
* agent can validate and pass structured arguments.
|
|
26
|
+
*/
|
|
27
|
+
function mcpToolToLangChainTool(tool, client, timeout) {
|
|
28
|
+
const rawSchema = (0, json_schema_to_zod_1.convertJsonSchemaToZod)(tool.inputSchema);
|
|
29
|
+
// DynamicStructuredTool requires a ZodObject at the top level
|
|
30
|
+
const objectSchema = rawSchema instanceof zod_1.z.ZodObject ? rawSchema : zod_1.z.object({ value: rawSchema });
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
return new tools_1.DynamicStructuredTool({
|
|
33
|
+
name: tool.name,
|
|
34
|
+
description: tool.description ?? '',
|
|
35
|
+
schema: objectSchema,
|
|
36
|
+
func: async (args) => {
|
|
37
|
+
try {
|
|
38
|
+
const result = await client.callTool({ name: tool.name, arguments: args }, types_js_1.CompatibilityCallToolResultSchema, { timeout });
|
|
39
|
+
if (result.isError) {
|
|
40
|
+
const errorMsg = extractTextFromContent(result) ?? `Tool "${tool.name}" returned an error`;
|
|
41
|
+
return errorMsg;
|
|
42
|
+
}
|
|
43
|
+
if (result.toolResult !== undefined) {
|
|
44
|
+
return typeof result.toolResult === 'string'
|
|
45
|
+
? result.toolResult
|
|
46
|
+
: JSON.stringify(result.toolResult);
|
|
47
|
+
}
|
|
48
|
+
if (result.content !== undefined) {
|
|
49
|
+
const text = extractTextFromContent(result);
|
|
50
|
+
return text ?? JSON.stringify(result.content);
|
|
51
|
+
}
|
|
52
|
+
return JSON.stringify(result);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
return `Error calling MCP tool "${tool.name}": ${error instanceof Error ? error.message : String(error)}`;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
metadata: { mcpTool: true, mcpServer: tool.name },
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function extractTextFromContent(result) {
|
|
62
|
+
if (result?.content && Array.isArray(result.content)) {
|
|
63
|
+
const textEntry = result.content.find((c) => c.type === 'text' && typeof c.text === 'string');
|
|
64
|
+
return textEntry?.text;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Connect to an MCP server and return its tools as LangChain DynamicStructuredTool instances.
|
|
70
|
+
* Supports both SSE and HTTP Streamable transports.
|
|
71
|
+
*/
|
|
72
|
+
async function getMcpServerTools(config) {
|
|
73
|
+
const { url, transport = 'httpStreamable', headers, name = 'ai-agent-mcp-client', timeout = 60_000, toolMode = 'all', includeTools, excludeTools, } = config;
|
|
74
|
+
const endpointUrl = normalizeUrl(url);
|
|
75
|
+
const client = new index_js_1.Client({ name, version: '1' }, { capabilities: {} });
|
|
76
|
+
if (transport === 'httpStreamable') {
|
|
77
|
+
const httpTransport = new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(endpointUrl), {
|
|
78
|
+
requestInit: { headers },
|
|
79
|
+
});
|
|
80
|
+
await client.connect(httpTransport);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const sseTransport = new sse_js_1.SSEClientTransport(new URL(endpointUrl), {
|
|
84
|
+
eventSourceInit: {
|
|
85
|
+
fetch: async (url, init) => fetch(url, {
|
|
86
|
+
...init,
|
|
87
|
+
headers: {
|
|
88
|
+
...(init?.headers ?? {}),
|
|
89
|
+
...(headers ?? {}),
|
|
90
|
+
Accept: 'text/event-stream',
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
94
|
+
requestInit: { headers },
|
|
95
|
+
});
|
|
96
|
+
await client.connect(sseTransport);
|
|
97
|
+
}
|
|
98
|
+
const allTools = await getAllMcpTools(client);
|
|
99
|
+
const filteredTools = filterTools(allTools, toolMode, includeTools, excludeTools);
|
|
100
|
+
return filteredTools.map((tool) => mcpToolToLangChainTool(tool, client, timeout));
|
|
101
|
+
}
|
|
102
|
+
function normalizeUrl(url) {
|
|
103
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
104
|
+
return `https://${url}`;
|
|
105
|
+
}
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
108
|
+
function filterTools(tools, mode, includeTools, excludeTools) {
|
|
109
|
+
switch (mode) {
|
|
110
|
+
case 'selected': {
|
|
111
|
+
if (!includeTools?.length)
|
|
112
|
+
return tools;
|
|
113
|
+
const include = new Set(includeTools);
|
|
114
|
+
return tools.filter((t) => include.has(t.name));
|
|
115
|
+
}
|
|
116
|
+
case 'except': {
|
|
117
|
+
const except = new Set(excludeTools ?? []);
|
|
118
|
+
return tools.filter((t) => !except.has(t.name));
|
|
119
|
+
}
|
|
120
|
+
case 'all':
|
|
121
|
+
default:
|
|
122
|
+
return tools;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=mcp-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../src/tools/mcp-client.ts"],"names":[],"mappings":";;AAiGA,8CA0CC;AA3ID,wEAAmE;AACnE,oEAA6E;AAC7E,0FAAmG;AACnG,iEAAuF;AACvF,iDAA8D;AAE9D,6BAAwB;AAExB,6DAA8D;AAK9D;;GAEG;AACH,KAAK,UAAU,cAAc,CAAC,MAAc,EAAE,MAAe;IAC3D,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IACjE,MAAM,QAAQ,GAAG,KAAkB,CAAC;IAEpC,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,QAAQ,CAAC,MAAM,CAAC,MAAM,cAAc,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IACnE,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,SAAS,sBAAsB,CAC7B,IAAa,EACb,MAAc,EACd,OAAe;IAEf,MAAM,SAAS,GAAG,IAAA,2CAAsB,EAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAE3D,8DAA8D;IAC9D,MAAM,YAAY,GAChB,SAAS,YAAY,OAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAEhF,8DAA8D;IAC9D,OAAO,IAAK,6BAA6B,CAAC;QACxC,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,WAAW,EAAE,IAAI,CAAC,WAAW,IAAI,EA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;QAClD,CAAC;QACD,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;YAC3C,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAClD,CAAC;QACD,KAAK,KAAK,CAAC;QACX;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Embeddings } from '@langchain/core/embeddings';
|
|
2
|
+
import { ToolOptions } from '../types';
|
|
3
|
+
export interface PGVectorConfig {
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
database: string;
|
|
7
|
+
user: string;
|
|
8
|
+
password: string;
|
|
9
|
+
tableName?: string;
|
|
10
|
+
collectionName?: string;
|
|
11
|
+
collectionTableName?: string;
|
|
12
|
+
columns?: {
|
|
13
|
+
idColumnName?: string;
|
|
14
|
+
vectorColumnName?: string;
|
|
15
|
+
contentColumnName?: string;
|
|
16
|
+
metadataColumnName?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface RetrieveDocumentsConfig {
|
|
20
|
+
pgConfig: PGVectorConfig;
|
|
21
|
+
embeddings: Embeddings;
|
|
22
|
+
topK?: number;
|
|
23
|
+
includeMetadata?: boolean;
|
|
24
|
+
metadataFilter?: Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Output format options for the vector search tool
|
|
28
|
+
*/
|
|
29
|
+
export type VectorSearchOutputFormat = 'structured' | 'n8n' | 'simple';
|
|
30
|
+
export interface VectorSearchToolConfig extends RetrieveDocumentsConfig {
|
|
31
|
+
/** Tool name (defaults to 'search_documents') */
|
|
32
|
+
toolName?: string;
|
|
33
|
+
/** Tool description */
|
|
34
|
+
toolDescription?: string;
|
|
35
|
+
/** Output format for results */
|
|
36
|
+
outputFormat?: VectorSearchOutputFormat;
|
|
37
|
+
/** Whether to allow dynamic topK parameter */
|
|
38
|
+
allowDynamicTopK?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a unified vector search tool with configurable behavior
|
|
42
|
+
*/
|
|
43
|
+
export declare function createVectorSearchTool(config: VectorSearchToolConfig): ToolOptions;
|
|
44
|
+
//# sourceMappingURL=vector-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-tools.d.ts","sourceRoot":"","sources":["../../src/tools/vector-tools.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAI7D,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAGvC,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,OAAO,CAAC,EAAE;QACR,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;KAC7B,CAAC;CACH;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,EAAE,cAAc,CAAC;IACzB,UAAU,EAAE,UAAU,CAAC;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACtC;AAoFD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEvE,MAAM,WAAW,sBAAuB,SAAQ,uBAAuB;IACrE,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uBAAuB;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gCAAgC;IAChC,YAAY,CAAC,EAAE,wBAAwB,CAAC;IACxC,8CAA8C;IAC9C,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAmGD;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,sBAAsB,GAC7B,WAAW,CAiDb"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createVectorSearchTool = createVectorSearchTool;
|
|
4
|
+
const pgvector_1 = require("@langchain/community/vectorstores/pgvector");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
/**
|
|
7
|
+
* Extended PGVectorStore class to handle custom filtering.
|
|
8
|
+
* This wrapper is necessary because when used as a retriever,
|
|
9
|
+
* similaritySearchVectorWithScore should use this.filter instead of
|
|
10
|
+
* expecting it from the parameter
|
|
11
|
+
*/
|
|
12
|
+
class ExtendedPGVectorStore extends pgvector_1.PGVectorStore {
|
|
13
|
+
static async initialize(embeddings, args) {
|
|
14
|
+
const { dimensions, ...rest } = args;
|
|
15
|
+
const postgresqlVectorStore = new this(embeddings, rest);
|
|
16
|
+
await postgresqlVectorStore._initializeClient();
|
|
17
|
+
await postgresqlVectorStore.ensureTableInDatabase(dimensions);
|
|
18
|
+
if (postgresqlVectorStore.collectionTableName) {
|
|
19
|
+
await postgresqlVectorStore.ensureCollectionTableInDatabase();
|
|
20
|
+
}
|
|
21
|
+
return postgresqlVectorStore;
|
|
22
|
+
}
|
|
23
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
24
|
+
const mergedFilter = { ...this.filter, ...filter };
|
|
25
|
+
return await super.similaritySearchVectorWithScore(query, k, mergedFilter);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a PostgreSQL connection pool
|
|
30
|
+
*/
|
|
31
|
+
function createPostgresPool(config) {
|
|
32
|
+
const Pool = require('pg').Pool;
|
|
33
|
+
return new Pool({
|
|
34
|
+
host: config.host,
|
|
35
|
+
port: config.port,
|
|
36
|
+
database: config.database,
|
|
37
|
+
user: config.user,
|
|
38
|
+
password: config.password,
|
|
39
|
+
max: 20,
|
|
40
|
+
idleTimeoutMillis: 30000,
|
|
41
|
+
connectionTimeoutMillis: 2000,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a PGVector store configuration
|
|
46
|
+
*/
|
|
47
|
+
function createPGVectorConfig(config, pool, filter) {
|
|
48
|
+
const pgVectorConfig = {
|
|
49
|
+
pool,
|
|
50
|
+
tableName: config.tableName || 'ai_agent_vectors',
|
|
51
|
+
filter,
|
|
52
|
+
};
|
|
53
|
+
if (config.collectionName) {
|
|
54
|
+
pgVectorConfig.collectionName = config.collectionName;
|
|
55
|
+
pgVectorConfig.collectionTableName =
|
|
56
|
+
config.collectionTableName || 'ai_agent_vector_collections';
|
|
57
|
+
}
|
|
58
|
+
if (config.columns) {
|
|
59
|
+
pgVectorConfig.columns = {
|
|
60
|
+
idColumnName: config.columns.idColumnName || 'id',
|
|
61
|
+
vectorColumnName: config.columns.vectorColumnName || 'embedding',
|
|
62
|
+
contentColumnName: config.columns.contentColumnName || 'text',
|
|
63
|
+
metadataColumnName: config.columns.metadataColumnName || 'metadata',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return pgVectorConfig;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Core function to perform vector similarity search
|
|
70
|
+
*/
|
|
71
|
+
async function performVectorSearch(query, config, topK) {
|
|
72
|
+
// Create PostgreSQL pool
|
|
73
|
+
const pool = createPostgresPool(config.pgConfig);
|
|
74
|
+
try {
|
|
75
|
+
// Create PGVector configuration
|
|
76
|
+
const pgVectorConfig = createPGVectorConfig(config.pgConfig, pool, config.metadataFilter);
|
|
77
|
+
// Initialize the vector store
|
|
78
|
+
const vectorStore = await ExtendedPGVectorStore.initialize(config.embeddings, pgVectorConfig);
|
|
79
|
+
try {
|
|
80
|
+
// Embed the query
|
|
81
|
+
const embeddedQuery = await config.embeddings.embedQuery(query);
|
|
82
|
+
// Search for similar documents
|
|
83
|
+
return await vectorStore.similaritySearchVectorWithScore(embeddedQuery, topK, config.metadataFilter);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
// Release the vector store client
|
|
87
|
+
vectorStore.client?.release();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
// Close the pool
|
|
92
|
+
await pool.end();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Format search results based on the specified output format
|
|
97
|
+
*/
|
|
98
|
+
function formatSearchResults(documents, format, includeMetadata) {
|
|
99
|
+
switch (format) {
|
|
100
|
+
case 'structured':
|
|
101
|
+
const results = documents.map(([doc, score]) => {
|
|
102
|
+
const result = {
|
|
103
|
+
pageContent: doc.pageContent,
|
|
104
|
+
score: score,
|
|
105
|
+
};
|
|
106
|
+
if (includeMetadata) {
|
|
107
|
+
result.metadata = doc.metadata;
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
});
|
|
111
|
+
return JSON.stringify({
|
|
112
|
+
success: true,
|
|
113
|
+
documents: results,
|
|
114
|
+
count: results.length,
|
|
115
|
+
});
|
|
116
|
+
case 'n8n':
|
|
117
|
+
return documents
|
|
118
|
+
.map(([doc]) => {
|
|
119
|
+
if (includeMetadata) {
|
|
120
|
+
return { type: 'text', text: JSON.stringify(doc) };
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
type: 'text',
|
|
124
|
+
text: JSON.stringify({ pageContent: doc.pageContent }),
|
|
125
|
+
};
|
|
126
|
+
})
|
|
127
|
+
.filter((document) => !!document);
|
|
128
|
+
case 'simple':
|
|
129
|
+
default:
|
|
130
|
+
return documents.map(([doc, score]) => ({
|
|
131
|
+
content: doc.pageContent,
|
|
132
|
+
score: score,
|
|
133
|
+
...(includeMetadata && { metadata: doc.metadata }),
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Creates a unified vector search tool with configurable behavior
|
|
139
|
+
*/
|
|
140
|
+
function createVectorSearchTool(config) {
|
|
141
|
+
const { toolName = 'search_documents', toolDescription = 'Search for relevant documents using vector similarity search.', outputFormat = 'simple', allowDynamicTopK = true, ...searchConfig } = config;
|
|
142
|
+
const baseSchema = zod_1.z.object({
|
|
143
|
+
query: zod_1.z.string().describe('The search query to find similar documents'),
|
|
144
|
+
});
|
|
145
|
+
let schema;
|
|
146
|
+
if (allowDynamicTopK) {
|
|
147
|
+
schema = baseSchema.extend({
|
|
148
|
+
topK: zod_1.z
|
|
149
|
+
.number()
|
|
150
|
+
.int()
|
|
151
|
+
.min(1)
|
|
152
|
+
.max(50)
|
|
153
|
+
.nullable()
|
|
154
|
+
.describe(`Number of documents to retrieve (1-50). Pass null to use the default value (${searchConfig.topK || 4})`),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
schema = baseSchema;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
name: toolName,
|
|
162
|
+
description: toolDescription,
|
|
163
|
+
schema,
|
|
164
|
+
func: async (args) => {
|
|
165
|
+
const { query } = args;
|
|
166
|
+
const topK = allowDynamicTopK
|
|
167
|
+
? args.topK ?? searchConfig.topK ?? 4
|
|
168
|
+
: searchConfig.topK || 4;
|
|
169
|
+
const documents = await performVectorSearch(query, searchConfig, topK);
|
|
170
|
+
return formatSearchResults(documents, outputFormat, searchConfig.includeMetadata !== false);
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=vector-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-tools.js","sourceRoot":"","sources":["../../src/tools/vector-tools.ts"],"names":[],"mappings":";;AA2OA,wDAmDC;AA9RD,yEAGoD;AAKpD,6BAAwB;AA6BxB;;;;;GAKG;AACH,MAAM,qBAAsB,SAAQ,wBAAa;IAC/C,MAAM,CAAC,KAAK,CAAC,UAAU,CACrB,UAAsB,EACtB,IAAiD;QAEjD,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC;QACrC,MAAM,qBAAqB,GAAG,IAAI,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAEzD,MAAM,qBAAqB,CAAC,iBAAiB,EAAE,CAAC;QAChD,MAAM,qBAAqB,CAAC,qBAAqB,CAAC,UAAU,CAAC,CAAC;QAC9D,IAAI,qBAAqB,CAAC,mBAAmB,EAAE,CAAC;YAC9C,MAAM,qBAAqB,CAAC,+BAA+B,EAAE,CAAC;QAChE,CAAC;QAED,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,+BAA+B,CACnC,KAAe,EACf,CAAS,EACT,MAAoC;QAEpC,MAAM,YAAY,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;QACnD,OAAO,MAAM,KAAK,CAAC,+BAA+B,CAAC,KAAK,EAAE,CAAC,EAAE,YAAY,CAAC,CAAC;IAC7E,CAAC;CACF;AAED;;GAEG;AACH,SAAS,kBAAkB,CAAC,MAAsB;IAChD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;IAChC,OAAO,IAAI,IAAI,CAAC;QACd,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,QAAQ,EAAE,MAAM,CAAC,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BACL,GAAG,CAAC,CAAC,CAAC;iBACN,GAAG,CAAC,EAAE,CAAC;iBACP,QAAQ,EAAE;iBACV,QAAQ,CACP,+EAA+E,YAAY,CAAC,IAAI,IAAI,CAAC,GAAG,CACzG;SACJ,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,MAAM,GAAG,UAAU,CAAC;IACtB,CAAC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,eAAe;QAC5B,MAAM;QACN,IAAI,EAAE,KAAK,EAAE,IAA4B,EAAE,EAAE;YAC3C,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;YACvB,MAAM,IAAI,GAAG,gBAAgB;gBAC3B,CAAC,CAAE,IAAY,CAAC,IAAI,IAAI,YAAY,CAAC,IAAI,IAAI,CAAC;gBAC9C,CAAC,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC,CAAC;YAE3B,MAAM,SAAS,GAAG,MAAM,mBAAmB,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC;YAEvE,OAAO,mBAAmB,CACxB,SAAS,EACT,YAAY,EACZ,YAAY,CAAC,eAAe,KAAK,KAAK,CACvC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
export interface AgentAction {
|
|
4
|
+
/** Name of the tool to execute */
|
|
5
|
+
tool: string;
|
|
6
|
+
/** Input parameters for the tool */
|
|
7
|
+
toolInput: any;
|
|
8
|
+
/** Tool call ID for tracking */
|
|
9
|
+
toolCallId: string;
|
|
10
|
+
/** Log message for the action */
|
|
11
|
+
log: string;
|
|
12
|
+
/** Array of messages in the conversation */
|
|
13
|
+
messageLog: any[];
|
|
14
|
+
}
|
|
15
|
+
export interface AgentStep {
|
|
16
|
+
/** The action taken by the agent */
|
|
17
|
+
action: AgentAction;
|
|
18
|
+
/** The observation/result from executing the action */
|
|
19
|
+
observation: string;
|
|
20
|
+
}
|
|
21
|
+
export interface AIAgentOptions {
|
|
22
|
+
/** The chat model to use for the agent */
|
|
23
|
+
model: ModelConfig;
|
|
24
|
+
/** System message to provide context to the agent */
|
|
25
|
+
systemMessage?: string;
|
|
26
|
+
/** Maximum number of iterations the agent can perform */
|
|
27
|
+
maxIterations?: number;
|
|
28
|
+
/** Whether to return intermediate steps in the response */
|
|
29
|
+
returnIntermediateSteps?: boolean;
|
|
30
|
+
/** Whether to passthrough binary images */
|
|
31
|
+
passthroughBinaryImages?: boolean;
|
|
32
|
+
/** Memory configuration for conversation history */
|
|
33
|
+
memory?: MemoryConfig;
|
|
34
|
+
/** Array of tools available to the agent */
|
|
35
|
+
tools?: ToolOptions[];
|
|
36
|
+
/** Knowledge bases configuration for automatic vector search tools */
|
|
37
|
+
knowledgeBases?: KnowledgeBaseConfig[];
|
|
38
|
+
/** MCP server configurations for automatic tool registration */
|
|
39
|
+
mcpServers?: MCPServerConfig[];
|
|
40
|
+
}
|
|
41
|
+
export interface ModelConfig {
|
|
42
|
+
provider: 'openai' | 'anthropic';
|
|
43
|
+
model: string;
|
|
44
|
+
temperature?: number;
|
|
45
|
+
maxTokens?: number;
|
|
46
|
+
apiKey?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface EmbeddingsConfig {
|
|
49
|
+
provider: 'openai' | 'cohere' | 'huggingface';
|
|
50
|
+
model: string;
|
|
51
|
+
apiKey?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface MemoryConfig {
|
|
54
|
+
/** Type of memory to use */
|
|
55
|
+
type?: 'buffer-window' | 'summary' | 'postgres';
|
|
56
|
+
/** Session ID for memory isolation. Auto-generated if not provided */
|
|
57
|
+
sessionId?: string;
|
|
58
|
+
/** For buffer window memory - number of messages to keep */
|
|
59
|
+
contextWindowLength?: number;
|
|
60
|
+
/** PostgreSQL configuration for postgres memory type */
|
|
61
|
+
postgresConfig?: PostgresConfig;
|
|
62
|
+
}
|
|
63
|
+
export interface AIAgentResponse {
|
|
64
|
+
/** The final output from the agent */
|
|
65
|
+
output: string;
|
|
66
|
+
/** Intermediate steps if returnIntermediateSteps is true */
|
|
67
|
+
intermediateSteps?: AgentStep[];
|
|
68
|
+
/** Error if execution failed */
|
|
69
|
+
error?: string;
|
|
70
|
+
}
|
|
71
|
+
export interface PostgresConfig {
|
|
72
|
+
/** Postgres host */
|
|
73
|
+
host: string;
|
|
74
|
+
/** Postgres port */
|
|
75
|
+
port?: number;
|
|
76
|
+
/** Database name */
|
|
77
|
+
database: string;
|
|
78
|
+
/** Username for Postgres connection */
|
|
79
|
+
user: string;
|
|
80
|
+
/** Password for Postgres connection */
|
|
81
|
+
password: string;
|
|
82
|
+
/** Table name for storing chat messages */
|
|
83
|
+
tableName?: string;
|
|
84
|
+
}
|
|
85
|
+
export interface ToolOptions {
|
|
86
|
+
/** Tool name */
|
|
87
|
+
name: string;
|
|
88
|
+
/** Tool description */
|
|
89
|
+
description: string;
|
|
90
|
+
/** Tool function */
|
|
91
|
+
func: (...args: any[]) => Promise<string> | string;
|
|
92
|
+
/** Tool schema for parameters - Zod schema or JSON Schema 7 defining the input structure */
|
|
93
|
+
schema?: z.ZodSchema | JSONSchema7;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Configuration for connecting to an MCP (Model Context Protocol) server.
|
|
97
|
+
* The agent will automatically register all tools exposed by the server.
|
|
98
|
+
*/
|
|
99
|
+
export interface MCPServerConfig {
|
|
100
|
+
/** The endpoint URL of the MCP server */
|
|
101
|
+
url: string;
|
|
102
|
+
/** Transport protocol to use (defaults to 'httpStreamable') */
|
|
103
|
+
transport?: 'sse' | 'httpStreamable';
|
|
104
|
+
/** Optional HTTP headers for authentication (e.g. Bearer token) */
|
|
105
|
+
headers?: Record<string, string>;
|
|
106
|
+
/** Display name for this MCP client (used in SDK metadata) */
|
|
107
|
+
name?: string;
|
|
108
|
+
/** Tool call timeout in milliseconds (defaults to 60000) */
|
|
109
|
+
timeout?: number;
|
|
110
|
+
/** Which tools to expose from this server */
|
|
111
|
+
toolMode?: 'all' | 'selected' | 'except';
|
|
112
|
+
/** Tools to include when toolMode is 'selected' */
|
|
113
|
+
includeTools?: string[];
|
|
114
|
+
/** Tools to exclude when toolMode is 'except' */
|
|
115
|
+
excludeTools?: string[];
|
|
116
|
+
}
|
|
117
|
+
export interface KnowledgeBaseConfig {
|
|
118
|
+
/** Name of the knowledge base (used as tool name) */
|
|
119
|
+
name: string;
|
|
120
|
+
/** Description to help the AI Agent understand when to query this knowledge base */
|
|
121
|
+
description: string;
|
|
122
|
+
/** PostgreSQL configuration for the vector store */
|
|
123
|
+
pgConfig: PGVectorConfig;
|
|
124
|
+
/** Embeddings configuration for the vector search */
|
|
125
|
+
embeddings: EmbeddingsConfig;
|
|
126
|
+
/** Number of documents to retrieve by default */
|
|
127
|
+
topK?: number;
|
|
128
|
+
/** Whether to include metadata in search results */
|
|
129
|
+
includeMetadata?: boolean;
|
|
130
|
+
/** Optional metadata filter to apply to searches */
|
|
131
|
+
metadataFilter?: Record<string, any>;
|
|
132
|
+
}
|
|
133
|
+
export interface PGVectorConfig {
|
|
134
|
+
/** Postgres host */
|
|
135
|
+
host: string;
|
|
136
|
+
/** Postgres port */
|
|
137
|
+
port: number;
|
|
138
|
+
/** Database name */
|
|
139
|
+
database: string;
|
|
140
|
+
/** Username for Postgres connection */
|
|
141
|
+
user: string;
|
|
142
|
+
/** Password for Postgres connection */
|
|
143
|
+
password: string;
|
|
144
|
+
/** Table name for storing vectors */
|
|
145
|
+
tableName?: string;
|
|
146
|
+
/** Collection name for organizing vectors */
|
|
147
|
+
collectionName?: string;
|
|
148
|
+
/** Collection table name */
|
|
149
|
+
collectionTableName?: string;
|
|
150
|
+
/** Custom column configuration */
|
|
151
|
+
columns?: {
|
|
152
|
+
idColumnName?: string;
|
|
153
|
+
vectorColumnName?: string;
|
|
154
|
+
contentColumnName?: string;
|
|
155
|
+
metadataColumnName?: string;
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAG7B,MAAM,WAAW,WAAW;IAC1B,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,SAAS,EAAE,GAAG,CAAC;IACf,gCAAgC;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,4CAA4C;IAC5C,UAAU,EAAE,GAAG,EAAE,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,oCAAoC;IACpC,MAAM,EAAE,WAAW,CAAC;IACpB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,0CAA0C;IAC1C,KAAK,EAAE,WAAW,CAAC;IAEnB,qDAAqD;IACrD,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,yDAAyD;IACzD,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,2DAA2D;IAC3D,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAElC,2CAA2C;IAC3C,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAElC,oDAAoD;IACpD,MAAM,CAAC,EAAE,YAAY,CAAC;IAEtB,4CAA4C;IAC5C,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IAEtB,sEAAsE;IACtE,cAAc,CAAC,EAAE,mBAAmB,EAAE,CAAC;IAEvC,gEAAgE;IAChE,UAAU,CAAC,EAAE,eAAe,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,QAAQ,GAAG,WAAW,C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CAAC;IACjB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,uCAAuC;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6CAA6C;IAC7C,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,4BAA4B;IAC5B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,kCAAkC;IAClC,OAAO,CAAC,EAAE;QACR,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;KAC7B,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lapage/ai-agent",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Standalone AI Agent library built on LangChain with MCP tool calling, memory, and vector knowledge base support",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"dev": "tsc --watch",
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"lint": "eslint src/**/*.ts",
|
|
19
|
+
"clean": "rm -rf dist",
|
|
20
|
+
"validate": "node validate.js",
|
|
21
|
+
"prepublishOnly": "npm run clean && npm run build && npm run validate",
|
|
22
|
+
"demo:postgres": "NODE_NO_WARNINGS=1 dotenv -- ts-node examples/postgres-demo.ts",
|
|
23
|
+
"demo:basic": "NODE_NO_WARNINGS=1 dotenv -- ts-node examples/basic-usage.ts",
|
|
24
|
+
"demo:openai": "NODE_NO_WARNINGS=1 dotenv -- ts-node examples/openai-examples.ts",
|
|
25
|
+
"demo:anthropic": "NODE_NO_WARNINGS=1 dotenv -- ts-node examples/anthropic-examples.ts"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"ai",
|
|
29
|
+
"agent",
|
|
30
|
+
"langchain",
|
|
31
|
+
"tools",
|
|
32
|
+
"llm",
|
|
33
|
+
"chat"
|
|
34
|
+
],
|
|
35
|
+
"author": "Huy Lan",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@langchain/community": "^0.3.0",
|
|
39
|
+
"@langchain/core": "^0.3.0",
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
41
|
+
"@types/json-schema": "^7.0.15",
|
|
42
|
+
"langchain": "^0.3.0",
|
|
43
|
+
"lodash": "^4.17.21",
|
|
44
|
+
"pg": "^8.16.0",
|
|
45
|
+
"zod": "^3.22.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@jest/globals": "^29.6.0",
|
|
49
|
+
"@langchain/anthropic": "^0.3.34",
|
|
50
|
+
"@langchain/openai": "^0.3.17",
|
|
51
|
+
"@types/jest": "^29.5.14",
|
|
52
|
+
"@types/lodash": "^4.14.195",
|
|
53
|
+
"@types/node": "^18.17.0",
|
|
54
|
+
"@types/pg": "^8.15.2",
|
|
55
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
56
|
+
"@typescript-eslint/parser": "^5.62.0",
|
|
57
|
+
"dotenv-cli": "^11.0.0",
|
|
58
|
+
"eslint": "^8.45.0",
|
|
59
|
+
"jest": "^29.6.0",
|
|
60
|
+
"ts-jest": "^29.1.0",
|
|
61
|
+
"ts-node": "^10.9.2",
|
|
62
|
+
"typescript": "^5.1.6"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"@langchain/anthropic": "^0.3.34",
|
|
66
|
+
"@langchain/openai": "^0.3.17"
|
|
67
|
+
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"@langchain/anthropic": {
|
|
70
|
+
"optional": true
|
|
71
|
+
},
|
|
72
|
+
"@langchain/openai": {
|
|
73
|
+
"optional": true
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|