matimo-examples 0.1.0-alpha.11
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/.env.example +49 -0
- package/LICENSE +21 -0
- package/README.md +525 -0
- package/agents/decorator-pattern-agent.ts +368 -0
- package/agents/factory-pattern-agent.ts +253 -0
- package/agents/langchain-agent.ts +146 -0
- package/edit/edit-decorator.ts +178 -0
- package/edit/edit-factory.ts +138 -0
- package/edit/edit-langchain.ts +292 -0
- package/execute/execute-decorator.ts +49 -0
- package/execute/execute-factory.ts +46 -0
- package/execute/execute-langchain.ts +232 -0
- package/github/github-decorator.ts +326 -0
- package/github/github-factory.ts +355 -0
- package/github/github-langchain.ts +206 -0
- package/github/github-with-approval.ts +228 -0
- package/gmail/README.md +345 -0
- package/gmail/gmail-decorator.ts +216 -0
- package/gmail/gmail-factory.ts +231 -0
- package/gmail/gmail-langchain.ts +201 -0
- package/hubspot/README.md +316 -0
- package/hubspot/hubspot-decorator.ts +180 -0
- package/hubspot/hubspot-factory.ts +188 -0
- package/hubspot/hubspot-langchain.ts +222 -0
- package/logger-example.ts +40 -0
- package/mailchimp/README.md +321 -0
- package/mailchimp/mailchimp-decorator.ts +277 -0
- package/mailchimp/mailchimp-factory.ts +187 -0
- package/mailchimp/mailchimp-langchain.ts +155 -0
- package/notion/README.md +293 -0
- package/notion/notion-decorator.ts +275 -0
- package/notion/notion-factory.ts +256 -0
- package/notion/notion-langchain.ts +237 -0
- package/package.json +79 -0
- package/postgres/README.md +188 -0
- package/postgres/postgres-decorator.ts +198 -0
- package/postgres/postgres-factory.ts +180 -0
- package/postgres/postgres-langchain.ts +213 -0
- package/postgres/postgres-with-approval.ts +344 -0
- package/read/read-decorator.ts +154 -0
- package/read/read-factory.ts +121 -0
- package/read/read-langchain.ts +273 -0
- package/search/search-decorator.ts +206 -0
- package/search/search-factory.ts +146 -0
- package/search/search-langchain.ts +255 -0
- package/slack/README.md +339 -0
- package/slack/slack-decorator.ts +245 -0
- package/slack/slack-factory.ts +226 -0
- package/slack/slack-langchain.ts +242 -0
- package/tsconfig.json +20 -0
- package/twilio/README.md +309 -0
- package/twilio/twilio-decorator.ts +288 -0
- package/twilio/twilio-factory.ts +238 -0
- package/twilio/twilio-langchain.ts +218 -0
- package/web/web-decorator.ts +52 -0
- package/web/web-factory.ts +70 -0
- package/web/web-langchain.ts +163 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* WEB TOOL - LANGCHAIN AI AGENT EXAMPLE
|
|
5
|
+
* ============================================================================
|
|
6
|
+
*
|
|
7
|
+
* PATTERN: True AI Agent with OpenAI + LangChain
|
|
8
|
+
* ─────────────────────────────────────────────────────────────────────────
|
|
9
|
+
* This is a REAL AI agent that:
|
|
10
|
+
* 1. Takes natural language user requests
|
|
11
|
+
* 2. Uses OpenAI LLM (GPT-4o-mini) to decide when to make HTTP requests
|
|
12
|
+
* 3. Generates appropriate URLs and methods based on context
|
|
13
|
+
* 4. Executes tools autonomously
|
|
14
|
+
* 5. Processes results and responds naturally
|
|
15
|
+
*
|
|
16
|
+
* SETUP:
|
|
17
|
+
* ─────────────────────────────────────────────────────────────────────────
|
|
18
|
+
* 1. Create .env file:
|
|
19
|
+
* OPENAI_API_KEY=sk-xxxxxxxxxxxxx
|
|
20
|
+
*
|
|
21
|
+
* 2. Install dependencies:
|
|
22
|
+
* npm install
|
|
23
|
+
*
|
|
24
|
+
* USAGE:
|
|
25
|
+
* ─────────────────────────────────────────────────────────────────────────
|
|
26
|
+
* export OPENAI_API_KEY=sk-xxxx
|
|
27
|
+
* npm run web:langchain
|
|
28
|
+
*
|
|
29
|
+
* ============================================================================
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import 'dotenv/config';
|
|
33
|
+
import { createAgent } from 'langchain';
|
|
34
|
+
import { ChatOpenAI } from '@langchain/openai';
|
|
35
|
+
import { MatimoInstance, convertToolsToLangChain, type ToolDefinition } from '@matimo/core';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Run AI Agent with Web tool
|
|
39
|
+
* The agent receives natural language requests and decides when to make HTTP requests
|
|
40
|
+
*/
|
|
41
|
+
async function runWebAIAgent() {
|
|
42
|
+
console.info('\n╔════════════════════════════════════════════════════════╗');
|
|
43
|
+
console.info('║ Web Tool AI Agent - LangChain + OpenAI ║');
|
|
44
|
+
console.info('║ True autonomous agent with LLM reasoning ║');
|
|
45
|
+
console.info('╚════════════════════════════════════════════════════════╝\n');
|
|
46
|
+
|
|
47
|
+
// Check required environment variables
|
|
48
|
+
const openaiKey = process.env.OPENAI_API_KEY;
|
|
49
|
+
if (!openaiKey) {
|
|
50
|
+
console.error('❌ Error: OPENAI_API_KEY not set in .env');
|
|
51
|
+
console.info(' Set it: export OPENAI_API_KEY="sk-..."');
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.info('🤖 Using OpenAI (GPT-4o-mini) as the AI agent\n');
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
// Initialize Matimo with auto-discovery
|
|
59
|
+
console.info('🚀 Initializing Matimo...');
|
|
60
|
+
const matimo = await MatimoInstance.init({ autoDiscover: true });
|
|
61
|
+
|
|
62
|
+
// Get web tool
|
|
63
|
+
console.info('💬 Loading web tool...');
|
|
64
|
+
const matimoTools = matimo.listTools();
|
|
65
|
+
const webTool = matimoTools.filter((t) => t.name === 'web');
|
|
66
|
+
console.info(`✅ Loaded ${webTool.length} web tool(s)\n`);
|
|
67
|
+
|
|
68
|
+
if (webTool.length === 0) {
|
|
69
|
+
console.error('❌ Web tool not found');
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Convert to LangChain tools using the built-in converter
|
|
74
|
+
const webTools = webTool as ToolDefinition[];
|
|
75
|
+
const langchainTools = await convertToolsToLangChain(webTools, matimo);
|
|
76
|
+
|
|
77
|
+
// Initialize OpenAI LLM
|
|
78
|
+
console.info('🤖 Initializing OpenAI (GPT-4o-mini) LLM...');
|
|
79
|
+
const model = new ChatOpenAI({
|
|
80
|
+
modelName: 'gpt-4o-mini',
|
|
81
|
+
temperature: 0.7,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Create agent
|
|
85
|
+
console.info('🔧 Creating agent...\n');
|
|
86
|
+
const agent = await createAgent({
|
|
87
|
+
model,
|
|
88
|
+
tools: langchainTools as any,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Define agent tasks (natural language requests)
|
|
92
|
+
const userRequests = [
|
|
93
|
+
{
|
|
94
|
+
title: 'Example 1: Fetch GitHub repository info',
|
|
95
|
+
request: 'Get information about the tallclub/matimo repository from GitHub API',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
title: 'Example 2: Check API status',
|
|
99
|
+
request: 'Make a request to the GitHub API and tell me what HTTP status code we get',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
title: 'Example 3: Fetch public data',
|
|
103
|
+
request: 'Get JSON data from a public REST API endpoint and summarize the response',
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
console.info('🧪 Running AI Agent Tasks');
|
|
108
|
+
console.info('═'.repeat(60) + '\n');
|
|
109
|
+
|
|
110
|
+
// Run each task through the agent
|
|
111
|
+
for (const task of userRequests) {
|
|
112
|
+
console.info(`${task.title}`);
|
|
113
|
+
console.info('─'.repeat(60));
|
|
114
|
+
console.info(`👤 User: "${task.request}"\n`);
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const response = await agent.invoke({
|
|
118
|
+
messages: [
|
|
119
|
+
{
|
|
120
|
+
role: 'user',
|
|
121
|
+
content: task.request,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Get the last message from the agent
|
|
127
|
+
const lastMessage = response.messages[response.messages.length - 1];
|
|
128
|
+
if (lastMessage) {
|
|
129
|
+
const content =
|
|
130
|
+
typeof lastMessage.content === 'string'
|
|
131
|
+
? lastMessage.content
|
|
132
|
+
: String(lastMessage.content);
|
|
133
|
+
|
|
134
|
+
if (content && content.trim()) {
|
|
135
|
+
console.info(`🤖 Agent: ${content}\n`);
|
|
136
|
+
} else {
|
|
137
|
+
console.info('🤖 Agent: (Request completed successfully)\n');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} catch (error) {
|
|
141
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
142
|
+
console.info(`⚠️ Agent error: ${errorMsg}\n`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
console.info('═'.repeat(60));
|
|
147
|
+
console.info('\n✨ AI Agent Examples Complete!\n');
|
|
148
|
+
console.info('Key Features:');
|
|
149
|
+
console.info(' ✅ Real LLM (OpenAI) decides which tools to use');
|
|
150
|
+
console.info(' ✅ Natural language requests, not API calls');
|
|
151
|
+
console.info(' ✅ LLM generates URLs and HTTP methods based on context');
|
|
152
|
+
console.info(' ✅ Agentic reasoning and decision-making\n');
|
|
153
|
+
} catch (error) {
|
|
154
|
+
console.error('❌ Error:', error instanceof Error ? error.message : String(error));
|
|
155
|
+
if (error instanceof Error && error.stack) {
|
|
156
|
+
console.error('Stack:', error.stack);
|
|
157
|
+
}
|
|
158
|
+
process.exit(1);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Run the AI agent
|
|
163
|
+
runWebAIAgent().catch(console.error);
|