@axiom-lattice/protocols 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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * AgentLatticeProtocol
3
+ *
4
+ * 智能体Lattice的协议,定义了智能体的行为和组合方式
5
+ */
6
+
7
+ import { CompiledStateGraph } from "@langchain/langgraph";
8
+ import { ZodSchema } from "zod";
9
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
10
+
11
+ /**
12
+ * 智能体类型枚举
13
+ */
14
+ export enum AgentType {
15
+ REACT = "react",
16
+ DEEP_AGENT = "deep_agent",
17
+ PLAN_EXECUTE = "plan_execute",
18
+ SEQUENTIAL = "sequential",
19
+ }
20
+
21
+ /**
22
+ * 智能体配置接口
23
+ */
24
+ export interface AgentConfig {
25
+ key: string; // 唯一键
26
+ name: string; // 名称
27
+ description: string; // 描述
28
+ type: AgentType; // 智能体类型
29
+ tools?: string[]; // 使用的工具列表
30
+ subAgents?: string[]; // 子智能体列表
31
+ prompt: string; // 提示词
32
+ schema?: ZodSchema<any>; // 输入验证模式
33
+ modelKey?: string; // 使用的模型键
34
+ }
35
+
36
+ /**
37
+ * 智能体客户端类型
38
+ */
39
+ export type AgentClient = CompiledStateGraph<any, any, any, any, any>;
40
+
41
+ /**
42
+ * Graph构建选项
43
+ */
44
+ export interface GraphBuildOptions {
45
+ overrideTools?: string[];
46
+ overrideModel?: string;
47
+ metadata?: Record<string, any>;
48
+ }
49
+
50
+ /**
51
+ * 智能体Lattice协议接口
52
+ */
53
+ export interface AgentLatticeProtocol
54
+ extends BaseLatticeProtocol<AgentConfig, AgentClient> {
55
+ // 智能体执行函数
56
+ invoke: (input: any, options?: any) => Promise<any>;
57
+
58
+ // 构建智能体图
59
+ buildGraph: (options?: GraphBuildOptions) => Promise<AgentClient>;
60
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * BaseLatticeProtocol
3
+ *
4
+ * 所有Lattice类型的基础协议,定义了通用接口和行为
5
+ */
6
+
7
+ /**
8
+ * 基础Lattice协议接口
9
+ * @template TConfig 配置类型
10
+ * @template TClient 客户端类型
11
+ */
12
+ export interface BaseLatticeProtocol<TConfig = any, TClient = any> {
13
+ // 唯一标识符
14
+ key: string;
15
+
16
+ // 配置信息
17
+ config: TConfig;
18
+
19
+ // 实际执行客户端
20
+ client: TClient;
21
+ }
22
+
23
+ /**
24
+ * Lattice消息接口
25
+ */
26
+ export interface LatticeMessage {
27
+ type: string; // 消息类型
28
+ source: string; // 源Lattice ID
29
+ target?: string; // 目标Lattice ID (可选)
30
+ payload: any; // 消息内容
31
+ timestamp: number; // 时间戳
32
+ metadata?: Record<string, any>; // 元数据
33
+ }
34
+
35
+ /**
36
+ * Lattice错误接口
37
+ */
38
+ export interface LatticeError {
39
+ code: string; // 错误代码
40
+ message: string; // 错误信息
41
+ source: string; // 错误源
42
+ stack?: string; // 堆栈跟踪
43
+ metadata?: Record<string, any>; // 元数据
44
+ }
45
+
46
+ /**
47
+ * Lattice事件总线接口
48
+ */
49
+ export interface LatticeEventBus {
50
+ subscribe: (
51
+ eventType: string,
52
+ handler: (message: LatticeMessage) => void
53
+ ) => void;
54
+ unsubscribe: (
55
+ eventType: string,
56
+ handler: (message: LatticeMessage) => void
57
+ ) => void;
58
+ publish: (eventType: string, message: LatticeMessage) => void;
59
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * MemoryLatticeProtocol
3
+ *
4
+ * 记忆Lattice的协议,用于管理智能体的上下文和记忆
5
+ */
6
+
7
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
8
+
9
+ /**
10
+ * 记忆类型枚举
11
+ */
12
+ export enum MemoryType {
13
+ SHORT_TERM = "short_term",
14
+ LONG_TERM = "long_term",
15
+ EPISODIC = "episodic",
16
+ SEMANTIC = "semantic",
17
+ WORKING = "working",
18
+ }
19
+
20
+ /**
21
+ * 记忆配置接口
22
+ */
23
+ export interface MemoryConfig {
24
+ name: string; // 名称
25
+ description: string; // 描述
26
+ type: MemoryType; // 记忆类型
27
+ ttl?: number; // 生存时间
28
+ capacity?: number; // 容量限制
29
+ }
30
+
31
+ /**
32
+ * 记忆客户端接口
33
+ */
34
+ export interface MemoryClient {
35
+ add: (key: string, value: any) => Promise<void>;
36
+ get: (key: string) => Promise<any>;
37
+ update: (key: string, value: any) => Promise<void>;
38
+ delete: (key: string) => Promise<void>;
39
+ search: (query: string, options?: any) => Promise<any[]>;
40
+ clear: () => Promise<void>;
41
+ }
42
+
43
+ /**
44
+ * 记忆Lattice协议接口
45
+ */
46
+ export interface MemoryLatticeProtocol
47
+ extends BaseLatticeProtocol<MemoryConfig, MemoryClient> {
48
+ // 记忆操作方法
49
+ add: (key: string, value: any) => Promise<void>;
50
+ get: (key: string) => Promise<any>;
51
+ update: (key: string, value: any) => Promise<void>;
52
+ delete: (key: string) => Promise<void>;
53
+ search: (query: string, options?: any) => Promise<any[]>;
54
+ clear: () => Promise<void>;
55
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * MessageProtocol
3
+ *
4
+ */
5
+
6
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
7
+
8
+ /**
9
+ * Base message interface
10
+ */
11
+ export interface BaseMessage {
12
+ id: string; // Unique identifier for the message
13
+ role: string; // Role of the sender (user, assistant, system, tool, developer)
14
+ content?: string; // Optional text content of the message
15
+ name?: string; // Optional name of the sender
16
+ }
17
+
18
+ /**
19
+ * User message interface
20
+ */
21
+ export interface UserMessage extends BaseMessage {
22
+ role: "human";
23
+ content: string; // Text input from the user
24
+ files?: Array<{ name: string; id: string }>; // Optional files attached to the message
25
+ }
26
+
27
+ /**
28
+ * Tool call interface
29
+ */
30
+ export interface ToolCall {
31
+ id: string; // Unique identifier for this tool call
32
+ name: string; // Name of the tool/function to call
33
+ args: Record<string, any>; // Arguments as an object
34
+ type: "tool_call"; // Type of tool call
35
+ response?: string; // Optional response from the tool execution
36
+ }
37
+
38
+ /**
39
+ * Assistant message interface
40
+ */
41
+ export interface AssistantMessage extends BaseMessage {
42
+ role: "ai";
43
+ content?: string; // Text response from the assistant (optional if using tool calls)
44
+ tool_calls?: ToolCall[]; // Optional tool calls made by the assistant
45
+ }
46
+
47
+ /**
48
+ * System message interface
49
+ */
50
+ export interface SystemMessage extends BaseMessage {
51
+ role: "system";
52
+ content: string; // Instructions or context for the assistant
53
+ }
54
+
55
+ /**
56
+ * Tool message interface
57
+ */
58
+ export interface ToolMessage extends BaseMessage {
59
+ role: "tool";
60
+ content: string; // Result from the tool execution
61
+ tool_call_id: string; // ID of the tool call this message responds to
62
+ }
63
+
64
+ /**
65
+ * Developer message interface
66
+ */
67
+ export interface DeveloperMessage extends BaseMessage {
68
+ role: "developer";
69
+ content: string; // Content for development or debugging
70
+ }
71
+
72
+ export interface MessageChunk {
73
+ type: string;
74
+ data: {
75
+ id: string;
76
+ content?: string;
77
+ tool_call_chunks?: Array<{
78
+ name?: string;
79
+ args?: string;
80
+ id?: string;
81
+ index: number;
82
+ }>;
83
+ tool_calls?: Array<{
84
+ name: string;
85
+ args: Record<string, any>;
86
+ id: string;
87
+ type: string;
88
+ response?: string;
89
+ }>;
90
+ additional_kwargs?: {
91
+ tool_calls?: Array<{
92
+ function?: { name?: string; arguments?: any };
93
+ id?: string;
94
+ }>;
95
+ };
96
+ tool_call_id?: string;
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Message type union
102
+ */
103
+ export type Message =
104
+ | UserMessage
105
+ | AssistantMessage
106
+ | SystemMessage
107
+ | ToolMessage
108
+ | DeveloperMessage;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * ModelLatticeProtocol
3
+ *
4
+ * 模型Lattice的协议,用于封装各种LLM模型的统一接口
5
+ */
6
+
7
+ import { BaseChatModel } from "@langchain/core/language_models/chat_models";
8
+ import { BaseMessage } from "@langchain/core/messages";
9
+ import { ChatResult } from "@langchain/core/outputs";
10
+ import { ZodSchema } from "zod";
11
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
12
+
13
+ /**
14
+ * LLM配置接口
15
+ */
16
+ export interface LLMConfig {
17
+ provider: "azure" | "openai" | "deepseek" | "siliconcloud" | "volcengine";
18
+ model: string;
19
+ maxTokens?: number;
20
+ temperature?: number;
21
+ timeout?: number;
22
+ maxRetries?: number;
23
+ streaming?: boolean;
24
+ apiKeyEnvName?: string;
25
+ baseURL?: string;
26
+ }
27
+
28
+ /**
29
+ * 模型Lattice协议接口
30
+ */
31
+ export interface ModelLatticeProtocol
32
+ extends BaseLatticeProtocol<LLMConfig, BaseChatModel> {
33
+ // 模型调用方法
34
+ generate: (messages: BaseMessage[], options?: any) => Promise<ChatResult>;
35
+
36
+ // 结构化输出调用
37
+ invokeWithStructuredOutput: <T>(
38
+ input: any,
39
+ schema: ZodSchema<T>,
40
+ options?: any
41
+ ) => Promise<T>;
42
+
43
+ // 绑定工具
44
+ bindTools: (tools: any[], options?: any) => any;
45
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * StorageLatticeProtocol
3
+ *
4
+ * 存储Lattice的协议,用于数据持久化和状态管理
5
+ */
6
+
7
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
8
+
9
+ /**
10
+ * 存储类型枚举
11
+ */
12
+ export enum StorageType {
13
+ MEMORY = "memory",
14
+ LOCAL = "local",
15
+ DATABASE = "database",
16
+ CLOUD = "cloud",
17
+ DISTRIBUTED = "distributed",
18
+ }
19
+
20
+ /**
21
+ * 存储配置接口
22
+ */
23
+ export interface StorageConfig {
24
+ name: string; // 名称
25
+ description: string; // 描述
26
+ type: StorageType; // 存储类型
27
+ connectionString?: string; // 连接字符串
28
+ options?: Record<string, any>; // 其他选项
29
+ }
30
+
31
+ /**
32
+ * 存储客户端接口
33
+ */
34
+ export interface StorageClient {
35
+ set: (key: string, value: any) => Promise<void>;
36
+ get: (key: string) => Promise<any>;
37
+ has: (key: string) => Promise<boolean>;
38
+ delete: (key: string) => Promise<boolean>;
39
+ clear: () => Promise<void>;
40
+ transaction: <T>(operations: () => Promise<T>) => Promise<T>;
41
+ }
42
+
43
+ /**
44
+ * 存储Lattice协议接口
45
+ */
46
+ export interface StorageLatticeProtocol
47
+ extends BaseLatticeProtocol<StorageConfig, StorageClient> {
48
+ // 存储操作方法
49
+ set: (key: string, value: any) => Promise<void>;
50
+ get: (key: string) => Promise<any>;
51
+ has: (key: string) => Promise<boolean>;
52
+ delete: (key: string) => Promise<boolean>;
53
+ clear: () => Promise<void>;
54
+ transaction: <T>(operations: () => Promise<T>) => Promise<T>;
55
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * ToolLatticeProtocol
3
+ *
4
+ * 工具Lattice的协议,用于定义可执行的功能单元
5
+ */
6
+
7
+ import { StructuredTool } from "@langchain/core/tools";
8
+ import { ZodSchema } from "zod";
9
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
10
+
11
+ /**
12
+ * 工具配置接口
13
+ */
14
+ export interface ToolConfig {
15
+ name: string; // 工具名称
16
+ description: string; // 工具描述
17
+ schema?: ZodSchema<any>; // 输入参数验证模式
18
+ returnDirect?: boolean; // 是否直接返回结果
19
+ needUserApprove?: boolean; // 是否需要用户确认
20
+ }
21
+
22
+ /**
23
+ * 工具执行函数类型
24
+ */
25
+ export type ToolExecutor<TInput = any, TOutput = any> = (
26
+ input: TInput,
27
+ config?: any
28
+ ) => Promise<TOutput>;
29
+
30
+ /**
31
+ * 工具Lattice协议接口
32
+ */
33
+ export interface ToolLatticeProtocol
34
+ extends BaseLatticeProtocol<ToolConfig, StructuredTool> {
35
+ // 工具执行函数
36
+ execute: ToolExecutor;
37
+
38
+ // 验证工具输入
39
+ validate: (input: any) => boolean;
40
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * UILatticeProtocol
3
+ *
4
+ * UI Lattice的协议,用于定义用户界面组件
5
+ */
6
+
7
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
8
+
9
+ /**
10
+ * UI组件类型枚举
11
+ */
12
+ export enum UIComponentType {
13
+ CONTAINER = "container",
14
+ INPUT = "input",
15
+ BUTTON = "button",
16
+ LIST = "list",
17
+ TABLE = "table",
18
+ CHART = "chart",
19
+ FORM = "form",
20
+ CARD = "card",
21
+ MODAL = "modal",
22
+ CUSTOM = "custom",
23
+ }
24
+
25
+ /**
26
+ * UI配置接口
27
+ */
28
+ export interface UIConfig {
29
+ name: string; // 组件名称
30
+ description: string; // 组件描述
31
+ type: UIComponentType; // 组件类型
32
+ props?: Record<string, any>; // 组件属性
33
+ children?: string[]; // 子组件列表
34
+ }
35
+
36
+ /**
37
+ * UI组件接口
38
+ * 使用泛型以适应不同的UI框架(React, Vue等)
39
+ */
40
+ export interface UIComponent<T = any> {
41
+ render: (props?: any) => T;
42
+ addEventListener: (event: string, handler: Function) => void;
43
+ removeEventListener: (event: string, handler: Function) => void;
44
+ }
45
+
46
+ /**
47
+ * UI Lattice协议接口
48
+ */
49
+ export interface UILatticeProtocol<T = any>
50
+ extends BaseLatticeProtocol<UIConfig, UIComponent<T>> {
51
+ // UI渲染方法
52
+ render: (props?: any) => T;
53
+
54
+ // 事件处理
55
+ addEventListener: (event: string, handler: Function) => void;
56
+ removeEventListener: (event: string, handler: Function) => void;
57
+ }
@@ -0,0 +1,183 @@
1
+ /**
2
+ * MessageProtocol Example
3
+ *
4
+ * Demonstrates how to use MessageProtocol for message interaction
5
+ */
6
+
7
+ import {
8
+ Message,
9
+ UserMessage,
10
+ AssistantMessage,
11
+ SystemMessage,
12
+ ToolMessage,
13
+ ToolCall,
14
+ MessageEventType,
15
+ MessagesSnapshotEvent,
16
+ } from "../MessageProtocol";
17
+
18
+ // Example 1: Basic message interaction
19
+ function basicMessageExample() {
20
+ // Create a simple conversation
21
+ const conversation: Message[] = [
22
+ // System message
23
+ {
24
+ id: "sys_1",
25
+ role: "system",
26
+ content: "You are a helpful AI assistant.",
27
+ },
28
+
29
+ // User message
30
+ {
31
+ id: "user_1",
32
+ role: "user",
33
+ content: "Can you help me check the weather in New York?",
34
+ },
35
+
36
+ // Assistant message (with tool call)
37
+ {
38
+ id: "asst_1",
39
+ role: "assistant",
40
+ content: "Let me check the weather for you.",
41
+ toolCalls: [
42
+ {
43
+ id: "tool_call_1",
44
+ type: "function",
45
+ function: {
46
+ name: "get_weather",
47
+ arguments: '{"location": "New York", "unit": "celsius"}',
48
+ },
49
+ },
50
+ ],
51
+ },
52
+
53
+ // Tool message (tool execution result)
54
+ {
55
+ id: "tool_1",
56
+ role: "tool",
57
+ content:
58
+ '{"temperature": 22, "condition": "Partly Cloudy", "humidity": 65}',
59
+ toolCallId: "tool_call_1",
60
+ },
61
+
62
+ // Assistant final response
63
+ {
64
+ id: "asst_2",
65
+ role: "assistant",
66
+ content:
67
+ "The weather in New York is partly cloudy with a temperature of 22°C and 65% humidity.",
68
+ },
69
+ ];
70
+
71
+ return conversation;
72
+ }
73
+
74
+ // Example 2: Message conversion (vendor neutrality)
75
+ function vendorNeutralityExample() {
76
+ const agUiMessages = basicMessageExample();
77
+
78
+ // Convert to OpenAI format
79
+ const openaiMessages = agUiMessages
80
+ .filter((msg) => ["user", "system", "assistant"].includes(msg.role))
81
+ .map((msg) => ({
82
+ role: msg.role as "user" | "system" | "assistant",
83
+ content: msg.content || "",
84
+ // Map tool calls if present
85
+ ...(msg.role === "assistant" && (msg as AssistantMessage).toolCalls
86
+ ? {
87
+ tool_calls: (msg as AssistantMessage).toolCalls?.map((tc) => ({
88
+ id: tc.id,
89
+ type: tc.type,
90
+ function: {
91
+ name: tc.function.name,
92
+ arguments: tc.function.arguments,
93
+ },
94
+ })),
95
+ }
96
+ : {}),
97
+ }));
98
+
99
+ return { agUiMessages, openaiMessages };
100
+ }
101
+
102
+ // Example 3: Message event handling
103
+ function messageEventsExample() {
104
+ // Message snapshot event
105
+ const snapshotEvent: MessagesSnapshotEvent = {
106
+ type: MessageEventType.MESSAGES_SNAPSHOT,
107
+ messages: basicMessageExample(),
108
+ };
109
+
110
+ // Handle events
111
+ function handleEvent(event: any) {
112
+ switch (event.type) {
113
+ case MessageEventType.MESSAGES_SNAPSHOT:
114
+ console.log(
115
+ "Received message snapshot with",
116
+ event.messages.length,
117
+ "messages"
118
+ );
119
+ break;
120
+ case MessageEventType.TEXT_MESSAGE_START:
121
+ console.log(
122
+ "New message started:",
123
+ event.messageId,
124
+ "role:",
125
+ event.role
126
+ );
127
+ break;
128
+ case MessageEventType.TEXT_MESSAGE_CONTENT:
129
+ console.log("Message content:", event.messageId, "delta:", event.delta);
130
+ break;
131
+ case MessageEventType.TEXT_MESSAGE_END:
132
+ console.log("Message ended:", event.messageId);
133
+ break;
134
+ // Other event handling...
135
+ }
136
+ }
137
+
138
+ // Simulate event handling
139
+ handleEvent(snapshotEvent);
140
+ }
141
+
142
+ // Example 4: Streaming message handling
143
+ function streamingMessageExample() {
144
+ // Simulate streaming message processing
145
+ const messageId = "stream_msg_1";
146
+ const chunks = [
147
+ "Hello",
148
+ ", I am ",
149
+ "an AI assistant",
150
+ ". How ",
151
+ "can I help ",
152
+ "you today?",
153
+ ];
154
+
155
+ // Start message
156
+ console.log({
157
+ type: MessageEventType.TEXT_MESSAGE_START,
158
+ messageId,
159
+ role: "assistant",
160
+ });
161
+
162
+ // Stream content
163
+ chunks.forEach((chunk) => {
164
+ console.log({
165
+ type: MessageEventType.TEXT_MESSAGE_CONTENT,
166
+ messageId,
167
+ delta: chunk,
168
+ });
169
+ });
170
+
171
+ // End message
172
+ console.log({
173
+ type: MessageEventType.TEXT_MESSAGE_END,
174
+ messageId,
175
+ });
176
+ }
177
+
178
+ export {
179
+ basicMessageExample,
180
+ vendorNeutralityExample,
181
+ messageEventsExample,
182
+ streamingMessageExample,
183
+ };
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Protocols
3
+ *
4
+ * 导出所有Lattice协议接口,为整个系统提供统一的接口规范
5
+ */
6
+
7
+ export * from "./BaseLatticeProtocol";
8
+ export * from "./ToolLatticeProtocol";
9
+ export * from "./ModelLatticeProtocol";
10
+ export * from "./AgentLatticeProtocol";
11
+ export * from "./MemoryLatticeProtocol";
12
+ export * from "./StorageLatticeProtocol";
13
+ export * from "./UILatticeProtocol";
14
+ export * from "./MessageProtocol";
15
+
16
+ // 导出通用类型
17
+ export * from "./types";