march-ai-sdk 0.3.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 +732 -0
- package/dist/app-C_umwZXh.d.ts +790 -0
- package/dist/extensions/langgraph.d.ts +144 -0
- package/dist/extensions/langgraph.js +326 -0
- package/dist/extensions/langgraph.js.map +1 -0
- package/dist/extensions/vercel-ai.d.ts +124 -0
- package/dist/extensions/vercel-ai.js +177 -0
- package/dist/extensions/vercel-ai.js.map +1 -0
- package/dist/index.d.ts +260 -0
- package/dist/index.js +1695 -0
- package/dist/index.js.map +1 -0
- package/dist/proto/gateway.proto +99 -0
- package/package.json +83 -0
- package/src/agent-state-client.ts +115 -0
- package/src/agent.ts +293 -0
- package/src/api-paths.ts +60 -0
- package/src/app.ts +235 -0
- package/src/artifact.ts +59 -0
- package/src/attachment-client.ts +78 -0
- package/src/checkpoint-client.ts +175 -0
- package/src/conversation-client.ts +109 -0
- package/src/conversation-message.ts +61 -0
- package/src/conversation.ts +123 -0
- package/src/exceptions.ts +78 -0
- package/src/extensions/index.ts +6 -0
- package/src/extensions/langgraph.ts +351 -0
- package/src/extensions/vercel-ai.ts +177 -0
- package/src/gateway-client.ts +420 -0
- package/src/heartbeat.ts +89 -0
- package/src/index.ts +70 -0
- package/src/memory-client.ts +125 -0
- package/src/memory.ts +68 -0
- package/src/message.ts +178 -0
- package/src/proto/gateway.proto +99 -0
- package/src/streamer.ts +242 -0
- package/src/types.ts +196 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// src/exceptions.ts
|
|
2
|
+
var MarchAgentError = class extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "MarchAgentError";
|
|
6
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var APIException = class extends MarchAgentError {
|
|
10
|
+
statusCode;
|
|
11
|
+
constructor(message, statusCode) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "APIException";
|
|
14
|
+
this.statusCode = statusCode;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/agent-state-client.ts
|
|
19
|
+
var AgentStateClient = class {
|
|
20
|
+
baseUrl;
|
|
21
|
+
constructor(baseUrl) {
|
|
22
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Store or update agent state.
|
|
26
|
+
*/
|
|
27
|
+
async put(conversationId, namespace, state) {
|
|
28
|
+
const url = `${this.baseUrl}/agent-state/${conversationId}`;
|
|
29
|
+
const payload = {
|
|
30
|
+
namespace,
|
|
31
|
+
state
|
|
32
|
+
};
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(url, {
|
|
35
|
+
method: "PUT",
|
|
36
|
+
headers: { "Content-Type": "application/json" },
|
|
37
|
+
body: JSON.stringify(payload)
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
const errorText = await response.text();
|
|
41
|
+
throw new APIException(`Failed to store agent state: ${response.status} - ${errorText}`, response.status);
|
|
42
|
+
}
|
|
43
|
+
return await response.json();
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error instanceof APIException) throw error;
|
|
46
|
+
throw new APIException(`Failed to store agent state: ${error}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get agent state.
|
|
51
|
+
*/
|
|
52
|
+
async get(conversationId, namespace) {
|
|
53
|
+
const url = new URL(`${this.baseUrl}/agent-state/${conversationId}`);
|
|
54
|
+
url.searchParams.set("namespace", namespace);
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch(url.toString());
|
|
57
|
+
if (response.status === 404) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
const errorText = await response.text();
|
|
62
|
+
throw new APIException(`Failed to get agent state: ${response.status} - ${errorText}`, response.status);
|
|
63
|
+
}
|
|
64
|
+
return await response.json();
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error instanceof APIException) throw error;
|
|
67
|
+
throw new APIException(`Failed to get agent state: ${error}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Delete agent state.
|
|
72
|
+
*/
|
|
73
|
+
async delete(conversationId, namespace) {
|
|
74
|
+
const url = new URL(`${this.baseUrl}/agent-state/${conversationId}`);
|
|
75
|
+
if (namespace) {
|
|
76
|
+
url.searchParams.set("namespace", namespace);
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const response = await fetch(url.toString(), { method: "DELETE" });
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
const errorText = await response.text();
|
|
82
|
+
throw new APIException(`Failed to delete agent state: ${response.status} - ${errorText}`, response.status);
|
|
83
|
+
}
|
|
84
|
+
return await response.json();
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error instanceof APIException) throw error;
|
|
87
|
+
throw new APIException(`Failed to delete agent state: ${error}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/extensions/vercel-ai.ts
|
|
93
|
+
var VercelAIMessageStore = class _VercelAIMessageStore {
|
|
94
|
+
static NAMESPACE = "vercel_ai";
|
|
95
|
+
client;
|
|
96
|
+
constructor(app) {
|
|
97
|
+
this.client = new AgentStateClient(app.gatewayClient.conversationStoreUrl);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Load message history for a conversation.
|
|
101
|
+
*
|
|
102
|
+
* @param conversationId - The conversation ID to load history for
|
|
103
|
+
* @returns Array of CoreMessage objects (empty array if no history)
|
|
104
|
+
*/
|
|
105
|
+
async load(conversationId) {
|
|
106
|
+
const result = await this.client.get(conversationId, _VercelAIMessageStore.NAMESPACE);
|
|
107
|
+
if (!result) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
const state = result.state ?? {};
|
|
111
|
+
const messages = state.messages;
|
|
112
|
+
if (!Array.isArray(messages)) {
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
return messages.filter(this.isValidMessage);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Save message history for a conversation.
|
|
119
|
+
*
|
|
120
|
+
* @param conversationId - The conversation ID to save history for
|
|
121
|
+
* @param messages - Array of CoreMessage objects to save
|
|
122
|
+
*/
|
|
123
|
+
async save(conversationId, messages) {
|
|
124
|
+
await this.client.put(conversationId, _VercelAIMessageStore.NAMESPACE, {
|
|
125
|
+
messages: messages.map(this.serializeMessage)
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Clear message history for a conversation.
|
|
130
|
+
*
|
|
131
|
+
* @param conversationId - The conversation ID to clear history for
|
|
132
|
+
*/
|
|
133
|
+
async clear(conversationId) {
|
|
134
|
+
await this.client.delete(conversationId, _VercelAIMessageStore.NAMESPACE);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Append messages to existing history.
|
|
138
|
+
*
|
|
139
|
+
* @param conversationId - The conversation ID
|
|
140
|
+
* @param newMessages - Messages to append
|
|
141
|
+
*/
|
|
142
|
+
async append(conversationId, newMessages) {
|
|
143
|
+
const existing = await this.load(conversationId);
|
|
144
|
+
await this.save(conversationId, [...existing, ...newMessages]);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get the last N messages from history.
|
|
148
|
+
*
|
|
149
|
+
* @param conversationId - The conversation ID
|
|
150
|
+
* @param count - Number of messages to retrieve
|
|
151
|
+
*/
|
|
152
|
+
async getLastMessages(conversationId, count) {
|
|
153
|
+
const history = await this.load(conversationId);
|
|
154
|
+
return history.slice(-count);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Validate that an object is a valid message.
|
|
158
|
+
*/
|
|
159
|
+
isValidMessage(msg) {
|
|
160
|
+
if (typeof msg !== "object" || msg === null) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const m = msg;
|
|
164
|
+
const validRoles = ["system", "user", "assistant", "tool"];
|
|
165
|
+
return typeof m.role === "string" && validRoles.includes(m.role) && (typeof m.content === "string" || Array.isArray(m.content));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Serialize a message for storage.
|
|
169
|
+
*/
|
|
170
|
+
serializeMessage(msg) {
|
|
171
|
+
return { ...msg };
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export { VercelAIMessageStore };
|
|
176
|
+
//# sourceMappingURL=vercel-ai.js.map
|
|
177
|
+
//# sourceMappingURL=vercel-ai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exceptions.ts","../../src/agent-state-client.ts","../../src/extensions/vercel-ai.ts"],"names":[],"mappings":";AAQO,IAAM,eAAA,GAAN,cAA8B,KAAA,CAAM;AAAA,EACvC,YAAY,OAAA,EAAiB;AACzB,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AACZ,IAAA,KAAA,CAAM,iBAAA,GAAoB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAAA,EACpD;AACJ,CAAA;AAmCO,IAAM,YAAA,GAAN,cAA2B,eAAA,CAAgB;AAAA,EAC9C,UAAA;AAAA,EAEA,WAAA,CAAY,SAAiB,UAAA,EAAqB;AAC9C,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,UAAA,GAAa,UAAA;AAAA,EACtB;AACJ,CAAA;;;ACtCO,IAAM,mBAAN,MAAuB;AAAA,EACT,OAAA;AAAA,EAEjB,YAAY,OAAA,EAAiB;AACzB,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAAA,EAC5C;AAA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IAAY,KAAA,CAAM,OAAA,CAAQ,EAAE,OAAO,CAAA,CAAA;AAAA,EAEjE;AAAA;AAAA;AAAA;AAAA,EAKQ,iBAAiB,GAAA,EAA2C;AAEhE,IAAA,OAAO,EAAE,GAAG,GAAA,EAAI;AAAA,EACpB;AACJ","file":"vercel-ai.js","sourcesContent":["/**\n * March Agent SDK - Custom Errors\n * Port of Python march_agent/exceptions.py\n */\n\n/**\n * Base error class for March Agent SDK\n */\nexport class MarchAgentError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'MarchAgentError'\n Error.captureStackTrace?.(this, this.constructor)\n }\n}\n\n/**\n * Error during agent registration with AI Inventory\n */\nexport class RegistrationError extends MarchAgentError {\n constructor(message: string) {\n super(message)\n this.name = 'RegistrationError'\n }\n}\n\n/**\n * Error during Kafka operations (produce/consume)\n */\nexport class KafkaError extends MarchAgentError {\n constructor(message: string) {\n super(message)\n this.name = 'KafkaError'\n }\n}\n\n/**\n * Error in SDK configuration\n */\nexport class ConfigurationError extends MarchAgentError {\n constructor(message: string) {\n super(message)\n this.name = 'ConfigurationError'\n }\n}\n\n/**\n * Error from API calls to backend services\n */\nexport class APIException extends MarchAgentError {\n statusCode?: number\n\n constructor(message: string, statusCode?: number) {\n super(message)\n this.name = 'APIException'\n this.statusCode = statusCode\n }\n}\n\n/**\n * Error during heartbeat operations\n */\nexport class HeartbeatError extends MarchAgentError {\n constructor(message: string) {\n super(message)\n this.name = 'HeartbeatError'\n }\n}\n\n/**\n * Error during gRPC connection/communication\n */\nexport class GatewayError extends MarchAgentError {\n constructor(message: string) {\n super(message)\n this.name = 'GatewayError'\n }\n}\n","/**\n * March Agent SDK - Agent State Client\n * Port of Python march_agent/agent_state_client.py\n */\n\nimport { APIException } from './exceptions.js'\n\nexport interface AgentStateResponse {\n conversationId: string\n namespace: string\n state: Record<string, unknown>\n createdAt?: string\n updatedAt?: string\n}\n\n/**\n * Async HTTP client for agent-state API.\n * Used for storing framework-specific state (e.g., Pydantic AI messages).\n */\nexport class AgentStateClient {\n private readonly baseUrl: string\n\n constructor(baseUrl: string) {\n this.baseUrl = baseUrl.replace(/\\/$/, '')\n }\n\n /**\n * Store or update agent state.\n */\n async put(\n conversationId: string,\n namespace: string,\n state: Record<string, unknown>\n ): Promise<{ conversationId: string; namespace: string; created: boolean }> {\n const url = `${this.baseUrl}/agent-state/${conversationId}`\n const payload = {\n namespace,\n state,\n }\n\n try {\n const response = await fetch(url, {\n method: 'PUT',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify(payload),\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new APIException(`Failed to store agent state: ${response.status} - ${errorText}`, response.status)\n }\n\n return await response.json() as { conversationId: string; namespace: string; created: boolean }\n } catch (error) {\n if (error instanceof APIException) throw error\n throw new APIException(`Failed to store agent state: ${error}`)\n }\n }\n\n /**\n * Get agent state.\n */\n async get(\n conversationId: string,\n namespace: string\n ): Promise<AgentStateResponse | null> {\n const url = new URL(`${this.baseUrl}/agent-state/${conversationId}`)\n url.searchParams.set('namespace', namespace)\n\n try {\n const response = await fetch(url.toString())\n\n if (response.status === 404) {\n return null\n }\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new APIException(`Failed to get agent state: ${response.status} - ${errorText}`, response.status)\n }\n\n return await response.json() as AgentStateResponse\n } catch (error) {\n if (error instanceof APIException) throw error\n throw new APIException(`Failed to get agent state: ${error}`)\n }\n }\n\n /**\n * Delete agent state.\n */\n async delete(\n conversationId: string,\n namespace?: string\n ): Promise<{ conversationId: string; namespace?: string; deleted: number }> {\n const url = new URL(`${this.baseUrl}/agent-state/${conversationId}`)\n if (namespace) {\n url.searchParams.set('namespace', namespace)\n }\n\n try {\n const response = await fetch(url.toString(), { method: 'DELETE' })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new APIException(`Failed to delete agent state: ${response.status} - ${errorText}`, response.status)\n }\n\n return await response.json() as { conversationId: string; namespace?: string; deleted: number }\n } catch (error) {\n if (error instanceof APIException) throw error\n throw new APIException(`Failed to delete agent state: ${error}`)\n }\n }\n}\n","/**\n * March Agent SDK - Vercel AI SDK Extension\n * \n * VercelAIMessageStore for persistent message history with Vercel AI SDK.\n * \n * @packageDocumentation\n */\n\nimport type { MarchAgentApp } from '../app.js'\nimport { AgentStateClient } from '../agent-state-client.js'\n\n// Type definitions compatible with Vercel AI SDK's CoreMessage\ninterface CoreMessage {\n role: 'system' | 'user' | 'assistant' | 'tool'\n content: string | Array<{ type: string;[key: string]: unknown }>\n [key: string]: unknown\n}\n\n/**\n * Persistent message store for Vercel AI SDK.\n * \n * Stores and retrieves AI SDK message history using the agent-state API.\n * Messages are serialized as JSON for full fidelity.\n * \n * @example\n * ```typescript\n * import { MarchAgentApp } from 'march-ai-sdk'\n * import { VercelAIMessageStore } from 'march-ai-sdk/extensions/vercel-ai'\n * import { streamText } from 'ai'\n * import { openai } from '@ai-sdk/openai'\n * \n * const app = new MarchAgentApp({\n * gatewayUrl: 'agent-gateway:8080',\n * apiKey: 'your-key',\n * })\n * \n * const store = new VercelAIMessageStore(app)\n * const agent = app.registerMe({ ... })\n * \n * agent.onMessage(async (message, sender) => {\n * // Load message history\n * const history = await store.load(message.conversationId)\n * \n * // Add user message\n * const messages: CoreMessage[] = [\n * ...history,\n * { role: 'user', content: message.content }\n * ]\n * \n * // Stream response\n * const streamer = agent.streamer(message)\n * \n * const result = await streamText({\n * model: openai('gpt-4o'),\n * messages,\n * onChunk: ({ chunk }) => {\n * if (chunk.type === 'text-delta') {\n * streamer.stream(chunk.textDelta)\n * }\n * }\n * })\n * \n * await streamer.finish()\n * \n * // Save updated history\n * await store.save(message.conversationId, [\n * ...messages,\n * { role: 'assistant', content: result.text }\n * ])\n * })\n * \n * app.run()\n * ```\n */\nexport class VercelAIMessageStore {\n private static readonly NAMESPACE = 'vercel_ai'\n private readonly client: AgentStateClient\n\n constructor(app: MarchAgentApp) {\n this.client = new AgentStateClient(app.gatewayClient.conversationStoreUrl)\n }\n\n /**\n * Load message history for a conversation.\n * \n * @param conversationId - The conversation ID to load history for\n * @returns Array of CoreMessage objects (empty array if no history)\n */\n async load(conversationId: string): Promise<CoreMessage[]> {\n const result = await this.client.get(conversationId, VercelAIMessageStore.NAMESPACE)\n\n if (!result) {\n return []\n }\n\n const state = result.state ?? {}\n const messages = state.messages as unknown[]\n\n if (!Array.isArray(messages)) {\n return []\n }\n\n // Validate and return messages\n return messages.filter(this.isValidMessage) as CoreMessage[]\n }\n\n /**\n * Save message history for a conversation.\n * \n * @param conversationId - The conversation ID to save history for\n * @param messages - Array of CoreMessage objects to save\n */\n async save(conversationId: string, messages: CoreMessage[]): Promise<void> {\n await this.client.put(conversationId, VercelAIMessageStore.NAMESPACE, {\n messages: messages.map(this.serializeMessage),\n })\n }\n\n /**\n * Clear message history for a conversation.\n * \n * @param conversationId - The conversation ID to clear history for\n */\n async clear(conversationId: string): Promise<void> {\n await this.client.delete(conversationId, VercelAIMessageStore.NAMESPACE)\n }\n\n /**\n * Append messages to existing history.\n * \n * @param conversationId - The conversation ID\n * @param newMessages - Messages to append\n */\n async append(conversationId: string, newMessages: CoreMessage[]): Promise<void> {\n const existing = await this.load(conversationId)\n await this.save(conversationId, [...existing, ...newMessages])\n }\n\n /**\n * Get the last N messages from history.\n * \n * @param conversationId - The conversation ID\n * @param count - Number of messages to retrieve\n */\n async getLastMessages(conversationId: string, count: number): Promise<CoreMessage[]> {\n const history = await this.load(conversationId)\n return history.slice(-count)\n }\n\n /**\n * Validate that an object is a valid message.\n */\n private isValidMessage(msg: unknown): msg is CoreMessage {\n if (typeof msg !== 'object' || msg === null) {\n return false\n }\n\n const m = msg as Record<string, unknown>\n const validRoles = ['system', 'user', 'assistant', 'tool']\n\n return (\n typeof m.role === 'string' &&\n validRoles.includes(m.role) &&\n (typeof m.content === 'string' || Array.isArray(m.content))\n )\n }\n\n /**\n * Serialize a message for storage.\n */\n private serializeMessage(msg: CoreMessage): Record<string, unknown> {\n // Return a plain object copy\n return { ...msg }\n }\n}\n\nexport type { CoreMessage }\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { G as GatewayClient } from './app-C_umwZXh.js';
|
|
2
|
+
export { A as Agent, a as AgentRegistrationData, b as AppOptions, c as Artifact, d as ArtifactInput, e as ArtifactType, f as AttachmentClient, g as AttachmentInfo, C as Conversation, h as ConversationClient, i as ConversationData, j as ConversationMessage, k as GetMessagesOptions, K as KafkaHeaders, l as KafkaMessage, M as MarchAgentApp, m as Memory, n as MemoryClient, o as MemoryMessage, p as MemorySearchResult, q as Message, r as MessageHandler, P as ProduceAck, R as RegisterOptions, S as SenderFilter, s as SenderFilterOptions, t as StreamOptions, u as Streamer, v as StreamerOptions, U as UserSummary, w as createAttachmentInfo, x as toArtifact } from './app-C_umwZXh.js';
|
|
3
|
+
import 'zod';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* March Agent SDK - Checkpoint Client
|
|
7
|
+
* Port of Python march_agent/checkpoint_client.py
|
|
8
|
+
*/
|
|
9
|
+
interface CheckpointConfig {
|
|
10
|
+
configurable: {
|
|
11
|
+
thread_id: string;
|
|
12
|
+
checkpoint_ns?: string;
|
|
13
|
+
checkpoint_id?: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface CheckpointData {
|
|
17
|
+
v: number;
|
|
18
|
+
id: string;
|
|
19
|
+
ts: string;
|
|
20
|
+
channel_values: Record<string, unknown>;
|
|
21
|
+
channel_versions: Record<string, string>;
|
|
22
|
+
versions_seen: Record<string, Record<string, string>>;
|
|
23
|
+
pending_sends?: unknown[];
|
|
24
|
+
}
|
|
25
|
+
interface CheckpointMetadata {
|
|
26
|
+
source: string;
|
|
27
|
+
step: number;
|
|
28
|
+
writes?: unknown;
|
|
29
|
+
parents?: Record<string, string>;
|
|
30
|
+
}
|
|
31
|
+
interface CheckpointTuple {
|
|
32
|
+
config: CheckpointConfig;
|
|
33
|
+
checkpoint: CheckpointData;
|
|
34
|
+
metadata: CheckpointMetadata;
|
|
35
|
+
parent_config?: CheckpointConfig;
|
|
36
|
+
pending_writes?: unknown[];
|
|
37
|
+
}
|
|
38
|
+
interface CheckpointListOptions {
|
|
39
|
+
threadId?: string;
|
|
40
|
+
checkpointNs?: string;
|
|
41
|
+
before?: string;
|
|
42
|
+
limit?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Async HTTP client for checkpoint-store API.
|
|
46
|
+
* Used by LangGraph integration for persisting graph state.
|
|
47
|
+
*/
|
|
48
|
+
declare class CheckpointClient {
|
|
49
|
+
private readonly baseUrl;
|
|
50
|
+
constructor(baseUrl: string);
|
|
51
|
+
/**
|
|
52
|
+
* Store a checkpoint.
|
|
53
|
+
*/
|
|
54
|
+
put(config: CheckpointConfig, checkpoint: CheckpointData, metadata: CheckpointMetadata, newVersions?: Record<string, unknown>): Promise<{
|
|
55
|
+
config: CheckpointConfig;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Get a checkpoint tuple.
|
|
59
|
+
*/
|
|
60
|
+
getTuple(threadId: string, checkpointNs?: string, checkpointId?: string): Promise<CheckpointTuple | null>;
|
|
61
|
+
/**
|
|
62
|
+
* List checkpoints.
|
|
63
|
+
*/
|
|
64
|
+
list(options?: CheckpointListOptions): Promise<CheckpointTuple[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Delete all checkpoints for a thread.
|
|
67
|
+
*/
|
|
68
|
+
deleteThread(threadId: string): Promise<{
|
|
69
|
+
thread_id: string;
|
|
70
|
+
deleted: number;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* March Agent SDK - Agent State Client
|
|
76
|
+
* Port of Python march_agent/agent_state_client.py
|
|
77
|
+
*/
|
|
78
|
+
interface AgentStateResponse {
|
|
79
|
+
conversationId: string;
|
|
80
|
+
namespace: string;
|
|
81
|
+
state: Record<string, unknown>;
|
|
82
|
+
createdAt?: string;
|
|
83
|
+
updatedAt?: string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Async HTTP client for agent-state API.
|
|
87
|
+
* Used for storing framework-specific state (e.g., Pydantic AI messages).
|
|
88
|
+
*/
|
|
89
|
+
declare class AgentStateClient {
|
|
90
|
+
private readonly baseUrl;
|
|
91
|
+
constructor(baseUrl: string);
|
|
92
|
+
/**
|
|
93
|
+
* Store or update agent state.
|
|
94
|
+
*/
|
|
95
|
+
put(conversationId: string, namespace: string, state: Record<string, unknown>): Promise<{
|
|
96
|
+
conversationId: string;
|
|
97
|
+
namespace: string;
|
|
98
|
+
created: boolean;
|
|
99
|
+
}>;
|
|
100
|
+
/**
|
|
101
|
+
* Get agent state.
|
|
102
|
+
*/
|
|
103
|
+
get(conversationId: string, namespace: string): Promise<AgentStateResponse | null>;
|
|
104
|
+
/**
|
|
105
|
+
* Delete agent state.
|
|
106
|
+
*/
|
|
107
|
+
delete(conversationId: string, namespace?: string): Promise<{
|
|
108
|
+
conversationId: string;
|
|
109
|
+
namespace?: string;
|
|
110
|
+
deleted: number;
|
|
111
|
+
}>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* March Agent SDK - Heartbeat Manager
|
|
116
|
+
* Port of Python march_agent/heartbeat.py
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Manages periodic heartbeats to keep the agent status active.
|
|
121
|
+
* Sends heartbeats via HTTP to the AI Inventory service.
|
|
122
|
+
*/
|
|
123
|
+
declare class HeartbeatManager {
|
|
124
|
+
private readonly gatewayClient;
|
|
125
|
+
private readonly agentName;
|
|
126
|
+
private readonly intervalMs;
|
|
127
|
+
private timer?;
|
|
128
|
+
private running;
|
|
129
|
+
constructor(gatewayClient: GatewayClient, agentName: string, intervalSeconds?: number);
|
|
130
|
+
/**
|
|
131
|
+
* Start sending heartbeats.
|
|
132
|
+
*/
|
|
133
|
+
start(): void;
|
|
134
|
+
/**
|
|
135
|
+
* Stop sending heartbeats.
|
|
136
|
+
*/
|
|
137
|
+
stop(): void;
|
|
138
|
+
/**
|
|
139
|
+
* Send a single heartbeat via HTTP to AI Inventory.
|
|
140
|
+
*/
|
|
141
|
+
private sendHeartbeat;
|
|
142
|
+
/**
|
|
143
|
+
* Check if heartbeat is running.
|
|
144
|
+
*/
|
|
145
|
+
isRunning(): boolean;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* March Agent SDK - API Paths Configuration
|
|
150
|
+
*
|
|
151
|
+
* Centralized configuration for all API endpoint paths.
|
|
152
|
+
* This allows easy configuration if API versions or paths change.
|
|
153
|
+
*/
|
|
154
|
+
/**
|
|
155
|
+
* API paths for AI Inventory service.
|
|
156
|
+
*/
|
|
157
|
+
declare const AI_INVENTORY_PATHS: {
|
|
158
|
+
/** Register a new agent */
|
|
159
|
+
readonly AGENT_REGISTER: "/api/v1/agents/register";
|
|
160
|
+
/** Send heartbeat */
|
|
161
|
+
readonly HEALTH_HEARTBEAT: "/api/v1/health/heartbeat";
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* API paths for Conversation Store service.
|
|
165
|
+
* Note: These paths don't have /api/v1/ prefix.
|
|
166
|
+
*/
|
|
167
|
+
declare const CONVERSATION_STORE_PATHS: {
|
|
168
|
+
/** Get/update conversation by ID */
|
|
169
|
+
readonly CONVERSATION: (conversationId: string) => string;
|
|
170
|
+
/** Get messages for a conversation */
|
|
171
|
+
readonly CONVERSATION_MESSAGES: (conversationId: string) => string;
|
|
172
|
+
/** Checkpoints base path */
|
|
173
|
+
readonly CHECKPOINTS: "/checkpoints/";
|
|
174
|
+
/** Checkpoint by thread ID */
|
|
175
|
+
readonly CHECKPOINT_THREAD: (threadId: string) => string;
|
|
176
|
+
/** Agent state by conversation ID */
|
|
177
|
+
readonly AGENT_STATE: (conversationId: string) => string;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* API paths for AI Memory service.
|
|
181
|
+
* Note: These paths don't have /api/v1/ prefix.
|
|
182
|
+
*/
|
|
183
|
+
declare const MEMORY_PATHS: {
|
|
184
|
+
/** User memory base */
|
|
185
|
+
readonly USER_MEMORY: (userId: string) => string;
|
|
186
|
+
/** Add messages to memory */
|
|
187
|
+
readonly USER_MESSAGES: (userId: string) => string;
|
|
188
|
+
/** Search user memory */
|
|
189
|
+
readonly USER_SEARCH: (userId: string) => string;
|
|
190
|
+
/** Get user summary */
|
|
191
|
+
readonly USER_SUMMARY: (userId: string) => string;
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Service names for gateway proxy routing.
|
|
195
|
+
*/
|
|
196
|
+
declare const SERVICES: {
|
|
197
|
+
readonly AI_INVENTORY: "ai-inventory";
|
|
198
|
+
readonly CONVERSATION_STORE: "conversation-store";
|
|
199
|
+
readonly AI_MEMORY: "ai-memory";
|
|
200
|
+
readonly ATTACHMENT: "attachment";
|
|
201
|
+
};
|
|
202
|
+
type ServiceName = typeof SERVICES[keyof typeof SERVICES];
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* March Agent SDK - Custom Errors
|
|
206
|
+
* Port of Python march_agent/exceptions.py
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* Base error class for March Agent SDK
|
|
210
|
+
*/
|
|
211
|
+
declare class MarchAgentError extends Error {
|
|
212
|
+
constructor(message: string);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Error during agent registration with AI Inventory
|
|
216
|
+
*/
|
|
217
|
+
declare class RegistrationError extends MarchAgentError {
|
|
218
|
+
constructor(message: string);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Error during Kafka operations (produce/consume)
|
|
222
|
+
*/
|
|
223
|
+
declare class KafkaError extends MarchAgentError {
|
|
224
|
+
constructor(message: string);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Error in SDK configuration
|
|
228
|
+
*/
|
|
229
|
+
declare class ConfigurationError extends MarchAgentError {
|
|
230
|
+
constructor(message: string);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Error from API calls to backend services
|
|
234
|
+
*/
|
|
235
|
+
declare class APIException extends MarchAgentError {
|
|
236
|
+
statusCode?: number;
|
|
237
|
+
constructor(message: string, statusCode?: number);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Error during heartbeat operations
|
|
241
|
+
*/
|
|
242
|
+
declare class HeartbeatError extends MarchAgentError {
|
|
243
|
+
constructor(message: string);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Error during gRPC connection/communication
|
|
247
|
+
*/
|
|
248
|
+
declare class GatewayError extends MarchAgentError {
|
|
249
|
+
constructor(message: string);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* March Agent SDK - TypeScript framework for building AI agents
|
|
254
|
+
*
|
|
255
|
+
* @packageDocumentation
|
|
256
|
+
*/
|
|
257
|
+
|
|
258
|
+
declare const VERSION = "0.3.0";
|
|
259
|
+
|
|
260
|
+
export { AI_INVENTORY_PATHS, APIException, AgentStateClient, type AgentStateResponse, CONVERSATION_STORE_PATHS, CheckpointClient, type CheckpointConfig, type CheckpointData, type CheckpointMetadata, type CheckpointTuple, ConfigurationError, GatewayClient, GatewayError, HeartbeatError, HeartbeatManager, KafkaError, MEMORY_PATHS, MarchAgentError, RegistrationError, SERVICES, type ServiceName, VERSION };
|