openliate 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +295 -2
- package/dist/bin.js +46374 -0
- package/dist/engine/engine.zip +0 -0
- package/dist/engine/index.js +36790 -0
- package/hub/agents/email-agent/agent.json +19 -0
- package/hub/agents/general-agent/agent.json +18 -0
- package/hub/agents/identity-agent/agent.json +19 -0
- package/hub/agents/research-agent/agent.json +18 -0
- package/hub/agents/sarvam-agent/agent.json +19 -0
- package/hub/llm/sarvam.json +27 -0
- package/hub/mcp/agentmail.json +10 -0
- package/hub/mcp/fetch.json +9 -0
- package/hub/mcp/neon.json +12 -0
- package/hub/mcp/sarvam.json +12 -0
- package/hub/mcp/supabase.json +12 -0
- package/hub/mcp/vercel.json +12 -0
- package/package.json +59 -4
- package/src/agent/agent.ts +165 -0
- package/src/agent/loop.ts +183 -0
- package/src/agent/types.ts +53 -0
- package/src/cli.ts +272 -0
- package/src/core/aum.ts +78 -0
- package/src/core/uuid.ts +19 -0
- package/src/engine/types.ts +104 -0
- package/src/environment/environment.ts +88 -0
- package/src/environment/types.ts +15 -0
- package/src/integration/integration.ts +37 -0
- package/src/integration/memory.ts +153 -0
- package/src/integration/types.ts +31 -0
- package/src/liate.ts +25 -0
- package/src/link/index.ts +317 -0
- package/src/link/neon.ts +646 -0
- package/src/link/supabase.ts +567 -0
- package/src/link/vercel.ts +633 -0
- package/src/llm/config.ts +46 -0
- package/src/llm/hub.ts +59 -0
- package/src/llm/llm.ts +60 -0
- package/src/llm/sarvam.ts +136 -0
- package/src/server/engine.ts +277 -0
- package/src/server/index.ts +32 -0
- package/src/tools/builtin/fetch.ts +66 -0
- package/src/tools/builtin/identity.ts +131 -0
- package/src/tools/mcp.ts +222 -0
- package/src/tools/registry.ts +218 -0
- package/src/tools/server.ts +70 -0
- package/src/tools/types.ts +37 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"L": "sarvam/sarvam-105b",
|
|
3
|
+
"I": {
|
|
4
|
+
"memory": "sessions/email-agent"
|
|
5
|
+
},
|
|
6
|
+
"A": {
|
|
7
|
+
"name": "email_agent",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"intent": "Autonomous Email Management Agent",
|
|
10
|
+
"maxSteps": 5
|
|
11
|
+
},
|
|
12
|
+
"T": [
|
|
13
|
+
"mcp:agentmail:list_inboxes,get_inbox,search_messages"
|
|
14
|
+
],
|
|
15
|
+
"E": {
|
|
16
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY",
|
|
17
|
+
"AGENTMAIL_API_KEY": "$AGENTMAIL_API_KEY"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"L": "sarvam/sarvam-105b",
|
|
3
|
+
"I": {
|
|
4
|
+
"memory": "sessions/general-agent"
|
|
5
|
+
},
|
|
6
|
+
"A": {
|
|
7
|
+
"name": "general_agent",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"intent": "Explain Liate: The Open Source Backend Layer for AI Agents",
|
|
10
|
+
"maxSteps": 5
|
|
11
|
+
},
|
|
12
|
+
"T": [
|
|
13
|
+
"fetch"
|
|
14
|
+
],
|
|
15
|
+
"E": {
|
|
16
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"L": "sarvam/sarvam-105b",
|
|
3
|
+
"I": {
|
|
4
|
+
"memory": "sessions/identity-agent"
|
|
5
|
+
},
|
|
6
|
+
"A": {
|
|
7
|
+
"name": "identity_agent",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"intent": "Verify and validate Indian identity credentials including 12-digit Aadhaar (Verhoeff checksum) and 10-character PAN card format with taxpayer classification.",
|
|
10
|
+
"maxSteps": 5
|
|
11
|
+
},
|
|
12
|
+
"T": [
|
|
13
|
+
"verify_identity"
|
|
14
|
+
],
|
|
15
|
+
"E": {
|
|
16
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"L": "sarvam/sarvam-105b",
|
|
3
|
+
"I": {
|
|
4
|
+
"memory": "sessions/research-agent"
|
|
5
|
+
},
|
|
6
|
+
"A": {
|
|
7
|
+
"name": "research_agent",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"intent": "Autonomous Web Research and Knowledge Extraction Agent",
|
|
10
|
+
"maxSteps": 5
|
|
11
|
+
},
|
|
12
|
+
"T": [
|
|
13
|
+
"mcp:fetch"
|
|
14
|
+
],
|
|
15
|
+
"E": {
|
|
16
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"L": "sarvam/sarvam-105b",
|
|
3
|
+
"I": {
|
|
4
|
+
"memory": "sessions/sarvam-agent"
|
|
5
|
+
},
|
|
6
|
+
"A": {
|
|
7
|
+
"name": "sarvam_agent",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"intent": "Autonomous Sovereign Indic AI Agent using Sarvam MCP tools",
|
|
10
|
+
"maxSteps": 5
|
|
11
|
+
},
|
|
12
|
+
"T": [
|
|
13
|
+
"mcp:sarvam"
|
|
14
|
+
],
|
|
15
|
+
"E": {
|
|
16
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sarvam",
|
|
3
|
+
"provider": "sarvam",
|
|
4
|
+
"version": "v2",
|
|
5
|
+
"base_url": "https://api.sarvam.ai/v2",
|
|
6
|
+
"endpoint": "https://api.sarvam.ai/v2/chat/completions",
|
|
7
|
+
"default_model": "sarvam-105b",
|
|
8
|
+
"available_models": [
|
|
9
|
+
"sarvam-105b",
|
|
10
|
+
"sarvam-105b-conversations",
|
|
11
|
+
"glm5.3",
|
|
12
|
+
"glm5.3-flash",
|
|
13
|
+
"gemma4",
|
|
14
|
+
"deepseekv4.1-flash-1017",
|
|
15
|
+
"kimi-k3"
|
|
16
|
+
],
|
|
17
|
+
"headers": {
|
|
18
|
+
"api-subscription-key": "$SARVAM_API_KEY",
|
|
19
|
+
"Authorization": "Bearer $SARVAM_API_KEY",
|
|
20
|
+
"Content-Type": "application/json"
|
|
21
|
+
},
|
|
22
|
+
"features": {
|
|
23
|
+
"tool_calling": true,
|
|
24
|
+
"streaming": true,
|
|
25
|
+
"reasoning": true
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "agentmail",
|
|
3
|
+
"name": "AgentMail",
|
|
4
|
+
"description": "Send and receive agent emails via native MCP v2 API",
|
|
5
|
+
"transport": "http",
|
|
6
|
+
"url": "https://mcp.agentmail.to/mcp?apiKey=$AGENTMAIL_API_KEY",
|
|
7
|
+
"headers": {
|
|
8
|
+
"Accept": "application/json, text/event-stream"
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "neon",
|
|
3
|
+
"name": "Neon",
|
|
4
|
+
"description": "Serverless Postgres database management, branching, and compute via Neon MCP",
|
|
5
|
+
"transport": "http",
|
|
6
|
+
"url": "https://mcp.neon.tech/mcp",
|
|
7
|
+
"headers": {
|
|
8
|
+
"Authorization": "Bearer $NEON_MCP_TOKEN",
|
|
9
|
+
"Accept": "application/json, text/event-stream"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "sarvam",
|
|
3
|
+
"name": "Sarvam AI",
|
|
4
|
+
"description": "Official Sarvam AI MCP server for speech, translation, Bulbul v4 TTS, and document intelligence",
|
|
5
|
+
"transport": "stdio",
|
|
6
|
+
"command": "uvx",
|
|
7
|
+
"args": ["sarvam-mcp"],
|
|
8
|
+
"env": {
|
|
9
|
+
"SARVAM_API_KEY": "$SARVAM_API_KEY"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "supabase",
|
|
3
|
+
"name": "Supabase",
|
|
4
|
+
"description": "Supabase Edge Functions, pgvector database, and storage management via Supabase MCP",
|
|
5
|
+
"transport": "http",
|
|
6
|
+
"url": "https://mcp.supabase.com/mcp",
|
|
7
|
+
"headers": {
|
|
8
|
+
"Authorization": "Bearer $SUPABASE_MCP_TOKEN",
|
|
9
|
+
"Accept": "application/json, text/event-stream"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "vercel",
|
|
3
|
+
"name": "Vercel",
|
|
4
|
+
"description": "Vercel deployment management, projects, and fluid compute via official Vercel MCP",
|
|
5
|
+
"transport": "http",
|
|
6
|
+
"url": "https://mcp.vercel.com",
|
|
7
|
+
"headers": {
|
|
8
|
+
"Authorization": "Bearer $VERCEL_TOKEN",
|
|
9
|
+
"Accept": "application/json, text/event-stream"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openliate",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Open Source Backend Layer for AI Agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/liate.ts",
|
|
7
|
+
"types": "./src/engine/types.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"openliate": "dist/bin.js",
|
|
10
|
+
"liate": "dist/bin.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/bin.js",
|
|
14
|
+
"dist/engine",
|
|
15
|
+
"src",
|
|
16
|
+
"hub"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "bun test",
|
|
20
|
+
"build": "bun run build:bin && bun run build:cli && bun run build:engine",
|
|
21
|
+
"build:bin": "bun build ./src/cli.ts --target node --format esm --outfile dist/bin.js",
|
|
22
|
+
"build:cli": "bun build ./src/liate.ts --compile --outfile dist/cli/liate",
|
|
23
|
+
"build:engine": "bun build ./src/server/index.ts --target node --format esm --outfile dist/engine/index.js",
|
|
24
|
+
"serve": "bun run dist/engine/index.js",
|
|
25
|
+
"typecheck": "tsc --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@clack/prompts": "1.8.1",
|
|
29
|
+
"@hono/node-server": "2.1.5",
|
|
30
|
+
"@modelcontextprotocol/client": "2.4.0",
|
|
31
|
+
"@modelcontextprotocol/core": "2.4.0",
|
|
32
|
+
"@modelcontextprotocol/server": "2.4.0",
|
|
33
|
+
"hono": "^4.7.0",
|
|
34
|
+
"jszip": "3.10.2",
|
|
35
|
+
"zod": "^3.24.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^22.10.0",
|
|
39
|
+
"typescript": "^5.7.0"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"liate",
|
|
43
|
+
"agent",
|
|
44
|
+
"autonomous-agents",
|
|
45
|
+
"sovereign-ai",
|
|
46
|
+
"agent-platform",
|
|
47
|
+
"runtime",
|
|
48
|
+
"mcp",
|
|
49
|
+
"sarvamai"
|
|
50
|
+
],
|
|
51
|
+
"author": "I Am Azent (@iamazent)",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/openliate/openliate.git"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/openliate/openliate/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://openliate.tech"
|
|
61
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { AUM } from "../core/aum";
|
|
4
|
+
import type { AgentManifest } from "../engine/types";
|
|
5
|
+
import { SarvamClient } from "../llm/sarvam";
|
|
6
|
+
import { normalizeModelConfig } from "../llm/config";
|
|
7
|
+
import { IntegrationResolver } from "../integration/integration";
|
|
8
|
+
import type { MemoryProvider } from "../integration/types";
|
|
9
|
+
import { ReActLoop } from "./loop";
|
|
10
|
+
import type { AgentTool, AgentContext, AgentRunResult, AgentExecutionOptions } from "./types";
|
|
11
|
+
import { ToolRegistry } from "../tools/registry";
|
|
12
|
+
import { EnvironmentResolver } from "../environment/environment";
|
|
13
|
+
|
|
14
|
+
export interface AgentConfigOptions {
|
|
15
|
+
manifest?: string | AgentManifest;
|
|
16
|
+
name?: string;
|
|
17
|
+
version?: string;
|
|
18
|
+
intent?: string;
|
|
19
|
+
model?: string;
|
|
20
|
+
memory?: string | MemoryProvider;
|
|
21
|
+
sessionId?: string;
|
|
22
|
+
tools?: Array<string | AgentTool>;
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class Agent {
|
|
27
|
+
public readonly name: string;
|
|
28
|
+
public readonly version: string;
|
|
29
|
+
public readonly intent: string;
|
|
30
|
+
public readonly model: string;
|
|
31
|
+
public readonly manifest?: AgentManifest;
|
|
32
|
+
private memory: MemoryProvider;
|
|
33
|
+
private llm: SarvamClient;
|
|
34
|
+
private tools: Map<string, AgentTool> = new Map();
|
|
35
|
+
private maxSteps: number = 10;
|
|
36
|
+
private toolsInitialized: boolean = false;
|
|
37
|
+
private rawToolSpecs: Array<string | AgentTool> = [];
|
|
38
|
+
|
|
39
|
+
constructor(options: AgentConfigOptions = {}) {
|
|
40
|
+
let resolvedManifest: AgentManifest | undefined;
|
|
41
|
+
|
|
42
|
+
if (typeof options.manifest === "string") {
|
|
43
|
+
const manifestPath = path.resolve(process.cwd(), options.manifest);
|
|
44
|
+
EnvironmentResolver.loadEnv(manifestPath);
|
|
45
|
+
const raw = fs.readFileSync(manifestPath, "utf-8");
|
|
46
|
+
resolvedManifest = AUM.resolveEnvironment(AUM.parse(raw));
|
|
47
|
+
} else if (typeof options.manifest === "object") {
|
|
48
|
+
EnvironmentResolver.loadEnv();
|
|
49
|
+
resolvedManifest = AUM.resolveEnvironment(AUM.parse(options.manifest));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.manifest = resolvedManifest;
|
|
53
|
+
|
|
54
|
+
// Resolve Name, Version, and Intent from manifest or programmatic options
|
|
55
|
+
this.name = options.name || resolvedManifest?.A?.name || "unnamed_agent";
|
|
56
|
+
this.version = options.version || resolvedManifest?.A?.version || "1.0.0";
|
|
57
|
+
this.intent = options.intent || resolvedManifest?.A?.intent || "Autonomous AI Agent";
|
|
58
|
+
this.maxSteps = resolvedManifest?.A?.maxSteps ?? 10;
|
|
59
|
+
|
|
60
|
+
// Resolve Model ([L] Layer) - strictly dynamic, no hardcoding
|
|
61
|
+
const normalizedL = resolvedManifest?.L ? normalizeModelConfig(resolvedManifest.L) : undefined;
|
|
62
|
+
const resolvedModel = options.model || normalizedL?.model;
|
|
63
|
+
if (!resolvedModel) {
|
|
64
|
+
throw new Error("Missing required model definition in Layer [L]");
|
|
65
|
+
}
|
|
66
|
+
this.model = resolvedModel;
|
|
67
|
+
|
|
68
|
+
// Resolve Memory Provider ([I] Layer)
|
|
69
|
+
if (options.memory && typeof options.memory === "object" && "append" in options.memory) {
|
|
70
|
+
this.memory = options.memory as MemoryProvider;
|
|
71
|
+
} else {
|
|
72
|
+
const memoryConfig = options.memory || resolvedManifest?.I;
|
|
73
|
+
const sId = options.sessionId || (typeof resolvedManifest?.I === "object" ? (resolvedManifest.I as any).sessionId || (resolvedManifest.I as any).session : undefined);
|
|
74
|
+
this.memory = IntegrationResolver.resolveMemory(memoryConfig, {
|
|
75
|
+
agentName: this.name,
|
|
76
|
+
agentVersion: this.version
|
|
77
|
+
}, sId);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Resolve LLM Provider ([L] Layer)
|
|
81
|
+
const apiKey = options.apiKey || resolvedManifest?.E?.SARVAM_API_KEY || process.env.SARVAM_API_KEY;
|
|
82
|
+
this.llm = new SarvamClient(apiKey);
|
|
83
|
+
|
|
84
|
+
// Collect tool specs from options or manifest ([T] Layer)
|
|
85
|
+
if (options.tools) {
|
|
86
|
+
this.rawToolSpecs.push(...options.tools);
|
|
87
|
+
}
|
|
88
|
+
if (resolvedManifest?.T) {
|
|
89
|
+
if (Array.isArray(resolvedManifest.T)) {
|
|
90
|
+
this.rawToolSpecs.push(...resolvedManifest.T);
|
|
91
|
+
} else if (typeof resolvedManifest.T === "object") {
|
|
92
|
+
for (const [key, val] of Object.entries(resolvedManifest.T)) {
|
|
93
|
+
if (typeof val === "object" && val !== null) {
|
|
94
|
+
this.rawToolSpecs.push({ id: key, name: key, ...val });
|
|
95
|
+
} else {
|
|
96
|
+
this.rawToolSpecs.push(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public registerTool(tool: AgentTool): void {
|
|
104
|
+
this.tools.set(tool.name, tool);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getMemory(): MemoryProvider {
|
|
108
|
+
return this.memory;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getContext(): AgentContext {
|
|
112
|
+
return {
|
|
113
|
+
agentName: this.name,
|
|
114
|
+
agentVersion: this.version,
|
|
115
|
+
sessionId: this.memory.sessionId,
|
|
116
|
+
state: {
|
|
117
|
+
intent: this.intent
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public async getTools(): Promise<AgentTool[]> {
|
|
123
|
+
await this.initializeTools();
|
|
124
|
+
return Array.from(this.tools.values());
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public async initializeTools(): Promise<void> {
|
|
128
|
+
if (this.toolsInitialized) return;
|
|
129
|
+
|
|
130
|
+
if (this.rawToolSpecs.length > 0) {
|
|
131
|
+
const customEnv = (this.manifest?.E as Record<string, string>) || process.env;
|
|
132
|
+
const resolved = await ToolRegistry.resolveTools(this.rawToolSpecs, customEnv);
|
|
133
|
+
for (const t of resolved) {
|
|
134
|
+
this.tools.set(t.name, t);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.toolsInitialized = true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public async run(input: string, options: AgentExecutionOptions = {}): Promise<AgentRunResult> {
|
|
142
|
+
await this.initializeTools();
|
|
143
|
+
|
|
144
|
+
const allTools = [
|
|
145
|
+
...Array.from(this.tools.values()),
|
|
146
|
+
...(options.tools || [])
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
const context = this.getContext();
|
|
150
|
+
|
|
151
|
+
return ReActLoop.execute(
|
|
152
|
+
input,
|
|
153
|
+
this.llm,
|
|
154
|
+
this.memory,
|
|
155
|
+
allTools,
|
|
156
|
+
context,
|
|
157
|
+
{
|
|
158
|
+
model: options.model ?? this.model,
|
|
159
|
+
maxSteps: options.maxSteps ?? this.maxSteps,
|
|
160
|
+
systemPrompt: options.systemPrompt,
|
|
161
|
+
temperature: options.temperature
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { AgentTool, AgentContext, AgentStep, AgentRunResult, AgentExecutionOptions, ToolCall, ToolResult } from "./types";
|
|
2
|
+
import type { MemoryProvider, AgentMessage } from "../integration/types";
|
|
3
|
+
import type { SarvamMessage, SarvamChatResponse } from "../llm/sarvam";
|
|
4
|
+
|
|
5
|
+
export interface LLMFunctionCaller {
|
|
6
|
+
chat(messages: SarvamMessage[], options?: any): Promise<SarvamChatResponse & { toolCalls?: ToolCall[] }>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class ReActLoop {
|
|
10
|
+
public static async execute(
|
|
11
|
+
userInput: string,
|
|
12
|
+
llm: LLMFunctionCaller,
|
|
13
|
+
memory: MemoryProvider,
|
|
14
|
+
tools: AgentTool[] = [],
|
|
15
|
+
context: AgentContext,
|
|
16
|
+
options: AgentExecutionOptions = {}
|
|
17
|
+
): Promise<AgentRunResult> {
|
|
18
|
+
const startTime = Date.now();
|
|
19
|
+
const maxSteps = options.maxSteps ?? 10;
|
|
20
|
+
const steps: AgentStep[] = [];
|
|
21
|
+
const toolMap = new Map<string, AgentTool>(tools.map(t => [t.name, t]));
|
|
22
|
+
|
|
23
|
+
const formattedTools = tools.length > 0 ? tools.map(t => ({
|
|
24
|
+
type: "function",
|
|
25
|
+
function: {
|
|
26
|
+
name: t.name,
|
|
27
|
+
description: t.description,
|
|
28
|
+
parameters: t.parameters || { type: "object", properties: {} }
|
|
29
|
+
}
|
|
30
|
+
})) : undefined;
|
|
31
|
+
|
|
32
|
+
// 1. Append user input to session memory
|
|
33
|
+
await memory.append({
|
|
34
|
+
role: "user",
|
|
35
|
+
content: userInput,
|
|
36
|
+
timestamp: startTime
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
let currentStep = 1;
|
|
40
|
+
let finalOutput = "";
|
|
41
|
+
let finalThought: string | undefined = undefined;
|
|
42
|
+
let finishReason: "stop" | "max_steps_reached" | "error" = "max_steps_reached";
|
|
43
|
+
let usedModel = options.model || "unknown";
|
|
44
|
+
|
|
45
|
+
// Build system message incorporating Intent, Name, and Version
|
|
46
|
+
const systemInstruction = options.systemPrompt ??
|
|
47
|
+
`You are ${context.agentName} (v${context.agentVersion}).
|
|
48
|
+
Your mission and intent: ${context.state.intent || "Autonomous AI Agent"}.
|
|
49
|
+
Follow the ReAct methodology: Reason about the user's request, select tools if needed, observe outputs, and provide a clear final answer.`;
|
|
50
|
+
|
|
51
|
+
while (currentStep <= maxSteps) {
|
|
52
|
+
const stepStartTime = Date.now();
|
|
53
|
+
|
|
54
|
+
// Retrieve full history from memory
|
|
55
|
+
const conversationHistory: SarvamMessage[] = memory.getMessages().map(m => {
|
|
56
|
+
const item: SarvamMessage = {
|
|
57
|
+
role: m.role as "system" | "user" | "assistant" | "tool",
|
|
58
|
+
content: m.content
|
|
59
|
+
};
|
|
60
|
+
if (m.name) item.name = m.name;
|
|
61
|
+
if (m.toolCallId) item.tool_call_id = m.toolCallId;
|
|
62
|
+
if (m.toolCalls && m.toolCalls.length > 0) {
|
|
63
|
+
item.tool_calls = m.toolCalls.map(tc => ({
|
|
64
|
+
id: tc.id,
|
|
65
|
+
type: "function",
|
|
66
|
+
function: {
|
|
67
|
+
name: tc.name,
|
|
68
|
+
arguments: JSON.stringify(tc.arguments)
|
|
69
|
+
}
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
return item;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const messages: SarvamMessage[] = [
|
|
76
|
+
{ role: "system", content: systemInstruction },
|
|
77
|
+
...conversationHistory
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
// Query LLM
|
|
81
|
+
const response = await llm.chat(messages, {
|
|
82
|
+
model: options.model,
|
|
83
|
+
maxTokens: options.maxTokens,
|
|
84
|
+
temperature: options.temperature ?? 0.2,
|
|
85
|
+
tools: formattedTools
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
usedModel = response.model || usedModel;
|
|
89
|
+
const thought = response.reasoningContent;
|
|
90
|
+
if (thought) {
|
|
91
|
+
finalThought = thought;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check if LLM requested tool calling
|
|
95
|
+
const requestedToolCalls: ToolCall[] = response.toolCalls || [];
|
|
96
|
+
|
|
97
|
+
if (requestedToolCalls.length > 0) {
|
|
98
|
+
// Record assistant tool request in session history
|
|
99
|
+
await memory.append({
|
|
100
|
+
role: "assistant",
|
|
101
|
+
content: response.text || "",
|
|
102
|
+
reasoningContent: thought,
|
|
103
|
+
toolCalls: requestedToolCalls,
|
|
104
|
+
timestamp: stepStartTime
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const stepResults: ToolResult[] = [];
|
|
108
|
+
|
|
109
|
+
for (const tc of requestedToolCalls) {
|
|
110
|
+
const tool = toolMap.get(tc.name);
|
|
111
|
+
let resultStr: string;
|
|
112
|
+
|
|
113
|
+
if (!tool) {
|
|
114
|
+
resultStr = `Error: Tool '${tc.name}' not found. Available tools: ${Array.from(toolMap.keys()).join(", ")}`;
|
|
115
|
+
} else {
|
|
116
|
+
try {
|
|
117
|
+
const res = await tool.execute(tc.arguments, context);
|
|
118
|
+
resultStr = typeof res === "string" ? res : JSON.stringify(res);
|
|
119
|
+
} catch (err: any) {
|
|
120
|
+
resultStr = `Error executing tool '${tc.name}': ${err.message}`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
stepResults.push({
|
|
125
|
+
toolCallId: tc.id,
|
|
126
|
+
toolName: tc.name,
|
|
127
|
+
result: resultStr
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Append tool observation to memory for subsequent turns
|
|
131
|
+
await memory.append({
|
|
132
|
+
role: "tool",
|
|
133
|
+
name: tc.name,
|
|
134
|
+
toolCallId: tc.id,
|
|
135
|
+
content: resultStr,
|
|
136
|
+
timestamp: Date.now()
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
steps.push({
|
|
141
|
+
step: currentStep,
|
|
142
|
+
thought,
|
|
143
|
+
toolCalls: requestedToolCalls,
|
|
144
|
+
toolResults: stepResults,
|
|
145
|
+
timestamp: stepStartTime
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
currentStep++;
|
|
149
|
+
} else {
|
|
150
|
+
// No tool calls - LLM produced final text answer
|
|
151
|
+
finalOutput = response.text;
|
|
152
|
+
finishReason = "stop";
|
|
153
|
+
|
|
154
|
+
steps.push({
|
|
155
|
+
step: currentStep,
|
|
156
|
+
thought,
|
|
157
|
+
output: finalOutput,
|
|
158
|
+
timestamp: stepStartTime
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Append final assistant response to memory
|
|
162
|
+
await memory.append({
|
|
163
|
+
role: "assistant",
|
|
164
|
+
content: finalOutput,
|
|
165
|
+
reasoningContent: thought,
|
|
166
|
+
timestamp: Date.now()
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
text: finalOutput,
|
|
175
|
+
thought: finalThought,
|
|
176
|
+
steps,
|
|
177
|
+
totalSteps: steps.length,
|
|
178
|
+
model: usedModel,
|
|
179
|
+
durationMs: Date.now() - startTime,
|
|
180
|
+
finishReason
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface ToolCall {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
arguments: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ToolResult {
|
|
8
|
+
toolCallId: string;
|
|
9
|
+
toolName: string;
|
|
10
|
+
result: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AgentTool {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
parameters?: Record<string, any>;
|
|
17
|
+
execute: (args: Record<string, any>, context?: AgentContext) => Promise<string | Record<string, any>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AgentContext {
|
|
21
|
+
agentName: string;
|
|
22
|
+
agentVersion: string;
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
state: Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AgentStep {
|
|
28
|
+
step: number;
|
|
29
|
+
thought?: string;
|
|
30
|
+
toolCalls?: ToolCall[];
|
|
31
|
+
toolResults?: ToolResult[];
|
|
32
|
+
output?: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AgentRunResult {
|
|
37
|
+
text: string;
|
|
38
|
+
thought?: string;
|
|
39
|
+
steps: AgentStep[];
|
|
40
|
+
totalSteps: number;
|
|
41
|
+
model: string;
|
|
42
|
+
durationMs: number;
|
|
43
|
+
finishReason: "stop" | "max_steps_reached" | "error";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface AgentExecutionOptions {
|
|
47
|
+
model?: string;
|
|
48
|
+
maxTokens?: number;
|
|
49
|
+
maxSteps?: number;
|
|
50
|
+
systemPrompt?: string;
|
|
51
|
+
temperature?: number;
|
|
52
|
+
tools?: AgentTool[];
|
|
53
|
+
}
|