openkbs 0.0.52 → 0.0.55

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.
Files changed (28) hide show
  1. package/README.md +1496 -192
  2. package/package.json +2 -1
  3. package/src/actions.js +407 -13
  4. package/src/index.js +38 -2
  5. package/src/utils.js +1 -1
  6. package/templates/.openkbs/knowledge/examples/ai-copywriter-agent/app/instructions.txt +44 -9
  7. package/templates/.openkbs/knowledge/examples/ai-copywriter-agent/src/Events/actions.js +43 -42
  8. package/templates/.openkbs/knowledge/examples/ai-copywriter-agent/src/Events/handler.js +14 -8
  9. package/templates/.openkbs/knowledge/examples/ai-copywriter-agent/src/Frontend/contentRender.js +95 -12
  10. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/README.md +64 -0
  11. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/app/instructions.txt +160 -0
  12. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/app/settings.json +7 -0
  13. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Events/actions.js +258 -0
  14. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Events/onRequest.js +13 -0
  15. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Events/onRequest.json +3 -0
  16. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Events/onResponse.js +13 -0
  17. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Events/onResponse.json +3 -0
  18. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Frontend/contentRender.js +170 -0
  19. package/templates/.openkbs/knowledge/examples/ai-marketing-agent/src/Frontend/contentRender.json +3 -0
  20. package/templates/.openkbs/knowledge/metadata.json +1 -1
  21. package/templates/CLAUDE.md +593 -222
  22. package/templates/app/instructions.txt +13 -1
  23. package/templates/app/settings.json +5 -6
  24. package/templates/src/Events/actions.js +43 -9
  25. package/templates/src/Events/handler.js +24 -25
  26. package/templates/webpack.contentRender.config.js +8 -2
  27. package/version.json +3 -3
  28. package/MODIFY.md +0 -132
@@ -15,7 +15,7 @@ const extractJSONFromText = (text) => {
15
15
  return null;
16
16
  }
17
17
 
18
- export const getActions = () => [
18
+ export const getActions = (meta) => [
19
19
  // IMPORTANT: Actions returning JOB_COMPLETED or JOB_FAILED stop agent execution and return final result
20
20
  [/[\s\S]*"type"\s*:\s*"JOB_COMPLETED"[\s\S]*/, async (match, event) => {
21
21
  const parsedData = extractJSONFromText(match[0]);
@@ -31,7 +31,6 @@ export const getActions = () => [
31
31
  }
32
32
  }],
33
33
 
34
-
35
34
  [/[\s\S]*"type"\s*:\s*"JOB_FAILED"[\s\S]*/, async (match, event) => {
36
35
  const parsedData = extractJSONFromText(match[0]);
37
36
  if (parsedData && parsedData.type === "JOB_FAILED") {
@@ -45,24 +44,26 @@ export const getActions = () => [
45
44
  }
46
45
  }],
47
46
 
48
- [/\/?googleSearch\("(.*?)"\)/, async (match) => {
49
- const q = match[1];
47
+ // Google Search with XML+JSON format
48
+ [/<googleSearch>([\s\S]*?)<\/googleSearch>/s, async (match) => {
50
49
  try {
51
- const response = await openkbs.googleSearch(q, {});
52
- const data = response?.map(({ title, link, snippet, pagemap }) => ({
50
+ const data = JSON.parse(match[1].trim());
51
+ const response = await openkbs.googleSearch(data.query);
52
+ const results = response?.map(({ title, link, snippet, pagemap }) => ({
53
53
  title, link, snippet, image: pagemap?.metatags?.[0]?.["og:image"]
54
54
  }));
55
- return { data };
55
+ return { data: results, ...meta };
56
56
  } catch (e) {
57
- return { error: e.message };
57
+ return { error: e.message, ...meta };
58
58
  }
59
59
  }],
60
60
 
61
- [/\/?youtubeSearch\("(.*?)"\)/, async (match) => {
62
- const q = match[1];
61
+ // YouTube Search with XML+JSON format
62
+ [/<youtubeSearch>([\s\S]*?)<\/youtubeSearch>/s, async (match) => {
63
63
  try {
64
- const response = await openkbs.googleSearch(q + ' site:youtube.com', { videoOnly: true });
65
- const data = response?.map(({ title, link, snippet, pagemap }) => ({
64
+ const data = JSON.parse(match[1].trim());
65
+ const response = await openkbs.googleSearch(data.query + ' site:youtube.com', { videoOnly: true });
66
+ const results = response?.map(({ title, link, snippet, pagemap }) => ({
66
67
  title,
67
68
  link: link.replace('www.youtube.com/watch?v=', 'youtu.be/'),
68
69
  snippet,
@@ -70,62 +71,62 @@ export const getActions = () => [
70
71
  duration: pagemap?.videoobject?.[0]?.duration,
71
72
  channel: pagemap?.metatags?.[0]?.["og:site_name"],
72
73
  })).filter(item => item.link.includes('youtu'));
73
- return { data };
74
+ return { data: results, ...meta };
74
75
  } catch (e) {
75
- return { error: e.message };
76
+ return { error: e.message, ...meta };
76
77
  }
77
78
  }],
78
79
 
79
- [/\/?googleImageSearch\("(.*?)"\)/, async (match) => {
80
- const q = match[1];
80
+ // Google Image Search with XML+JSON format
81
+ [/<googleImageSearch>([\s\S]*?)<\/googleImageSearch>/s, async (match) => {
81
82
  try {
82
- const response = await openkbs.googleSearch(q, { searchType: 'image' });
83
- const data = response?.map(({ title, link, snippet, pagemap }) => {
83
+ const data = JSON.parse(match[1].trim());
84
+ const response = await openkbs.googleSearch(data.query, { searchType: 'image' });
85
+ const results = response?.map(({ title, link, snippet, pagemap }) => {
84
86
  const imageObj = pagemap?.cse_image?.[0];
85
87
  const thumbnail = imageObj?.src || pagemap?.metatags?.[0]?.["og:image"] || link;
86
- return {
87
- title,
88
- link: link,
89
- snippet,
90
- image: thumbnail
91
- };
88
+ return { title, link, snippet, image: thumbnail };
92
89
  });
93
- return { data };
90
+ return { data: results, ...meta };
94
91
  } catch (e) {
95
- return { error: e.message };
92
+ return { error: e.message, ...meta };
96
93
  }
97
94
  }],
98
95
 
99
- [/\/?webpageToText\("(.*)"\)/, async (match) => {
96
+ // Webpage to Text with XML+JSON format
97
+ [/<webpageToText>([\s\S]*?)<\/webpageToText>/s, async (match) => {
100
98
  try {
101
- let response = await openkbs.webpageToText(match[1]);
102
- if(!response?.url) return { data: { error: "Unable to read website" } };
103
- return { data: response };
99
+ const data = JSON.parse(match[1].trim());
100
+ let response = await openkbs.webpageToText(data.url);
101
+ if (!response?.url) return { data: { error: "Unable to read website" }, ...meta };
102
+ return { data: response, ...meta };
104
103
  } catch (e) {
105
- return { error: e.response?.data || e };
104
+ return { error: e.response?.data || e.message, ...meta };
106
105
  }
107
106
  }],
108
107
 
109
- [/\/?documentToText\("(.*)"\)/, async (match) => {
108
+ // Document to Text with XML+JSON format
109
+ [/<documentToText>([\s\S]*?)<\/documentToText>/s, async (match) => {
110
110
  try {
111
- let response = await openkbs.documentToText(match[1]);
112
- return { data: response };
111
+ const data = JSON.parse(match[1].trim());
112
+ let response = await openkbs.documentToText(data.url);
113
+ return { data: response, ...meta };
113
114
  } catch (e) {
114
- return { error: e.response.data };
115
+ return { error: e.response?.data || e.message, ...meta };
115
116
  }
116
117
  }],
117
118
 
118
- [/\/?imageToText\("(.*)"\)/, async (match) => {
119
+ // Image to Text (OCR) with XML+JSON format
120
+ [/<imageToText>([\s\S]*?)<\/imageToText>/s, async (match) => {
119
121
  try {
120
- let response = await openkbs.imageToText(match[1]);
121
-
122
+ const data = JSON.parse(match[1].trim());
123
+ let response = await openkbs.imageToText(data.url);
122
124
  if (response?.detections?.[0]?.txt) {
123
125
  response = { detections: response?.detections?.[0]?.txt };
124
126
  }
125
-
126
- return { data: response };
127
+ return { data: response, ...meta };
127
128
  } catch (e) {
128
- return { error: e.response.data };
129
+ return { error: e.response?.data || e.message, ...meta };
129
130
  }
130
131
  }],
131
- ];
132
+ ];
@@ -2,7 +2,14 @@ import {getActions} from './actions.js';
2
2
 
3
3
  export const backendHandler = async (event) => {
4
4
  const lastMessage = event.payload.messages[event.payload.messages.length - 1];
5
- const actions = getActions();
5
+ const reachedMessageLimit = event?.payload?.messages?.length > 60;
6
+
7
+ // Meta for continuing chat model requests
8
+ const meta = {
9
+ _meta_actions: reachedMessageLimit ? [] : ["REQUEST_CHAT_MODEL"]
10
+ };
11
+
12
+ const actions = getActions(meta);
6
13
 
7
14
  const matchingActions = actions.reduce((acc, [regex, action]) => {
8
15
  const matches = [...lastMessage.content.matchAll(new RegExp(regex, 'g'))];
@@ -12,31 +19,30 @@ export const backendHandler = async (event) => {
12
19
  return acc;
13
20
  }, []);
14
21
 
15
- const reachedMessageLimit = event?.payload?.messages?.length > 60;
16
-
17
22
  if (matchingActions.length > 0) {
18
23
  try {
19
24
  const results = await Promise.all(matchingActions);
20
25
 
21
26
  // IMPORTANT: Actions returning JOB_COMPLETED or JOB_FAILED stop agent execution and return final result
22
- const isOnlyJobCompletion = results.length === 1 &&
27
+ const isOnlyJobCompletion = results.length === 1 &&
23
28
  (results[0]?.type === 'JOB_COMPLETED' || results[0]?.type === 'JOB_FAILED');
24
-
25
- const meta = {
29
+
30
+ // Override meta for job completion
31
+ const finalMeta = {
26
32
  _meta_actions: (reachedMessageLimit || isOnlyJobCompletion) ? [] : ["REQUEST_CHAT_MODEL"]
27
33
  };
28
34
 
29
35
  if (results?.[0]?.data?.some?.(o => o?.type === 'image_url')) {
30
36
  return {
31
37
  ...results[0],
32
- ...meta
38
+ ...finalMeta
33
39
  };
34
40
  }
35
41
 
36
42
  return {
37
43
  type: 'RESPONSE',
38
44
  data: results,
39
- ...meta
45
+ ...finalMeta
40
46
  };
41
47
  } catch (error) {
42
48
  return {
@@ -1,6 +1,11 @@
1
1
  import React, { useEffect } from "react";
2
2
  import JsonView from '@uiw/react-json-view';
3
- import { Chip } from '@mui/material';
3
+ import { Box, Tooltip, Chip } from '@mui/material';
4
+ import SearchIcon from '@mui/icons-material/Search';
5
+ import LanguageIcon from '@mui/icons-material/Language';
6
+ import ImageIcon from '@mui/icons-material/Image';
7
+ import YouTubeIcon from '@mui/icons-material/YouTube';
8
+ import ArticleIcon from '@mui/icons-material/Article';
4
9
 
5
10
  const extractJSONFromText = (text) => {
6
11
  let braceCount = 0, startIndex = text.indexOf('{');
@@ -19,24 +24,102 @@ const extractJSONFromText = (text) => {
19
24
  return null;
20
25
  }
21
26
 
22
- const parseCommands = (text) => {
23
- return text.match(/\/\w+\("([^"]+)"\)/g) || [];
24
- }
27
+ // Command patterns for XML+JSON format
28
+ const COMMAND_PATTERNS = [
29
+ /<googleSearch>[\s\S]*?<\/googleSearch>/,
30
+ /<youtubeSearch>[\s\S]*?<\/youtubeSearch>/,
31
+ /<googleImageSearch>[\s\S]*?<\/googleImageSearch>/,
32
+ /<webpageToText>[\s\S]*?<\/webpageToText>/,
33
+ /<documentToText>[\s\S]*?<\/documentToText>/,
34
+ /<imageToText>[\s\S]*?<\/imageToText>/
35
+ ];
36
+
37
+ // Icon mapping for commands
38
+ const commandIcons = {
39
+ googleSearch: SearchIcon,
40
+ youtubeSearch: YouTubeIcon,
41
+ googleImageSearch: ImageIcon,
42
+ webpageToText: LanguageIcon,
43
+ documentToText: ArticleIcon,
44
+ imageToText: ImageIcon
45
+ };
46
+
47
+ // Parse commands from content
48
+ const parseCommands = (content) => {
49
+ const commands = [];
50
+ const regex = /<(\w+)>([\s\S]*?)<\/\1>/g;
51
+ let match;
52
+ while ((match = regex.exec(content)) !== null) {
53
+ try {
54
+ commands.push({
55
+ name: match[1],
56
+ data: JSON.parse(match[2].trim())
57
+ });
58
+ } catch (e) {
59
+ commands.push({ name: match[1], data: match[2].trim() });
60
+ }
61
+ }
62
+ return commands;
63
+ };
64
+
65
+ // Render command as icon with tooltip
66
+ const CommandIcon = ({ command }) => {
67
+ const Icon = commandIcons[command.name] || SearchIcon;
68
+ return (
69
+ <Tooltip
70
+ title={
71
+ <Box sx={{ p: 1 }}>
72
+ <Box sx={{ fontWeight: 'bold', color: '#4CAF50', mb: 0.5 }}>{command.name}</Box>
73
+ <pre style={{ margin: 0, fontSize: '10px' }}>
74
+ {typeof command.data === 'object'
75
+ ? JSON.stringify(command.data, null, 2)
76
+ : command.data}
77
+ </pre>
78
+ </Box>
79
+ }
80
+ arrow
81
+ >
82
+ <Box sx={{
83
+ display: 'inline-flex',
84
+ width: 32, height: 32,
85
+ borderRadius: '50%',
86
+ backgroundColor: 'rgba(76, 175, 80, 0.1)',
87
+ border: '2px solid rgba(76, 175, 80, 0.3)',
88
+ alignItems: 'center',
89
+ justifyContent: 'center',
90
+ mx: 0.5,
91
+ cursor: 'pointer',
92
+ '&:hover': {
93
+ backgroundColor: 'rgba(76, 175, 80, 0.2)',
94
+ transform: 'scale(1.1)'
95
+ },
96
+ transition: 'all 0.2s'
97
+ }}>
98
+ <Icon sx={{ fontSize: 16, color: '#4CAF50' }} />
99
+ </Box>
100
+ </Tooltip>
101
+ );
102
+ };
25
103
 
26
104
  // do NOT useState() directly in this function, it is not a React component
27
105
  const onRenderChatMessage = async (params) => {
28
106
  const { content } = params.messages[params.msgIndex];
29
107
  const JSONData = extractJSONFromText(content);
30
- const commands = parseCommands(content);
31
-
108
+
109
+ // Render JOB_COMPLETED as JSON view
32
110
  if (JSONData?.type === 'JOB_COMPLETED') {
33
111
  return <JsonView value={JSONData} />
34
112
  }
35
-
36
- if (commands.length > 0) {
37
- return commands.map((cmd, i) => (
38
- <Chip key={i} label={cmd} size="small" />
39
- ));
113
+
114
+ // Check for commands in content
115
+ const hasCommand = COMMAND_PATTERNS.some(p => p.test(content));
116
+ if (hasCommand) {
117
+ const commands = parseCommands(content);
118
+ return (
119
+ <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
120
+ {commands.map((cmd, i) => <CommandIcon key={i} command={cmd} />)}
121
+ </Box>
122
+ );
40
123
  }
41
124
 
42
125
  // return undefined to use default message render
@@ -61,4 +144,4 @@ const Header = ({ setRenderSettings }) => {
61
144
 
62
145
  const exports = { onRenderChatMessage, Header };
63
146
  window.contentRender = exports;
64
- export default exports;
147
+ export default exports;
@@ -0,0 +1,64 @@
1
+ # AI Marketing Agent
2
+
3
+ A comprehensive AI marketing assistant that can create content, generate images and videos, search the web, send emails, and schedule tasks.
4
+
5
+ ## Features
6
+
7
+ - **AI Image Generation**: Gemini 2.5 Flash Image and GPT-Image-1 models
8
+ - **AI Video Generation**: Sora 2 and Sora 2 Pro models
9
+ - **Web Search**: Google Search and Image Search
10
+ - **Email Marketing**: Send emails directly from chat
11
+ - **Task Scheduling**: Schedule future reminders and tasks
12
+ - **Web Publishing**: Create and publish HTML landing pages
13
+ - **Memory System**: Persistent storage for user preferences and content
14
+
15
+ ## Command Format
16
+
17
+ All commands use XML tags with JSON content:
18
+
19
+ ```xml
20
+ <commandName>
21
+ {
22
+ "param1": "value1",
23
+ "param2": "value2"
24
+ }
25
+ </commandName>
26
+ ```
27
+
28
+ ## Available Commands
29
+
30
+ ### Content Creation
31
+ - `<createAIImage>` - Generate images with AI
32
+ - `<createAIVideo>` - Generate videos with Sora 2
33
+ - `<publishWebPage>` - Publish HTML landing pages
34
+
35
+ ### Search & Research
36
+ - `<googleSearch>` - Web search
37
+ - `<googleImageSearch>` - Image search
38
+ - `<webpageToText>` - Extract text from webpages
39
+ - `<viewImage>` - View image in context
40
+
41
+ ### Communication
42
+ - `<sendMail>` - Send emails
43
+
44
+ ### Task Management
45
+ - `<scheduleTask>` - Schedule future tasks
46
+ - `<getScheduledTasks/>` - List scheduled tasks
47
+
48
+ ### Memory
49
+ - `<setMemory>` - Save to memory
50
+ - `<deleteItem>` - Delete from memory
51
+
52
+ ## Architecture
53
+
54
+ - `src/Events/actions.js` - Command implementations
55
+ - `src/Events/onRequest.js` - User message handler
56
+ - `src/Events/onResponse.js` - LLM response handler
57
+ - `src/Frontend/contentRender.js` - UI customization
58
+ - `app/instructions.txt` - LLM instructions
59
+
60
+ ## Deployment
61
+
62
+ ```bash
63
+ openkbs push
64
+ ```
@@ -0,0 +1,160 @@
1
+ You are an AI Marketing Assistant that helps users create content, images, videos, and marketing materials.
2
+
3
+ ## Current Time
4
+ - UTC: {{openkbsDateNow}}
5
+ - Local: {{openkbsDate:en-US:UTC}}
6
+
7
+ ## Core Capabilities
8
+ 1. **AI Image Generation** - Create images with Gemini or GPT-Image
9
+ 2. **AI Video Generation** - Create videos with Sora 2
10
+ 3. **Web Search** - Search Google for information and images
11
+ 4. **Email Marketing** - Send marketing emails
12
+ 5. **Task Scheduling** - Schedule future reminders and tasks
13
+ 6. **Web Publishing** - Create and publish landing pages
14
+ 7. **Memory** - Remember user preferences and content ideas
15
+
16
+ ## AVAILABLE COMMANDS
17
+ To execute a command, output it and wait for the system response.
18
+
19
+ <setMemory>
20
+ {
21
+ "itemId": "memory_key_name",
22
+ "value": "any value or object",
23
+ "expirationInMinutes": 1440
24
+ }
25
+ </setMemory>
26
+ Description: """
27
+ Save information to memory. itemId must start with 'memory_'. Optional expiration.
28
+ """
29
+
30
+ <deleteItem>
31
+ {
32
+ "itemId": "memory_key_name"
33
+ }
34
+ </deleteItem>
35
+ Description: """
36
+ Delete a memory item by its itemId.
37
+ """
38
+
39
+ <createAIImage>
40
+ {
41
+ "model": "gemini-2.5-flash-image",
42
+ "aspect_ratio": "16:9",
43
+ "prompt": "detailed image description"
44
+ }
45
+ </createAIImage>
46
+ Description: """
47
+ Generate AI images.
48
+ - gemini-2.5-flash-image: aspect_ratio (1:1, 16:9, 9:16, 3:2, 4:3, etc.), supports imageUrls for editing
49
+ - gpt-image-1: size (1024x1024, 1536x1024, 1024x1536), better for text in images
50
+ """
51
+
52
+ <createAIVideo>
53
+ {
54
+ "model": "sora-2",
55
+ "size": "1280x720",
56
+ "seconds": 8,
57
+ "prompt": "detailed video description"
58
+ }
59
+ </createAIVideo>
60
+ Description: """
61
+ Generate AI videos with Sora 2.
62
+ - model: sora-2 (fast) or sora-2-pro (higher quality)
63
+ - size: 1280x720 (landscape) or 720x1280 (portrait)
64
+ - seconds: 4, 8, or 12
65
+ - input_reference_url: optional reference image
66
+ """
67
+
68
+ <continueVideoPolling>
69
+ {
70
+ "videoId": "video_id_here"
71
+ }
72
+ </continueVideoPolling>
73
+ Description: """
74
+ Check status of pending video generation.
75
+ """
76
+
77
+ <viewImage>
78
+ {
79
+ "url": "https://example.com/image.jpg"
80
+ }
81
+ </viewImage>
82
+ Description: """
83
+ Add an image to the conversation for visual analysis.
84
+ """
85
+
86
+ <googleSearch>
87
+ {
88
+ "query": "search query"
89
+ }
90
+ </googleSearch>
91
+ Description: """
92
+ Search Google for information.
93
+ """
94
+
95
+ <googleImageSearch>
96
+ {
97
+ "query": "search query",
98
+ "limit": 5
99
+ }
100
+ </googleImageSearch>
101
+ Description: """
102
+ Search Google for images.
103
+ """
104
+
105
+ <webpageToText>
106
+ {
107
+ "url": "https://example.com/page"
108
+ }
109
+ </webpageToText>
110
+ Description: """
111
+ Extract text content from a webpage.
112
+ """
113
+
114
+ <sendMail>
115
+ {
116
+ "to": "email@example.com",
117
+ "subject": "Subject line",
118
+ "body": "Email body content"
119
+ }
120
+ </sendMail>
121
+ Description: """
122
+ Send an email.
123
+ """
124
+
125
+ <scheduleTask>
126
+ {
127
+ "delay": "2h",
128
+ "message": "Task description"
129
+ }
130
+ </scheduleTask>
131
+ Description: """
132
+ Schedule a future task.
133
+ - delay: minutes (number), "2h" (hours), or "1d" (days)
134
+ - time: specific UTC time "2025-01-15T14:00:00Z"
135
+ Task will trigger a new chat at scheduled time.
136
+ """
137
+
138
+ <getScheduledTasks/>
139
+ Description: """
140
+ List all scheduled tasks.
141
+ """
142
+
143
+ <publishWebPage>
144
+ <!DOCTYPE html>
145
+ <html>
146
+ <head><title>Page Title</title></head>
147
+ <body>
148
+ <h1>Content</h1>
149
+ </body>
150
+ </html>
151
+ </publishWebPage>
152
+ Description: """
153
+ Publish an HTML landing page. Title must be in English for filename generation.
154
+ """
155
+
156
+ ## Guidelines
157
+ - Always confirm actions with the user before executing
158
+ - Save important user information to memory
159
+ - When creating images/videos, provide detailed prompts
160
+ - For scheduled tasks, confirm the time with the user
@@ -0,0 +1,7 @@
1
+ {
2
+ "userId": "public",
3
+ "kbTitle": "AI Marketing Agent",
4
+ "kbDescription": "AI assistant for marketing content creation, image/video generation, and task scheduling",
5
+ "model": "anthropic.claude-3-5-sonnet-20241022-v2:0",
6
+ "inputTools": ["speechToText"]
7
+ }