project-graph-mcp 2.3.0 → 2.3.2
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/package.json +1 -3
- package/project-graph-mcp-2.3.0.tgz +0 -0
- package/src/network/web-server.js +1 -1
- package/vendor/symbiote-node/engine/AgentUICommands.js +100 -0
- package/vendor/symbiote-node/engine/Executor.js +371 -0
- package/vendor/symbiote-node/engine/Graph.js +314 -0
- package/vendor/symbiote-node/engine/GraphServer.js +353 -0
- package/vendor/symbiote-node/engine/HandlerLoader.js +145 -0
- package/vendor/symbiote-node/engine/History.js +83 -0
- package/vendor/symbiote-node/engine/Lifecycle.js +118 -0
- package/vendor/symbiote-node/engine/Persistence.js +84 -0
- package/vendor/symbiote-node/engine/Registry.js +264 -0
- package/vendor/symbiote-node/engine/SocketTypes.js +79 -0
- package/vendor/symbiote-node/engine/cli.js +404 -0
- package/vendor/symbiote-node/engine/index.js +56 -0
- package/vendor/symbiote-node/engine/nanoid.js +28 -0
- package/vendor/symbiote-node/engine/package.json +26 -0
- package/vendor/symbiote-node/engine/packs/ai/beat-detect.handler.js +215 -0
- package/vendor/symbiote-node/engine/packs/ai/content-adapt.handler.js +238 -0
- package/vendor/symbiote-node/engine/packs/ai/face-detect.handler.js +287 -0
- package/vendor/symbiote-node/engine/packs/ai/grok-generate.handler.js +565 -0
- package/vendor/symbiote-node/engine/packs/ai/kling-lipsync.handler.js +414 -0
- package/vendor/symbiote-node/engine/packs/ai/lesson-generate.handler.js +343 -0
- package/vendor/symbiote-node/engine/packs/ai/opencode.handler.js +164 -0
- package/vendor/symbiote-node/engine/packs/ai/replicate-lipsync.handler.js +341 -0
- package/vendor/symbiote-node/engine/packs/ai/tts.handler.js +241 -0
- package/vendor/symbiote-node/engine/packs/ai/whisper.handler.js +191 -0
- package/vendor/symbiote-node/engine/packs/data/db-query.handler.js +67 -0
- package/vendor/symbiote-node/engine/packs/data/news-accumulate.handler.js +281 -0
- package/vendor/symbiote-node/engine/packs/data/personas.handler.js +160 -0
- package/vendor/symbiote-node/engine/packs/data/prompt-loader.handler.js +193 -0
- package/vendor/symbiote-node/engine/packs/data/roles.handler.js +216 -0
- package/vendor/symbiote-node/engine/packs/data/rss-feed.handler.js +244 -0
- package/vendor/symbiote-node/engine/packs/debug/inject.handler.js +52 -0
- package/vendor/symbiote-node/engine/packs/flow/agent.handler.js +73 -0
- package/vendor/symbiote-node/engine/packs/flow/if.handler.js +107 -0
- package/vendor/symbiote-node/engine/packs/flow/loop.handler.js +58 -0
- package/vendor/symbiote-node/engine/packs/flow/merge.handler.js +60 -0
- package/vendor/symbiote-node/engine/packs/flow/retry.handler.js +65 -0
- package/vendor/symbiote-node/engine/packs/flow/switch.handler.js +64 -0
- package/vendor/symbiote-node/engine/packs/flow/wait-all.handler.js +39 -0
- package/vendor/symbiote-node/engine/packs/io/http-request.handler.js +82 -0
- package/vendor/symbiote-node/engine/packs/io/read-file.handler.js +60 -0
- package/vendor/symbiote-node/engine/packs/io/write-file.handler.js +63 -0
- package/vendor/symbiote-node/engine/packs/transform/anchor-match.handler.js +494 -0
- package/vendor/symbiote-node/engine/packs/transform/effects-skeleton.handler.js +417 -0
- package/vendor/symbiote-node/engine/packs/transform/json-parse.handler.js +43 -0
- package/vendor/symbiote-node/engine/packs/transform/lipsync-select.handler.js +339 -0
- package/vendor/symbiote-node/engine/packs/transform/riopla-adapt.handler.js +432 -0
- package/vendor/symbiote-node/engine/packs/transform/set.handler.js +57 -0
- package/vendor/symbiote-node/engine/packs/transform/template-builder.handler.js +134 -0
- package/vendor/symbiote-node/engine/packs/transform/template.handler.js +79 -0
- package/vendor/symbiote-node/engine/packs/transform/timeline-build.handler.js +399 -0
- package/vendor/symbiote-node/engine/packs/util/delay.handler.js +39 -0
- package/vendor/symbiote-node/engine/packs/util/log.handler.js +44 -0
- package/vendor/symbiote-node/engine/packs/video-pack.js +323 -0
- package/vendor/symbiote-node/package.json +2 -2
- package/web/app.js +6 -3
- package/web/components/canvas-graph.js +50 -11
- package/web/components/code-block.js +1 -1
- package/web/components/event-feed/MiniGraphWidget.js +105 -15
- package/web/components/follow-ribbon.js +134 -0
- package/web/follow-controller.js +241 -0
- package/web/panels/code-viewer.js +1 -1
- package/web/panels/dep-graph.js +21 -42
- package/web/style.css +6 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ai/beat-detect — Audio beat detection via librosa (SSH)
|
|
3
|
+
*
|
|
4
|
+
* Analyzes audio files to extract:
|
|
5
|
+
* - Beat timestamps and tempo (BPM)
|
|
6
|
+
* - Waveform peaks (configurable resolution)
|
|
7
|
+
* - Energy contour
|
|
8
|
+
* - Quiet zones (silence detection)
|
|
9
|
+
* - Strong onsets (transient detection)
|
|
10
|
+
*
|
|
11
|
+
* Uses Python librosa library on remote server via SSH.
|
|
12
|
+
* Based on Mr-Computer/modules/ai-music-video beat-detector-ssh.js
|
|
13
|
+
*
|
|
14
|
+
* Remote: mr-agent@mr-agent.rnd-pro.com
|
|
15
|
+
* Script: beat-detection.py (uploaded automatically)
|
|
16
|
+
* Venv: /home/mr-agent/automations/argentine-spanish-bot/venv
|
|
17
|
+
*
|
|
18
|
+
* @module agi-graph/packs/ai/beat-detect
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { execSync } from 'child_process';
|
|
22
|
+
import { promises as fs } from 'fs';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import os from 'os';
|
|
25
|
+
|
|
26
|
+
export default {
|
|
27
|
+
type: 'ai/beat-detect',
|
|
28
|
+
category: 'ai',
|
|
29
|
+
icon: 'graphic_eq',
|
|
30
|
+
|
|
31
|
+
driver: {
|
|
32
|
+
description: 'Audio beat detection via librosa — beats, tempo, peaks, energy',
|
|
33
|
+
inputs: [
|
|
34
|
+
{ name: 'audioPath', type: 'string' },
|
|
35
|
+
],
|
|
36
|
+
outputs: [
|
|
37
|
+
{ name: 'beats', type: 'any' },
|
|
38
|
+
{ name: 'tempo', type: 'number' },
|
|
39
|
+
{ name: 'peaks', type: 'any' },
|
|
40
|
+
{ name: 'energy', type: 'any' },
|
|
41
|
+
{ name: 'quietZones', type: 'any' },
|
|
42
|
+
{ name: 'strongOnsets', type: 'any' },
|
|
43
|
+
{ name: 'duration', type: 'number' },
|
|
44
|
+
{ name: 'error', type: 'string' },
|
|
45
|
+
],
|
|
46
|
+
params: {
|
|
47
|
+
mode: { type: 'string', default: 'ssh', description: 'ssh | http' },
|
|
48
|
+
peaksPerSecond: { type: 'int', default: 10, description: 'Waveform peaks resolution' },
|
|
49
|
+
sampleRate: { type: 'int', default: 22050, description: 'Audio sample rate for analysis' },
|
|
50
|
+
hopLength: { type: 'int', default: 512, description: 'Hop length for beat tracking' },
|
|
51
|
+
// SSH params
|
|
52
|
+
remoteHost: { type: 'string', default: 'mr-agent@mr-agent.rnd-pro.com', description: 'SSH host' },
|
|
53
|
+
remotePath: { type: 'string', default: '/home/mr-agent/automations/argentine-spanish-bot', description: 'Remote project path' },
|
|
54
|
+
remoteVenv: { type: 'string', default: '/home/mr-agent/automations/argentine-spanish-bot/venv', description: 'Remote Python venv' },
|
|
55
|
+
scriptPath: { type: 'string', default: '', description: 'Local path to beat-detection.py (auto-resolved)' },
|
|
56
|
+
// HTTP params
|
|
57
|
+
endpoint: { type: 'string', default: 'http://localhost:5009', description: 'Beat detection HTTP endpoint' },
|
|
58
|
+
timeout: { type: 'int', default: 180000, description: 'Max wait time (ms)' },
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
lifecycle: {
|
|
63
|
+
validate: (inputs) => {
|
|
64
|
+
if (!inputs.audioPath) return false;
|
|
65
|
+
return true;
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
cacheKey: (inputs, params) =>
|
|
69
|
+
`beat:${params.mode}:${inputs.audioPath}:${params.peaksPerSecond}:${params.sampleRate}`,
|
|
70
|
+
|
|
71
|
+
execute: async (inputs, params) => {
|
|
72
|
+
const { audioPath } = inputs;
|
|
73
|
+
const mode = params.mode || 'ssh';
|
|
74
|
+
|
|
75
|
+
if (mode === 'http') {
|
|
76
|
+
return executeHTTP(audioPath, params);
|
|
77
|
+
}
|
|
78
|
+
return executeSSH(audioPath, params);
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** @type {Object} Empty result template */
|
|
84
|
+
const EMPTY = {
|
|
85
|
+
beats: null, tempo: 0, peaks: null, energy: null,
|
|
86
|
+
quietZones: null, strongOnsets: null, duration: 0, error: null,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* SSH mode: upload audio → run librosa beat detection → parse JSON result
|
|
91
|
+
* @param {string} audioPath - Local path to audio file
|
|
92
|
+
* @param {Object} params - Node params
|
|
93
|
+
* @returns {Promise<Object>}
|
|
94
|
+
*/
|
|
95
|
+
async function executeSSH(audioPath, params) {
|
|
96
|
+
const host = params.remoteHost || process.env.WHISPER_REMOTE_HOST || 'mr-agent@mr-agent.rnd-pro.com';
|
|
97
|
+
const remotePath = params.remotePath || process.env.WHISPER_REMOTE_PATH || '/home/mr-agent/automations/argentine-spanish-bot';
|
|
98
|
+
const venv = params.remoteVenv || process.env.WHISPER_REMOTE_VENV || `${remotePath}/venv`;
|
|
99
|
+
const sr = params.sampleRate || parseInt(process.env.BEAT_SAMPLE_RATE, 10) || 22050;
|
|
100
|
+
const hop = params.hopLength || parseInt(process.env.BEAT_HOP_LENGTH, 10) || 512;
|
|
101
|
+
const pps = params.peaksPerSecond || 10;
|
|
102
|
+
const remoteTmpDir = '/tmp/agi-graph-beat';
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
// Verify local file exists
|
|
106
|
+
await fs.access(audioPath);
|
|
107
|
+
|
|
108
|
+
const filename = path.basename(audioPath);
|
|
109
|
+
const remoteAudio = `${remoteTmpDir}/${filename}`;
|
|
110
|
+
|
|
111
|
+
// Setup remote directory
|
|
112
|
+
execSync(`ssh ${host} "mkdir -p ${remoteTmpDir}"`, {
|
|
113
|
+
encoding: 'utf-8', stdio: 'pipe', timeout: 10000,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Upload audio
|
|
117
|
+
execSync(`scp "${audioPath}" "${host}:${remoteAudio}"`, {
|
|
118
|
+
encoding: 'utf-8', stdio: 'pipe', timeout: 60000,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Upload or locate beat detection script
|
|
122
|
+
let remoteScript = `${remoteTmpDir}/beat-detection.py`;
|
|
123
|
+
const localScript = params.scriptPath
|
|
124
|
+
|| path.join(process.cwd(), 'utils/beat-detection.py');
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
await fs.access(localScript);
|
|
128
|
+
execSync(`scp "${localScript}" "${host}:${remoteScript}"`, {
|
|
129
|
+
encoding: 'utf-8', stdio: 'pipe', timeout: 10000,
|
|
130
|
+
});
|
|
131
|
+
} catch {
|
|
132
|
+
// Script might already be on remote, try using module path
|
|
133
|
+
remoteScript = `${remotePath}/utils/beat-detection.py`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
// Run beat detection
|
|
138
|
+
const pythonCmd = `${venv}/bin/python3`;
|
|
139
|
+
const cmd = `"${pythonCmd}" "${remoteScript}" "${remoteAudio}" --sr ${sr} --hop ${hop} --pps ${pps}`;
|
|
140
|
+
const fullCmd = `ssh ${host} '${cmd}'`;
|
|
141
|
+
|
|
142
|
+
const output = execSync(fullCmd, {
|
|
143
|
+
encoding: 'utf-8',
|
|
144
|
+
maxBuffer: 50 * 1024 * 1024,
|
|
145
|
+
timeout: params.timeout || 180000,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const result = JSON.parse(output);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
beats: result.beats,
|
|
152
|
+
tempo: result.tempo,
|
|
153
|
+
peaks: result.peaks,
|
|
154
|
+
energy: result.energy,
|
|
155
|
+
quietZones: result.quiet_zones,
|
|
156
|
+
strongOnsets: result.strong_onsets,
|
|
157
|
+
duration: result.duration,
|
|
158
|
+
error: null,
|
|
159
|
+
};
|
|
160
|
+
} finally {
|
|
161
|
+
// Cleanup remote audio
|
|
162
|
+
execSync(`ssh ${host} "rm -f ${remoteAudio}"`, {
|
|
163
|
+
encoding: 'utf-8', stdio: 'pipe', timeout: 5000,
|
|
164
|
+
}).toString();
|
|
165
|
+
}
|
|
166
|
+
} catch (err) {
|
|
167
|
+
return { ...EMPTY, error: err.message };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* HTTP mode: POST audio to beat detection API
|
|
173
|
+
* @param {string} audioPath - Local path to audio file
|
|
174
|
+
* @param {Object} params - Node params
|
|
175
|
+
* @returns {Promise<Object>}
|
|
176
|
+
*/
|
|
177
|
+
async function executeHTTP(audioPath, params) {
|
|
178
|
+
const endpoint = params.endpoint || 'http://localhost:5009';
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
const audioBuffer = await fs.readFile(audioPath);
|
|
182
|
+
const blob = new Blob([audioBuffer], { type: 'audio/wav' });
|
|
183
|
+
|
|
184
|
+
const formData = new FormData();
|
|
185
|
+
formData.append('file', blob, path.basename(audioPath));
|
|
186
|
+
formData.append('sample_rate', String(params.sampleRate || 22050));
|
|
187
|
+
formData.append('hop_length', String(params.hopLength || 512));
|
|
188
|
+
formData.append('peaks_per_second', String(params.peaksPerSecond || 10));
|
|
189
|
+
|
|
190
|
+
const response = await fetch(`${endpoint}/analyze`, {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
body: formData,
|
|
193
|
+
signal: AbortSignal.timeout(params.timeout || 180000),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
return { ...EMPTY, error: `Beat API error: ${response.status}` };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const result = await response.json();
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
beats: result.beats,
|
|
204
|
+
tempo: result.tempo,
|
|
205
|
+
peaks: result.peaks,
|
|
206
|
+
energy: result.energy,
|
|
207
|
+
quietZones: result.quiet_zones,
|
|
208
|
+
strongOnsets: result.strong_onsets,
|
|
209
|
+
duration: result.duration,
|
|
210
|
+
error: null,
|
|
211
|
+
};
|
|
212
|
+
} catch (err) {
|
|
213
|
+
return { ...EMPTY, error: err.message };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ai/content-adapt — AI Content Adaptation
|
|
3
|
+
*
|
|
4
|
+
* Adapts content to target language levels using AI (OpenRouter).
|
|
5
|
+
* Supports news adaptation, trending topic adaptation, and generic
|
|
6
|
+
* content adaptation with vocabulary extraction and grammar notes.
|
|
7
|
+
*
|
|
8
|
+
* Ported from Mr-Computer/automations/argentine-spanish-bot/src/services/contentAdaptationService.js
|
|
9
|
+
*
|
|
10
|
+
* @module agi-graph/packs/ai/content-adapt
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Simple in-memory cache
|
|
15
|
+
* @type {Map<string, {data: any, timestamp: number}>}
|
|
16
|
+
*/
|
|
17
|
+
const cache = new Map();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Simple hash for caching
|
|
21
|
+
* @param {string} str
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
function hashStr(str) {
|
|
25
|
+
let hash = 0;
|
|
26
|
+
for (let i = 0; i < str.length; i++) {
|
|
27
|
+
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
|
28
|
+
hash |= 0;
|
|
29
|
+
}
|
|
30
|
+
return Math.abs(hash).toString(36);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create adaptation prompt for AI
|
|
35
|
+
* @param {string} content
|
|
36
|
+
* @param {string} contentType
|
|
37
|
+
* @param {string} targetLevel
|
|
38
|
+
* @param {Object} options
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
function createAdaptationPrompt(content, contentType, targetLevel, options) {
|
|
42
|
+
const typeLabels = {
|
|
43
|
+
news: 'a news article',
|
|
44
|
+
trending: 'a trending topic',
|
|
45
|
+
general: 'educational content',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
let prompt = `You are a language adaptation specialist. Adapt the following ${typeLabels[contentType] || 'content'} to ${targetLevel} level Spanish (Rioplatense dialect).\n\n`;
|
|
49
|
+
|
|
50
|
+
prompt += `ORIGINAL CONTENT:\n${content}\n\n`;
|
|
51
|
+
|
|
52
|
+
prompt += `REQUIREMENTS:\n`;
|
|
53
|
+
prompt += `- Adapt vocabulary and grammar to ${targetLevel} level\n`;
|
|
54
|
+
prompt += `- Use Rioplatense Spanish (vos instead of tú, local vocabulary)\n`;
|
|
55
|
+
prompt += `- Keep the essential information\n`;
|
|
56
|
+
prompt += `- Maximum 350-550 characters for the adapted text\n`;
|
|
57
|
+
|
|
58
|
+
if (options.includeVocabulary !== false) {
|
|
59
|
+
prompt += `- Extract 10 key vocabulary items with translations (es→ru)\n`;
|
|
60
|
+
}
|
|
61
|
+
if (options.includeGrammarNotes !== false) {
|
|
62
|
+
prompt += `- Include 1-2 grammar notes relevant to the content\n`;
|
|
63
|
+
}
|
|
64
|
+
if (options.includeLesson !== false) {
|
|
65
|
+
prompt += `- Create a micro-lesson (A1 level) inspired by the content\n`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
prompt += `\nOUTPUT FORMAT: JSON object with fields: adaptedContent, vocabulary (array of {es, ru}), grammarNotes (array of {concept, explanation}), lesson (object with title_es, focus, examples)\n`;
|
|
69
|
+
|
|
70
|
+
return prompt;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parse structured AI response
|
|
75
|
+
* @param {string} responseText
|
|
76
|
+
* @returns {Object}
|
|
77
|
+
*/
|
|
78
|
+
function parseAiResponse(responseText) {
|
|
79
|
+
// Try to extract JSON from response
|
|
80
|
+
const jsonMatch = responseText.match(/\{[\s\S]*\}/);
|
|
81
|
+
if (jsonMatch) {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(jsonMatch[0]);
|
|
84
|
+
} catch { /* fallback */ }
|
|
85
|
+
}
|
|
86
|
+
return { adaptedContent: responseText, vocabulary: [], grammarNotes: [] };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Calculate complexity score for content
|
|
91
|
+
* @param {string} content
|
|
92
|
+
* @returns {number}
|
|
93
|
+
*/
|
|
94
|
+
function calculateComplexity(content) {
|
|
95
|
+
const words = content.split(/\s+/);
|
|
96
|
+
const avgWordLength = words.reduce((sum, w) => sum + w.length, 0) / words.length;
|
|
97
|
+
const sentenceCount = content.split(/[.!?]+/).filter(Boolean).length;
|
|
98
|
+
const avgSentenceLength = words.length / sentenceCount;
|
|
99
|
+
|
|
100
|
+
// Simple score 0-1 based on word and sentence length
|
|
101
|
+
const score = Math.min(1, (avgWordLength / 10 + avgSentenceLength / 30) / 2);
|
|
102
|
+
return Math.round(score * 100) / 100;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ─── Handler Definition ────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
export default {
|
|
108
|
+
type: 'ai/content-adapt',
|
|
109
|
+
category: 'ai',
|
|
110
|
+
icon: 'auto_fix_high',
|
|
111
|
+
|
|
112
|
+
driver: {
|
|
113
|
+
description: 'AI-powered content adaptation to target language levels with vocabulary extraction',
|
|
114
|
+
inputs: [
|
|
115
|
+
{ name: 'content', type: 'string' },
|
|
116
|
+
],
|
|
117
|
+
outputs: [
|
|
118
|
+
{ name: 'result', type: 'any' },
|
|
119
|
+
{ name: 'error', type: 'string' },
|
|
120
|
+
],
|
|
121
|
+
params: {
|
|
122
|
+
operation: { type: 'string', default: 'adapt', description: 'Operation: adapt | adapt-news | adapt-trending' },
|
|
123
|
+
apiKey: { type: 'string', default: null, description: 'OpenRouter API key (or OPENROUTER_API_KEY env)' },
|
|
124
|
+
model: { type: 'string', default: 'anthropic/claude-sonnet-4', description: 'AI model to use' },
|
|
125
|
+
targetLevel: { type: 'string', default: 'A1', description: 'Target language level (A1, A2, B1, B2)' },
|
|
126
|
+
// Content metadata
|
|
127
|
+
title: { type: 'string', default: null, description: 'Content title (for news/trending)' },
|
|
128
|
+
sourceUrl: { type: 'string', default: null, description: 'Source URL' },
|
|
129
|
+
// Options
|
|
130
|
+
includeVocabulary: { type: 'boolean', default: true, description: 'Include vocabulary extraction' },
|
|
131
|
+
includeGrammarNotes: { type: 'boolean', default: true, description: 'Include grammar notes' },
|
|
132
|
+
includeLesson: { type: 'boolean', default: true, description: 'Include micro-lesson' },
|
|
133
|
+
// Rate limiting
|
|
134
|
+
maxRetries: { type: 'int', default: 3, description: 'Maximum retry attempts' },
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
lifecycle: {
|
|
139
|
+
validate: (inputs, params) => {
|
|
140
|
+
if (typeof inputs.content !== 'string' || inputs.content.length === 0) return false;
|
|
141
|
+
const apiKey = params.apiKey || process.env.OPENROUTER_API_KEY;
|
|
142
|
+
if (!apiKey) return false;
|
|
143
|
+
return true;
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
cacheKey: (inputs, params) => {
|
|
147
|
+
return `content-adapt:${params.operation}:${params.targetLevel}:${hashStr(inputs.content.slice(0, 200))}`;
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
execute: async (inputs, params) => {
|
|
151
|
+
const { content } = inputs;
|
|
152
|
+
const { operation, model, targetLevel, maxRetries } = params;
|
|
153
|
+
const apiKey = params.apiKey || process.env.OPENROUTER_API_KEY;
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
// Check cache
|
|
157
|
+
const cacheKey = `${operation}:${hashStr(content.slice(0, 200))}`;
|
|
158
|
+
const cached = cache.get(cacheKey);
|
|
159
|
+
if (cached && Date.now() - cached.timestamp < 3600000) {
|
|
160
|
+
return { result: { ...cached.data, cached: true } };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Determine content type
|
|
164
|
+
const contentType = operation === 'adapt-news' ? 'news'
|
|
165
|
+
: operation === 'adapt-trending' ? 'trending'
|
|
166
|
+
: 'general';
|
|
167
|
+
|
|
168
|
+
const fullContent = params.title
|
|
169
|
+
? `Title: ${params.title}\n\n${content}`
|
|
170
|
+
: content;
|
|
171
|
+
|
|
172
|
+
const prompt = createAdaptationPrompt(fullContent, contentType, targetLevel, {
|
|
173
|
+
includeVocabulary: params.includeVocabulary,
|
|
174
|
+
includeGrammarNotes: params.includeGrammarNotes,
|
|
175
|
+
includeLesson: params.includeLesson,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Make API request with retry
|
|
179
|
+
let lastError;
|
|
180
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
181
|
+
try {
|
|
182
|
+
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
headers: {
|
|
185
|
+
'Content-Type': 'application/json',
|
|
186
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
187
|
+
},
|
|
188
|
+
body: JSON.stringify({
|
|
189
|
+
model,
|
|
190
|
+
messages: [{ role: 'user', content: prompt }],
|
|
191
|
+
temperature: 0.7,
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
lastError = `API error: HTTP ${response.status}`;
|
|
197
|
+
if (attempt < maxRetries - 1) {
|
|
198
|
+
await new Promise(r => setTimeout(r, 1000 * (attempt + 1)));
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
return { error: lastError };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const data = await response.json();
|
|
205
|
+
const aiResponse = data.choices?.[0]?.message?.content || '';
|
|
206
|
+
|
|
207
|
+
const parsed = parseAiResponse(aiResponse);
|
|
208
|
+
const complexity = calculateComplexity(content);
|
|
209
|
+
|
|
210
|
+
const result = {
|
|
211
|
+
original: content,
|
|
212
|
+
adapted: parsed,
|
|
213
|
+
contentType,
|
|
214
|
+
targetLevel,
|
|
215
|
+
complexity,
|
|
216
|
+
model,
|
|
217
|
+
sourceUrl: params.sourceUrl,
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// Update cache
|
|
221
|
+
cache.set(cacheKey, { data: result, timestamp: Date.now() });
|
|
222
|
+
|
|
223
|
+
return { result };
|
|
224
|
+
} catch (err) {
|
|
225
|
+
lastError = err.message;
|
|
226
|
+
if (attempt < maxRetries - 1) {
|
|
227
|
+
await new Promise(r => setTimeout(r, 1000 * (attempt + 1)));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return { error: `content-adapt failed after ${maxRetries} attempts: ${lastError}` };
|
|
233
|
+
} catch (err) {
|
|
234
|
+
return { error: `content-adapt ${operation} failed: ${err.message}` };
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
};
|