@axiom-lattice/core 1.0.13 → 1.0.21
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/LICENSE +201 -0
- package/package.json +16 -13
- package/.eslintrc.json +0 -22
- package/.turbo/turbo-build.log +0 -21
- package/jest.config.js +0 -21
- package/src/__tests__/AgentManager.test.ts +0 -202
- package/src/__tests__/setup.ts +0 -5
- package/src/agent_lattice/AgentLatticeManager.ts +0 -216
- package/src/agent_lattice/builders/AgentBuilder.ts +0 -79
- package/src/agent_lattice/builders/AgentGraphBuilder.ts +0 -22
- package/src/agent_lattice/builders/AgentGraphBuilderFactory.ts +0 -70
- package/src/agent_lattice/builders/AgentParamsBuilder.ts +0 -86
- package/src/agent_lattice/builders/DeepAgentGraphBuilder.ts +0 -46
- package/src/agent_lattice/builders/PlanExecuteAgentGraphBuilder.ts +0 -46
- package/src/agent_lattice/builders/ReActAgentGraphBuilder.ts +0 -42
- package/src/agent_lattice/builders/index.ts +0 -14
- package/src/agent_lattice/index.ts +0 -27
- package/src/agent_lattice/types.ts +0 -42
- package/src/base/BaseLatticeManager.ts +0 -145
- package/src/base/index.ts +0 -1
- package/src/createPlanExecuteAgent.ts +0 -475
- package/src/deep_agent/graph.ts +0 -106
- package/src/deep_agent/index.ts +0 -25
- package/src/deep_agent/prompts.ts +0 -284
- package/src/deep_agent/state.ts +0 -63
- package/src/deep_agent/subAgent.ts +0 -185
- package/src/deep_agent/tools.ts +0 -273
- package/src/deep_agent/types.ts +0 -71
- package/src/index.ts +0 -9
- package/src/logger/Logger.ts +0 -186
- package/src/memory_lattice/DefaultMemorySaver.ts +0 -4
- package/src/memory_lattice/MemoryLatticeManager.ts +0 -105
- package/src/memory_lattice/index.ts +0 -9
- package/src/model_lattice/ModelLattice.ts +0 -208
- package/src/model_lattice/ModelLatticeManager.ts +0 -125
- package/src/model_lattice/index.ts +0 -1
- package/src/tool_lattice/ToolLatticeManager.ts +0 -221
- package/src/tool_lattice/get_current_date_time/index.ts +0 -14
- package/src/tool_lattice/index.ts +0 -2
- package/src/tool_lattice/internet_search/index.ts +0 -66
- package/src/types.ts +0 -28
- package/src/util/PGMemory.ts +0 -16
- package/src/util/genUICard.ts +0 -3
- package/src/util/genUIMarkdown.ts +0 -3
- package/src/util/getLastHumanMessageData.ts +0 -41
- package/src/util/returnAIResponse.ts +0 -30
- package/src/util/returnErrorResponse.ts +0 -26
- package/src/util/returnFeedbackResponse.ts +0 -25
- package/src/util/returnToolResponse.ts +0 -32
- package/src/util/withAgentName.ts +0 -220
- package/src/util/zod-to-prompt.ts +0 -50
- package/tsconfig.json +0 -26
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AIMessage,
|
|
3
|
-
BaseMessage,
|
|
4
|
-
BaseMessageLike,
|
|
5
|
-
HumanMessage,
|
|
6
|
-
} from "@langchain/core/messages";
|
|
7
|
-
import { ChatDeepSeek } from "@langchain/deepseek";
|
|
8
|
-
import { AzureChatOpenAI, ChatOpenAI } from "@langchain/openai";
|
|
9
|
-
import { z } from "zod";
|
|
10
|
-
import { ZodType as ZodTypeV3 } from "zod/v3";
|
|
11
|
-
import { $ZodType as ZodTypeV4 } from "zod/v4/core";
|
|
12
|
-
import {
|
|
13
|
-
BaseChatModel,
|
|
14
|
-
BaseChatModelCallOptions,
|
|
15
|
-
} from "@langchain/core/language_models/chat_models";
|
|
16
|
-
import {
|
|
17
|
-
BaseLanguageModel,
|
|
18
|
-
BaseLanguageModelInput,
|
|
19
|
-
} from "@langchain/core/language_models/base";
|
|
20
|
-
import {
|
|
21
|
-
CallbackManagerForLLMRun,
|
|
22
|
-
Callbacks,
|
|
23
|
-
} from "@langchain/core/callbacks/manager";
|
|
24
|
-
import { ChatResult } from "@langchain/core/outputs";
|
|
25
|
-
BaseLanguageModel;
|
|
26
|
-
import { LLMConfig } from "@axiom-lattice/protocols";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* ModelLattice类,继承自BaseChatModel
|
|
30
|
-
* 简化版本,只保留通过LLMConfig创建LLM实例的功能
|
|
31
|
-
*/
|
|
32
|
-
export class ModelLattice extends BaseChatModel {
|
|
33
|
-
private config: LLMConfig;
|
|
34
|
-
private llm: BaseChatModel;
|
|
35
|
-
|
|
36
|
-
lc_namespace: string[] = ["langchain", "model_lattice"];
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 构造函数
|
|
40
|
-
* @param config LLM配置
|
|
41
|
-
*/
|
|
42
|
-
constructor(config: LLMConfig) {
|
|
43
|
-
super({});
|
|
44
|
-
this.config = config;
|
|
45
|
-
this.llm = this.initChatModel(config);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 返回模型类型
|
|
50
|
-
*/
|
|
51
|
-
_llmType(): string {
|
|
52
|
-
return "model_lattice";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 返回模型类型
|
|
57
|
-
*/
|
|
58
|
-
_modelType(): string {
|
|
59
|
-
return this.llm._modelType();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 实现BaseChatModel的_generate方法
|
|
64
|
-
* @param messages 消息数组
|
|
65
|
-
* @param options 调用选项
|
|
66
|
-
* @param runManager 回调管理器
|
|
67
|
-
* @returns 聊天结果
|
|
68
|
-
*/
|
|
69
|
-
async _generate(
|
|
70
|
-
messages: BaseMessage[],
|
|
71
|
-
options: this["ParsedCallOptions"],
|
|
72
|
-
runManager?: CallbackManagerForLLMRun
|
|
73
|
-
): Promise<ChatResult> {
|
|
74
|
-
return this.llm._generate(messages, options as any, runManager);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 将工具绑定到模型
|
|
79
|
-
* @param tools 工具列表
|
|
80
|
-
* @param tool_choice 工具选择选项
|
|
81
|
-
* @returns 绑定工具后的模型
|
|
82
|
-
*/
|
|
83
|
-
bindTools(
|
|
84
|
-
tools: any[],
|
|
85
|
-
{
|
|
86
|
-
tool_choice = "auto",
|
|
87
|
-
...kwargs
|
|
88
|
-
}: {
|
|
89
|
-
tool_choice?: "auto" | "none" | "required" | any;
|
|
90
|
-
[key: string]: any;
|
|
91
|
-
} = {}
|
|
92
|
-
): any {
|
|
93
|
-
// 如果底层LLM实现了bindTools方法,则使用它的实现
|
|
94
|
-
if (typeof this.llm.bindTools === "function") {
|
|
95
|
-
return this.llm.bindTools(tools, {
|
|
96
|
-
tool_choice,
|
|
97
|
-
...kwargs,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 如果底层LLM没有实现bindTools方法,抛出错误
|
|
102
|
-
throw new Error("llm not support bindTools");
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 使用结构化输出调用LLM
|
|
107
|
-
* @param input 输入
|
|
108
|
-
* @param schema 结构化输出的schema
|
|
109
|
-
* @param options 调用选项
|
|
110
|
-
* @returns 结构化输出
|
|
111
|
-
*/
|
|
112
|
-
async invokeWithStructuredOutput<
|
|
113
|
-
RunOutput extends Record<string, any> = Record<string, any>
|
|
114
|
-
>(
|
|
115
|
-
input: BaseLanguageModelInput,
|
|
116
|
-
schema: ZodTypeV3<RunOutput> | ZodTypeV4<RunOutput> | Record<string, any>,
|
|
117
|
-
options?: BaseChatModelCallOptions & {
|
|
118
|
-
includeRaw?: boolean;
|
|
119
|
-
name?: string;
|
|
120
|
-
method?: "functionCalling" | "jsonMode";
|
|
121
|
-
}
|
|
122
|
-
): Promise<RunOutput | { raw: BaseMessage; parsed: RunOutput }> {
|
|
123
|
-
// 使用LLM的withStructuredOutput方法
|
|
124
|
-
if (this.llm.withStructuredOutput) {
|
|
125
|
-
// 使用类型断言解决类型兼容性问题
|
|
126
|
-
const structuredLLM = this.llm.withStructuredOutput(schema, {
|
|
127
|
-
includeRaw: options?.includeRaw ?? false,
|
|
128
|
-
name: options?.name,
|
|
129
|
-
method: options?.method,
|
|
130
|
-
} as any);
|
|
131
|
-
|
|
132
|
-
return structuredLLM.invoke(input, options);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// 如果LLM没有withStructuredOutput方法,抛出错误
|
|
136
|
-
throw new Error("当前LLM不支持结构化输出");
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 创建LLM实例
|
|
141
|
-
* @param config LLM配置
|
|
142
|
-
* @returns LLM实例
|
|
143
|
-
*/
|
|
144
|
-
private initChatModel(config: LLMConfig): BaseChatModel {
|
|
145
|
-
if (config.provider === "azure") {
|
|
146
|
-
return new AzureChatOpenAI({
|
|
147
|
-
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
|
|
148
|
-
azureOpenAIApiInstanceName: "convertlab-westus",
|
|
149
|
-
azureOpenAIApiDeploymentName: config.model,
|
|
150
|
-
azureOpenAIApiVersion: "2024-02-01",
|
|
151
|
-
temperature: config.temperature || 0,
|
|
152
|
-
maxTokens: config.maxTokens,
|
|
153
|
-
timeout: config.timeout,
|
|
154
|
-
maxRetries: config.maxRetries || 2,
|
|
155
|
-
streaming: config.streaming,
|
|
156
|
-
});
|
|
157
|
-
} else if (config.provider === "deepseek") {
|
|
158
|
-
return new ChatDeepSeek({
|
|
159
|
-
model: config.model,
|
|
160
|
-
temperature: config.temperature || 0,
|
|
161
|
-
maxTokens: config.maxTokens,
|
|
162
|
-
timeout: config.timeout,
|
|
163
|
-
maxRetries: config.maxRetries || 2,
|
|
164
|
-
apiKey: process.env[config.apiKeyEnvName || "DEEPSEEK_API_KEY"],
|
|
165
|
-
streaming: config.streaming,
|
|
166
|
-
});
|
|
167
|
-
} else if (config.provider === "siliconcloud") {
|
|
168
|
-
return new ChatOpenAI({
|
|
169
|
-
model: config.model,
|
|
170
|
-
temperature: config.temperature || 0,
|
|
171
|
-
maxTokens: config.maxTokens,
|
|
172
|
-
timeout: config.timeout,
|
|
173
|
-
maxRetries: config.maxRetries || 2,
|
|
174
|
-
apiKey: process.env[config.apiKeyEnvName || "SILICONCLOUD_API_KEY"],
|
|
175
|
-
configuration: {
|
|
176
|
-
baseURL: "https://api.siliconflow.cn/v1",
|
|
177
|
-
},
|
|
178
|
-
streaming: config.streaming,
|
|
179
|
-
});
|
|
180
|
-
} else if (config.provider === "volcengine") {
|
|
181
|
-
return new ChatOpenAI({
|
|
182
|
-
model: config.model,
|
|
183
|
-
temperature: config.temperature || 0,
|
|
184
|
-
maxTokens: config.maxTokens,
|
|
185
|
-
timeout: config.timeout,
|
|
186
|
-
maxRetries: config.maxRetries || 2,
|
|
187
|
-
apiKey: process.env[config.apiKeyEnvName || "VOLCENGINE_API_KEY"],
|
|
188
|
-
configuration: {
|
|
189
|
-
baseURL: "https://ark.cn-beijing.volces.com/api/v3",
|
|
190
|
-
},
|
|
191
|
-
streaming: config.streaming,
|
|
192
|
-
});
|
|
193
|
-
} else {
|
|
194
|
-
return new ChatOpenAI({
|
|
195
|
-
model: config.model,
|
|
196
|
-
temperature: config.temperature || 0,
|
|
197
|
-
maxTokens: config.maxTokens,
|
|
198
|
-
timeout: config.timeout,
|
|
199
|
-
maxRetries: config.maxRetries || 2,
|
|
200
|
-
streaming: config.streaming,
|
|
201
|
-
apiKey: process.env[config.apiKeyEnvName || "OPENAI_API_KEY"],
|
|
202
|
-
configuration: {
|
|
203
|
-
baseURL: config.baseURL,
|
|
204
|
-
},
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { BaseLatticeManager } from "../base/BaseLatticeManager";
|
|
2
|
-
import { ModelLattice as LLMModelLattice } from "./ModelLattice";
|
|
3
|
-
import { LLMConfig } from "@axiom-lattice/protocols";
|
|
4
|
-
|
|
5
|
-
// 模型配置接口,直接使用 LLMConfig
|
|
6
|
-
export type ModelConfig = LLMConfig;
|
|
7
|
-
|
|
8
|
-
// 模型客户端类型,使用 LLMManager
|
|
9
|
-
type ModelClient = LLMModelLattice;
|
|
10
|
-
|
|
11
|
-
// 定义接口
|
|
12
|
-
export interface ModelLatticeInterface {
|
|
13
|
-
key: string;
|
|
14
|
-
client: ModelClient;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* ModelLatticeManager - 单例模型Lattice管理器
|
|
19
|
-
* 负责注册、管理各种语言模型Lattice
|
|
20
|
-
*/
|
|
21
|
-
export class ModelLatticeManager extends BaseLatticeManager<ModelLatticeInterface> {
|
|
22
|
-
private static _instance: ModelLatticeManager;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 获取ModelLatticeManager单例实例
|
|
26
|
-
*/
|
|
27
|
-
public static getInstance(): ModelLatticeManager {
|
|
28
|
-
if (!ModelLatticeManager._instance) {
|
|
29
|
-
ModelLatticeManager._instance = new ModelLatticeManager();
|
|
30
|
-
}
|
|
31
|
-
return ModelLatticeManager._instance;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 获取Lattice类型前缀
|
|
36
|
-
*/
|
|
37
|
-
protected getLatticeType(): string {
|
|
38
|
-
return "models";
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 注册模型Lattice
|
|
43
|
-
* @param key Lattice键名
|
|
44
|
-
* @param config 模型配置
|
|
45
|
-
*/
|
|
46
|
-
public registerLattice(key: string, config: ModelConfig): void {
|
|
47
|
-
// 使用 LLMManager 创建客户端
|
|
48
|
-
const client = new LLMModelLattice(config);
|
|
49
|
-
|
|
50
|
-
// 创建模型Lattice对象
|
|
51
|
-
const modelLattice: ModelLatticeInterface = {
|
|
52
|
-
key,
|
|
53
|
-
client,
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
// 调用基类的register方法
|
|
57
|
-
this.register(key, modelLattice);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 获取ModelLattice
|
|
62
|
-
* @param key Lattice键名
|
|
63
|
-
*/
|
|
64
|
-
public getModelLattice(key: string): ModelLatticeInterface {
|
|
65
|
-
const modelLattice = this.get(key);
|
|
66
|
-
if (!modelLattice) {
|
|
67
|
-
throw new Error(`ModelLattice ${key} not found`);
|
|
68
|
-
}
|
|
69
|
-
return modelLattice;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 获取所有Lattice
|
|
74
|
-
*/
|
|
75
|
-
public getAllLattices(): ModelLatticeInterface[] {
|
|
76
|
-
return this.getAll();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 检查Lattice是否存在
|
|
81
|
-
* @param key Lattice键名
|
|
82
|
-
*/
|
|
83
|
-
public hasLattice(key: string): boolean {
|
|
84
|
-
return this.has(key);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 移除Lattice
|
|
89
|
-
* @param key Lattice键名
|
|
90
|
-
*/
|
|
91
|
-
public removeLattice(key: string): boolean {
|
|
92
|
-
return this.remove(key);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 清空所有Lattice
|
|
97
|
-
*/
|
|
98
|
-
public clearLattices(): void {
|
|
99
|
-
this.clear();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 获取Lattice数量
|
|
104
|
-
*/
|
|
105
|
-
public getLatticeCount(): number {
|
|
106
|
-
return this.count();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 获取Lattice键名列表
|
|
111
|
-
*/
|
|
112
|
-
public getLatticeKeys(): string[] {
|
|
113
|
-
return this.keys();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// 导出单例实例
|
|
118
|
-
export const modelLatticeManager = ModelLatticeManager.getInstance();
|
|
119
|
-
|
|
120
|
-
// 导出便捷方法
|
|
121
|
-
export const registerModelLattice = (key: string, config: ModelConfig) =>
|
|
122
|
-
modelLatticeManager.registerLattice(key, config);
|
|
123
|
-
|
|
124
|
-
export const getModelLattice = (key: string) =>
|
|
125
|
-
modelLatticeManager.getModelLattice(key);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./ModelLatticeManager";
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DynamicStructuredTool,
|
|
3
|
-
StructuredTool,
|
|
4
|
-
tool,
|
|
5
|
-
} from "@langchain/core/tools";
|
|
6
|
-
import { BaseLatticeManager } from "../base/BaseLatticeManager";
|
|
7
|
-
import { z } from "zod";
|
|
8
|
-
import { interrupt } from "@langchain/langgraph";
|
|
9
|
-
import { UserFeedbackResponse } from "../types";
|
|
10
|
-
import { ToolConfig, ToolExecutor } from "@axiom-lattice/protocols";
|
|
11
|
-
|
|
12
|
-
// 工具定义接口 - 使用协议中的ToolConfig
|
|
13
|
-
export type ToolDefinition = ToolConfig;
|
|
14
|
-
|
|
15
|
-
// Lattice 接口定义
|
|
16
|
-
export interface ToolLattice {
|
|
17
|
-
key: string;
|
|
18
|
-
config: ToolConfig;
|
|
19
|
-
client: StructuredTool;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* ToolLatticeManager - 单例工具Lattice管理器
|
|
24
|
-
* 负责注册、管理各种工具Lattice
|
|
25
|
-
*/
|
|
26
|
-
export class ToolLatticeManager extends BaseLatticeManager<ToolLattice> {
|
|
27
|
-
private static _instance: ToolLatticeManager;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 获取ToolLatticeManager单例实例
|
|
31
|
-
*/
|
|
32
|
-
public static getInstance(): ToolLatticeManager {
|
|
33
|
-
if (!ToolLatticeManager._instance) {
|
|
34
|
-
ToolLatticeManager._instance = new ToolLatticeManager();
|
|
35
|
-
}
|
|
36
|
-
return ToolLatticeManager._instance;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 获取Lattice类型前缀
|
|
41
|
-
*/
|
|
42
|
-
protected getLatticeType(): string {
|
|
43
|
-
return "tools";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 注册工具Lattice
|
|
48
|
-
* @param key Lattice键名
|
|
49
|
-
* @param config 工具配置(定义)
|
|
50
|
-
* @param executor 工具执行函数
|
|
51
|
-
*/
|
|
52
|
-
public registerLattice<TInput = any, TOutput = any>(
|
|
53
|
-
key: string,
|
|
54
|
-
config: ToolConfig,
|
|
55
|
-
executor: ToolExecutor<TInput, TOutput>
|
|
56
|
-
): void {
|
|
57
|
-
// 创建工具Lattice对象
|
|
58
|
-
let toolExecutor = async (input: TInput, exe_config: any) => {
|
|
59
|
-
const result = await executor(input, exe_config);
|
|
60
|
-
return result;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
// if (config.needUserApprove) {
|
|
64
|
-
// toolExecutor = async (input: TInput, exe_config: any) => {
|
|
65
|
-
// const contents = [
|
|
66
|
-
// "```confirm",
|
|
67
|
-
// JSON.stringify({
|
|
68
|
-
// message: "try to " + config.name + ",please confirm the action",
|
|
69
|
-
// }),
|
|
70
|
-
// "```",
|
|
71
|
-
// ].join("\n");
|
|
72
|
-
|
|
73
|
-
// const feedback: UserFeedbackResponse = await interrupt(contents);
|
|
74
|
-
// if (feedback.data.action === "yes") {
|
|
75
|
-
// const result = await executor(input, exe_config);
|
|
76
|
-
// return result;
|
|
77
|
-
// } else {
|
|
78
|
-
// return "user denied the action" as unknown as TOutput;
|
|
79
|
-
// }
|
|
80
|
-
// };
|
|
81
|
-
// }
|
|
82
|
-
|
|
83
|
-
const toolLattice: ToolLattice = {
|
|
84
|
-
key,
|
|
85
|
-
config,
|
|
86
|
-
client: tool(toolExecutor as ToolExecutor, config),
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
// 调用基类的register方法
|
|
90
|
-
this.register(key, toolLattice);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 获取ToolLattice
|
|
95
|
-
* @param key Lattice键名
|
|
96
|
-
*/
|
|
97
|
-
public getToolLattice(key: string): ToolLattice | undefined {
|
|
98
|
-
return this.get(key);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 获取所有Lattice
|
|
103
|
-
*/
|
|
104
|
-
public getAllLattices(): ToolLattice[] {
|
|
105
|
-
return this.getAll();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 检查Lattice是否存在
|
|
110
|
-
* @param key Lattice键名
|
|
111
|
-
*/
|
|
112
|
-
public hasLattice(key: string): boolean {
|
|
113
|
-
return this.has(key);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 移除Lattice
|
|
118
|
-
* @param key Lattice键名
|
|
119
|
-
*/
|
|
120
|
-
public removeLattice(key: string): boolean {
|
|
121
|
-
return this.remove(key);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 清空所有Lattice
|
|
126
|
-
*/
|
|
127
|
-
public clearLattices(): void {
|
|
128
|
-
this.clear();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 获取Lattice数量
|
|
133
|
-
*/
|
|
134
|
-
public getLatticeCount(): number {
|
|
135
|
-
return this.count();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* 获取Lattice键名列表
|
|
140
|
-
*/
|
|
141
|
-
public getLatticeKeys(): string[] {
|
|
142
|
-
return this.keys();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 获取工具定义
|
|
147
|
-
* @param key Lattice键名
|
|
148
|
-
*/
|
|
149
|
-
public getToolDefinition(key: string): ToolDefinition {
|
|
150
|
-
const toolLattice = this.getToolLattice(key);
|
|
151
|
-
if (!toolLattice) {
|
|
152
|
-
throw new Error(`ToolLattice ${key} not found`);
|
|
153
|
-
}
|
|
154
|
-
return toolLattice.config;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* 获取工具客户端
|
|
159
|
-
* @param key Lattice键名
|
|
160
|
-
*/
|
|
161
|
-
public getToolClient(key: string): StructuredTool {
|
|
162
|
-
const toolLattice = this.getToolLattice(key);
|
|
163
|
-
if (!toolLattice) {
|
|
164
|
-
throw new Error(`ToolLattice ${key} not found`);
|
|
165
|
-
}
|
|
166
|
-
return toolLattice.client;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* 获取所有工具定义
|
|
171
|
-
*/
|
|
172
|
-
public getAllToolDefinitions(): ToolDefinition[] {
|
|
173
|
-
return this.getAllLattices().map((lattice) => lattice.config);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 验证工具输入参数
|
|
178
|
-
* @param key Lattice键名
|
|
179
|
-
* @param input 输入参数
|
|
180
|
-
*/
|
|
181
|
-
public validateToolInput(key: string, input: any): boolean {
|
|
182
|
-
const toolLattice = this.getToolLattice(key);
|
|
183
|
-
if (!toolLattice) {
|
|
184
|
-
return false;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
if (toolLattice.config.schema) {
|
|
189
|
-
toolLattice.config.schema.parse(input);
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
} catch {
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 导出单例实例
|
|
199
|
-
export const toolLatticeManager = ToolLatticeManager.getInstance();
|
|
200
|
-
|
|
201
|
-
// 导出便捷方法
|
|
202
|
-
export const registerToolLattice = (
|
|
203
|
-
key: string,
|
|
204
|
-
config: ToolConfig,
|
|
205
|
-
executor: ToolExecutor
|
|
206
|
-
) => toolLatticeManager.registerLattice(key, config, executor);
|
|
207
|
-
|
|
208
|
-
export const getToolLattice = (key: string) =>
|
|
209
|
-
toolLatticeManager.getToolLattice(key);
|
|
210
|
-
|
|
211
|
-
export const getToolDefinition = (key: string) =>
|
|
212
|
-
toolLatticeManager.getToolDefinition(key);
|
|
213
|
-
|
|
214
|
-
export const getToolClient = (key: string) =>
|
|
215
|
-
toolLatticeManager.getToolClient(key);
|
|
216
|
-
|
|
217
|
-
export const getAllToolDefinitions = () =>
|
|
218
|
-
toolLatticeManager.getAllToolDefinitions();
|
|
219
|
-
|
|
220
|
-
export const validateToolInput = (key: string, input: any) =>
|
|
221
|
-
toolLatticeManager.validateToolInput(key, input);
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import z from "zod";
|
|
2
|
-
import { registerToolLattice } from "../ToolLatticeManager";
|
|
3
|
-
|
|
4
|
-
registerToolLattice(
|
|
5
|
-
"get_current_date_time",
|
|
6
|
-
{
|
|
7
|
-
name: "获取当前日期时间",
|
|
8
|
-
description: "获取当前日期时间",
|
|
9
|
-
schema: z.object({}),
|
|
10
|
-
},
|
|
11
|
-
async () => {
|
|
12
|
-
return "当前日期时间:" + new Date().toLocaleString();
|
|
13
|
-
}
|
|
14
|
-
);
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import z from "zod";
|
|
2
|
-
import { registerToolLattice } from "../ToolLatticeManager";
|
|
3
|
-
import { TavilySearch } from "@langchain/tavily";
|
|
4
|
-
import "dotenv/config";
|
|
5
|
-
import { LangGraphRunnableConfig } from "@langchain/langgraph";
|
|
6
|
-
import { genUICard } from "@util/genUICard";
|
|
7
|
-
|
|
8
|
-
type Topic = "general" | "news" | "finance";
|
|
9
|
-
|
|
10
|
-
registerToolLattice(
|
|
11
|
-
"internet_search",
|
|
12
|
-
{
|
|
13
|
-
name: "internet_search",
|
|
14
|
-
description: "Run a web search",
|
|
15
|
-
needUserApprove: true,
|
|
16
|
-
schema: z.object({
|
|
17
|
-
query: z.string().describe("The search query"),
|
|
18
|
-
maxResults: z
|
|
19
|
-
.number()
|
|
20
|
-
.optional()
|
|
21
|
-
.default(5)
|
|
22
|
-
.describe("Maximum number of results to return"),
|
|
23
|
-
topic: z
|
|
24
|
-
.enum(["general", "news", "finance"])
|
|
25
|
-
.optional()
|
|
26
|
-
.default("general")
|
|
27
|
-
.describe("Search topic category"),
|
|
28
|
-
includeRawContent: z
|
|
29
|
-
.boolean()
|
|
30
|
-
.optional()
|
|
31
|
-
.default(false)
|
|
32
|
-
.describe("Whether to include raw content"),
|
|
33
|
-
}),
|
|
34
|
-
},
|
|
35
|
-
async (
|
|
36
|
-
{
|
|
37
|
-
query,
|
|
38
|
-
maxResults = 5,
|
|
39
|
-
topic = "general" as Topic,
|
|
40
|
-
includeRawContent = false,
|
|
41
|
-
}: {
|
|
42
|
-
query: string;
|
|
43
|
-
maxResults?: number;
|
|
44
|
-
topic?: Topic;
|
|
45
|
-
includeRawContent?: boolean;
|
|
46
|
-
},
|
|
47
|
-
config: LangGraphRunnableConfig
|
|
48
|
-
) => {
|
|
49
|
-
/**
|
|
50
|
-
* Run a web search
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
// Note: You'll need to install and import tavily-js or similar package
|
|
54
|
-
const tavilySearch = new TavilySearch({
|
|
55
|
-
maxResults,
|
|
56
|
-
tavilyApiKey: process.env.TAVILY_API_KEY,
|
|
57
|
-
includeRawContent,
|
|
58
|
-
topic,
|
|
59
|
-
});
|
|
60
|
-
const tavilyResponse = await tavilySearch.invoke({ query });
|
|
61
|
-
|
|
62
|
-
return genUICard("Internet Search:" + query, "generic_data_table", {
|
|
63
|
-
dataSource: tavilyResponse.results,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
);
|
package/src/types.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { BaseMessageLike } from "@langchain/core/messages";
|
|
2
|
-
import {
|
|
3
|
-
Annotation,
|
|
4
|
-
// AnnotationRoot,
|
|
5
|
-
messagesStateReducer,
|
|
6
|
-
} from "@langchain/langgraph";
|
|
7
|
-
|
|
8
|
-
export const BaseAnnotations = Annotation.Root({
|
|
9
|
-
"x-tenant-id": Annotation<number>(),
|
|
10
|
-
messages: Annotation<BaseMessageLike[]>({
|
|
11
|
-
reducer: messagesStateReducer,
|
|
12
|
-
default() {
|
|
13
|
-
return [];
|
|
14
|
-
},
|
|
15
|
-
}),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
type MessagesAnnotaionType = typeof BaseAnnotations.State;
|
|
19
|
-
export type StateBaseType = MessagesAnnotaionType & {
|
|
20
|
-
[key: string]: any;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type UserFeedbackResponse = {
|
|
24
|
-
action: string;
|
|
25
|
-
data: any;
|
|
26
|
-
message: string;
|
|
27
|
-
config?: { thread_id?: string; work_log_id?: string };
|
|
28
|
-
};
|
package/src/util/PGMemory.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { MemorySaver } from "@langchain/langgraph";
|
|
2
|
-
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
|
|
3
|
-
import dotenv from "dotenv";
|
|
4
|
-
|
|
5
|
-
const globalMemory = PostgresSaver.fromConnString(process.env.DATABASE_URL!);
|
|
6
|
-
|
|
7
|
-
const memory = new MemorySaver();
|
|
8
|
-
// 创建一个管理器来访问这个实例
|
|
9
|
-
export class MemoryManager {
|
|
10
|
-
private static instance: MemorySaver = memory;
|
|
11
|
-
|
|
12
|
-
static getInstance(): MemorySaver {
|
|
13
|
-
return MemoryManager.instance;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
//MemoryManager.getInstance().setup();
|
package/src/util/genUICard.ts
DELETED