@pool4t9/server 1.0.0 → 1.0.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 +2 -2
- package/dist/initAI.d.ts +2 -2
- package/dist/initAI.js +40 -12
- package/dist/llm.d.ts +3 -3
- package/dist/llm.js +7 -14
- package/dist/memory/dbMemoryAdapter.d.ts +2 -0
- package/dist/memory/dbMemoryAdapter.js +31 -0
- package/dist/memory/types.d.ts +5 -0
- package/dist/memory/types.js +2 -0
- package/dist/toolBuilder.d.ts +3 -0
- package/dist/toolBuilder.js +12 -0
- package/dist/type.d.ts +44 -2
- package/dist/type.js +27 -0
- package/package.json +7 -1
package/README.md
CHANGED
package/dist/initAI.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare function initAI(config: any): {
|
|
2
|
-
registerQuery(name: string, fn: Function): void;
|
|
3
|
-
query(message: string, ctx?:
|
|
2
|
+
registerQuery(name: string, fn: Function, schema: any): void;
|
|
3
|
+
query(message: string, ctx?: any): Promise<{
|
|
4
4
|
message: any;
|
|
5
5
|
}>;
|
|
6
6
|
};
|
package/dist/initAI.js
CHANGED
|
@@ -4,26 +4,54 @@ exports.initAI = initAI;
|
|
|
4
4
|
const llm_1 = require("./llm");
|
|
5
5
|
const prompt_1 = require("./prompt");
|
|
6
6
|
const executor_1 = require("./executor");
|
|
7
|
+
const messages_1 = require("@langchain/core/messages");
|
|
8
|
+
const toolBuilder_1 = require("./toolBuilder");
|
|
7
9
|
function initAI(config) {
|
|
8
|
-
const
|
|
10
|
+
const baseLLM = (0, llm_1.createLLM)(config.llm);
|
|
9
11
|
const queries = {};
|
|
12
|
+
const tools = [];
|
|
13
|
+
let llm = baseLLM.bindTools(tools);
|
|
14
|
+
const memory = config.memoryAdapter;
|
|
10
15
|
return {
|
|
11
|
-
registerQuery(name, fn) {
|
|
16
|
+
registerQuery(name, fn, schema) {
|
|
17
|
+
if (queries[name]) {
|
|
18
|
+
throw new Error(`Query "${name}" already registered`);
|
|
19
|
+
}
|
|
12
20
|
queries[name] = fn;
|
|
21
|
+
tools.push((0, toolBuilder_1.createToolBuilder)(name, fn, schema)); // 👈 build ONCE
|
|
22
|
+
llm = baseLLM.bindTools(tools);
|
|
13
23
|
},
|
|
14
24
|
async query(message, ctx = {}) {
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
const conversationId = ctx.conversationId ?? "default";
|
|
26
|
+
const history = await memory.load(conversationId);
|
|
27
|
+
const systemPrompt = (0, prompt_1.buildPrompt)(config.schema);
|
|
28
|
+
const ai = await llm.invoke([
|
|
29
|
+
new messages_1.SystemMessage(systemPrompt),
|
|
30
|
+
...history,
|
|
31
|
+
new messages_1.HumanMessage(message),
|
|
32
|
+
]);
|
|
33
|
+
// save user message
|
|
34
|
+
await memory.save(conversationId, [
|
|
35
|
+
...history,
|
|
36
|
+
new messages_1.HumanMessage(message)
|
|
37
|
+
]);
|
|
38
|
+
// 1️⃣ Tool call path
|
|
39
|
+
if (ai.tool_calls?.length) {
|
|
40
|
+
const toolCall = ai.tool_calls[0];
|
|
41
|
+
const result = await (0, executor_1.executeQuery)(toolCall.name, toolCall.args, queries, config.db, ctx);
|
|
42
|
+
await memory.save(conversationId, [
|
|
43
|
+
...history,
|
|
44
|
+
new messages_1.AIMessage(ai.content)
|
|
45
|
+
]);
|
|
46
|
+
return {
|
|
47
|
+
message: result
|
|
48
|
+
};
|
|
23
49
|
}
|
|
24
|
-
|
|
50
|
+
// 2️⃣ Normal text response
|
|
25
51
|
return {
|
|
26
|
-
message: ai.
|
|
52
|
+
message: typeof ai.content === "string"
|
|
53
|
+
? ai.content
|
|
54
|
+
: ai.content[0]?.text
|
|
27
55
|
};
|
|
28
56
|
}
|
|
29
57
|
};
|
package/dist/llm.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
2
|
+
import { AIConfig } from "./type";
|
|
3
|
+
export declare function createLLM(config: AIConfig): ChatOpenAI<import("@langchain/openai").ChatOpenAICallOptions>;
|
package/dist/llm.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createLLM = createLLM;
|
|
4
|
+
const openai_1 = require("@langchain/openai");
|
|
4
5
|
function createLLM(config) {
|
|
5
|
-
if (config.provider === "openai") {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
if (config.provider === "gemini") {
|
|
14
|
-
return {
|
|
15
|
-
async chat(payload) {
|
|
16
|
-
return payload.__mockResponse;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
6
|
+
if (config.llm.provider === "openai") {
|
|
7
|
+
const openai = new openai_1.ChatOpenAI({
|
|
8
|
+
apiKey: config.llm.apiKey,
|
|
9
|
+
model: config.llm.model || "gpt-4o-mini",
|
|
10
|
+
});
|
|
11
|
+
return openai;
|
|
19
12
|
}
|
|
20
13
|
throw new Error("Unsupported LLM provider");
|
|
21
14
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDbMemoryAdapter = createDbMemoryAdapter;
|
|
4
|
+
const messages_1 = require("@langchain/core/messages");
|
|
5
|
+
function createDbMemoryAdapter(db) {
|
|
6
|
+
return {
|
|
7
|
+
async load(conversationId) {
|
|
8
|
+
const rows = await db.chat_messages.findMany({
|
|
9
|
+
where: { conversation_id: conversationId },
|
|
10
|
+
orderBy: { created_at: "asc" }
|
|
11
|
+
});
|
|
12
|
+
return rows.map((row) => {
|
|
13
|
+
if (row.role === "human")
|
|
14
|
+
return new messages_1.HumanMessage(row.content);
|
|
15
|
+
if (row.role === "ai")
|
|
16
|
+
return new messages_1.AIMessage(row.content);
|
|
17
|
+
return new messages_1.SystemMessage(row.content);
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
async save(conversationId, messages) {
|
|
21
|
+
const last = messages[messages.length - 1];
|
|
22
|
+
await db.chat_messages.create({
|
|
23
|
+
data: {
|
|
24
|
+
conversation_id: conversationId,
|
|
25
|
+
role: last._getType(),
|
|
26
|
+
content: last.content
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
export declare function createToolBuilder(name: string, handler: Function, schema: ZodSchema): DynamicStructuredTool<ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, unknown, unknown, any, string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createToolBuilder = createToolBuilder;
|
|
4
|
+
const tools_1 = require("@langchain/core/tools");
|
|
5
|
+
function createToolBuilder(name, handler, schema) {
|
|
6
|
+
return new tools_1.DynamicStructuredTool({
|
|
7
|
+
name,
|
|
8
|
+
description: `Execute ${name}`,
|
|
9
|
+
schema: schema, // tighten later
|
|
10
|
+
func: async (args) => handler(args),
|
|
11
|
+
});
|
|
12
|
+
}
|
package/dist/type.d.ts
CHANGED
|
@@ -1,4 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { MemoryAdapter } from "./memory/types";
|
|
3
|
+
/**
|
|
4
|
+
* LLM config
|
|
5
|
+
*/
|
|
6
|
+
export declare const llmConfigSchema: z.ZodObject<{
|
|
7
|
+
provider: z.ZodEnum<{
|
|
8
|
+
openai: "openai";
|
|
9
|
+
gemini: "gemini";
|
|
10
|
+
anthropic: "anthropic";
|
|
11
|
+
}>;
|
|
12
|
+
apiKey: z.ZodString;
|
|
13
|
+
model: z.ZodOptional<z.ZodString>;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
/**
|
|
16
|
+
* Memory adapter (user provided)
|
|
17
|
+
*/
|
|
18
|
+
export declare const memoryAdapterSchema: z.ZodCustom<MemoryAdapter, MemoryAdapter>;
|
|
19
|
+
/**
|
|
20
|
+
* Main AI config
|
|
21
|
+
*/
|
|
22
|
+
export declare const aiConfigSchema: z.ZodObject<{
|
|
23
|
+
llm: z.ZodObject<{
|
|
24
|
+
provider: z.ZodEnum<{
|
|
25
|
+
openai: "openai";
|
|
26
|
+
gemini: "gemini";
|
|
27
|
+
anthropic: "anthropic";
|
|
28
|
+
}>;
|
|
29
|
+
apiKey: z.ZodString;
|
|
30
|
+
model: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
db: z.ZodAny;
|
|
33
|
+
schema: z.ZodAny;
|
|
34
|
+
memoryAdapter: z.ZodCustom<MemoryAdapter, MemoryAdapter>;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
export type LLMProvider = "openai" | "gemini" | "anthropic";
|
|
2
37
|
export type AIConfig = {
|
|
3
38
|
llm: {
|
|
4
39
|
provider: LLMProvider;
|
|
@@ -6,8 +41,15 @@ export type AIConfig = {
|
|
|
6
41
|
model?: string;
|
|
7
42
|
};
|
|
8
43
|
db: {
|
|
9
|
-
type: "postgres" | "mysql";
|
|
44
|
+
type: "postgres" | "mysql" | "mongodb";
|
|
10
45
|
url: string;
|
|
46
|
+
database?: string;
|
|
47
|
+
username?: string;
|
|
48
|
+
password?: string;
|
|
49
|
+
port?: number;
|
|
50
|
+
host?: string;
|
|
51
|
+
ssl?: boolean;
|
|
52
|
+
schema?: string;
|
|
11
53
|
};
|
|
12
54
|
schema: Record<string, {
|
|
13
55
|
description?: string;
|
package/dist/type.js
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.aiConfigSchema = exports.memoryAdapterSchema = exports.llmConfigSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
/**
|
|
6
|
+
* LLM config
|
|
7
|
+
*/
|
|
8
|
+
exports.llmConfigSchema = zod_1.z.object({
|
|
9
|
+
provider: zod_1.z.enum(["openai", "gemini", "anthropic"]),
|
|
10
|
+
apiKey: zod_1.z.string().min(1, "API key is required"),
|
|
11
|
+
model: zod_1.z.string().optional(),
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* Memory adapter (user provided)
|
|
15
|
+
*/
|
|
16
|
+
exports.memoryAdapterSchema = zod_1.z.custom((val) => typeof val === "object" &&
|
|
17
|
+
typeof val.load === "function" &&
|
|
18
|
+
typeof val.save === "function", {
|
|
19
|
+
message: "memoryAdapter must implement load(conversationId) and save(conversationId, messages)",
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Main AI config
|
|
23
|
+
*/
|
|
24
|
+
exports.aiConfigSchema = zod_1.z.object({
|
|
25
|
+
llm: exports.llmConfigSchema,
|
|
26
|
+
db: zod_1.z.any(), // intentionally open
|
|
27
|
+
schema: zod_1.z.any(), // db schema / tables user wants AI to know
|
|
28
|
+
memoryAdapter: exports.memoryAdapterSchema,
|
|
29
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pool4t9/server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "AI chatbot server package for building intelligent chat interfaces",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,5 +34,11 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"typescript": "^5.9.3"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@langchain/core": "^1.1.17",
|
|
40
|
+
"@langchain/openai": "^1.2.3",
|
|
41
|
+
"langchain": "^1.2.13",
|
|
42
|
+
"zod": "^4.3.6"
|
|
37
43
|
}
|
|
38
44
|
}
|