opc-agent 0.5.1 → 0.7.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/dist/channels/web.d.ts +14 -1
- package/dist/channels/web.js +289 -3
- package/dist/cli.js +135 -4
- package/dist/core/auth.d.ts +13 -0
- package/dist/core/auth.js +41 -0
- package/dist/core/knowledge.d.ts +28 -0
- package/dist/core/knowledge.js +212 -0
- package/dist/deploy/hermes.d.ts +11 -0
- package/dist/deploy/hermes.js +147 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +21 -1
- package/dist/marketplace/index.d.ts +34 -0
- package/dist/marketplace/index.js +202 -0
- package/dist/schema/oad.d.ts +69 -0
- package/dist/schema/oad.js +7 -1
- package/dist/skills/document.d.ts +27 -0
- package/dist/skills/document.js +80 -0
- package/dist/skills/http.d.ts +8 -0
- package/dist/skills/http.js +34 -0
- package/dist/skills/scheduler.d.ts +21 -0
- package/dist/skills/scheduler.js +70 -0
- package/dist/skills/webhook-trigger.d.ts +17 -0
- package/dist/skills/webhook-trigger.js +49 -0
- package/package.json +1 -1
- package/src/channels/web.ts +302 -3
- package/src/cli.ts +149 -4
- package/src/core/auth.ts +57 -0
- package/src/core/knowledge.ts +210 -0
- package/src/deploy/hermes.ts +156 -0
- package/src/index.ts +18 -0
- package/src/marketplace/index.ts +223 -0
- package/src/schema/oad.ts +7 -0
- package/src/skills/document.ts +100 -0
- package/src/skills/http.ts +35 -0
- package/src/skills/scheduler.ts +80 -0
- package/src/skills/webhook-trigger.ts +59 -0
- package/templates/Dockerfile +15 -0
- package/templates/docker-compose.yml +21 -0
- package/tests/v070.test.ts +76 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knowledge Base / RAG - Local vector storage with semantic search
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import * as crypto from 'crypto';
|
|
7
|
+
|
|
8
|
+
// Simple in-memory vector store (PGlite-compatible interface for future migration)
|
|
9
|
+
interface VectorEntry {
|
|
10
|
+
id: string;
|
|
11
|
+
content: string;
|
|
12
|
+
embedding: number[];
|
|
13
|
+
metadata: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface KnowledgeStore {
|
|
17
|
+
entries: VectorEntry[];
|
|
18
|
+
version: number;
|
|
19
|
+
updatedAt: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const CHUNK_SIZE = 500; // chars per chunk
|
|
23
|
+
const CHUNK_OVERLAP = 50;
|
|
24
|
+
const STORE_FILE = '.opc-knowledge.json';
|
|
25
|
+
|
|
26
|
+
function splitText(text: string, chunkSize = CHUNK_SIZE, overlap = CHUNK_OVERLAP): string[] {
|
|
27
|
+
const chunks: string[] = [];
|
|
28
|
+
// Split by paragraphs first, then by size
|
|
29
|
+
const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim());
|
|
30
|
+
let current = '';
|
|
31
|
+
|
|
32
|
+
for (const para of paragraphs) {
|
|
33
|
+
if (current.length + para.length > chunkSize && current.length > 0) {
|
|
34
|
+
chunks.push(current.trim());
|
|
35
|
+
// Keep overlap from end of current
|
|
36
|
+
current = current.slice(-overlap) + '\n\n' + para;
|
|
37
|
+
} else {
|
|
38
|
+
current += (current ? '\n\n' : '') + para;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (current.trim()) chunks.push(current.trim());
|
|
42
|
+
|
|
43
|
+
// If any chunk is still too large, split by sentences
|
|
44
|
+
const result: string[] = [];
|
|
45
|
+
for (const chunk of chunks) {
|
|
46
|
+
if (chunk.length <= chunkSize * 1.5) {
|
|
47
|
+
result.push(chunk);
|
|
48
|
+
} else {
|
|
49
|
+
const sentences = chunk.split(/(?<=[.!?])\s+/);
|
|
50
|
+
let buf = '';
|
|
51
|
+
for (const s of sentences) {
|
|
52
|
+
if (buf.length + s.length > chunkSize && buf) {
|
|
53
|
+
result.push(buf.trim());
|
|
54
|
+
buf = buf.slice(-overlap) + ' ' + s;
|
|
55
|
+
} else {
|
|
56
|
+
buf += (buf ? ' ' : '') + s;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (buf.trim()) result.push(buf.trim());
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Simple TF-IDF-like embedding (no external dependencies)
|
|
66
|
+
// For production, replace with real embedding API
|
|
67
|
+
function simpleEmbed(text: string): number[] {
|
|
68
|
+
const words = text.toLowerCase().replace(/[^a-z0-9\s]/g, '').split(/\s+/).filter(Boolean);
|
|
69
|
+
const dim = 128;
|
|
70
|
+
const vec = new Array(dim).fill(0);
|
|
71
|
+
|
|
72
|
+
for (const word of words) {
|
|
73
|
+
const hash = crypto.createHash('md5').update(word).digest();
|
|
74
|
+
for (let i = 0; i < dim; i++) {
|
|
75
|
+
vec[i] += (hash[i % hash.length] - 128) / 128;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Normalize
|
|
80
|
+
const mag = Math.sqrt(vec.reduce((s, v) => s + v * v, 0)) || 1;
|
|
81
|
+
return vec.map(v => v / mag);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function cosineSimilarity(a: number[], b: number[]): number {
|
|
85
|
+
let dot = 0, magA = 0, magB = 0;
|
|
86
|
+
for (let i = 0; i < a.length; i++) {
|
|
87
|
+
dot += a[i] * b[i];
|
|
88
|
+
magA += a[i] * a[i];
|
|
89
|
+
magB += b[i] * b[i];
|
|
90
|
+
}
|
|
91
|
+
return dot / (Math.sqrt(magA) * Math.sqrt(magB) || 1);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class KnowledgeBase {
|
|
95
|
+
private store: KnowledgeStore;
|
|
96
|
+
private storePath: string;
|
|
97
|
+
|
|
98
|
+
constructor(baseDir: string = '.') {
|
|
99
|
+
this.storePath = path.join(baseDir, STORE_FILE);
|
|
100
|
+
this.store = this.load();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private load(): KnowledgeStore {
|
|
104
|
+
try {
|
|
105
|
+
if (fs.existsSync(this.storePath)) {
|
|
106
|
+
return JSON.parse(fs.readFileSync(this.storePath, 'utf-8'));
|
|
107
|
+
}
|
|
108
|
+
} catch { /* ignore */ }
|
|
109
|
+
return { entries: [], version: 1, updatedAt: new Date().toISOString() };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private save(): void {
|
|
113
|
+
this.store.updatedAt = new Date().toISOString();
|
|
114
|
+
fs.writeFileSync(this.storePath, JSON.stringify(this.store), 'utf-8');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async addFile(filePath: string): Promise<{ chunks: number }> {
|
|
118
|
+
const absPath = path.resolve(filePath);
|
|
119
|
+
if (!fs.existsSync(absPath)) {
|
|
120
|
+
throw new Error(`File not found: ${absPath}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const content = fs.readFileSync(absPath, 'utf-8');
|
|
124
|
+
const filename = path.basename(absPath);
|
|
125
|
+
|
|
126
|
+
// Remove existing entries for this file
|
|
127
|
+
this.store.entries = this.store.entries.filter(
|
|
128
|
+
e => e.metadata.source !== filename
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const chunks = splitText(content);
|
|
132
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
133
|
+
const chunk = chunks[i];
|
|
134
|
+
this.store.entries.push({
|
|
135
|
+
id: `${filename}_${i}_${Date.now()}`,
|
|
136
|
+
content: chunk,
|
|
137
|
+
embedding: simpleEmbed(chunk),
|
|
138
|
+
metadata: {
|
|
139
|
+
source: filename,
|
|
140
|
+
chunkIndex: i,
|
|
141
|
+
totalChunks: chunks.length,
|
|
142
|
+
addedAt: new Date().toISOString(),
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
this.save();
|
|
148
|
+
return { chunks: chunks.length };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async addText(text: string, source: string = 'manual'): Promise<{ chunks: number }> {
|
|
152
|
+
const chunks = splitText(text);
|
|
153
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
154
|
+
this.store.entries.push({
|
|
155
|
+
id: `${source}_${i}_${Date.now()}`,
|
|
156
|
+
content: chunks[i],
|
|
157
|
+
embedding: simpleEmbed(chunks[i]),
|
|
158
|
+
metadata: { source, chunkIndex: i, totalChunks: chunks.length, addedAt: new Date().toISOString() },
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
this.save();
|
|
162
|
+
return { chunks: chunks.length };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async search(query: string, topK: number = 5): Promise<Array<{ content: string; score: number; source: string }>> {
|
|
166
|
+
if (this.store.entries.length === 0) return [];
|
|
167
|
+
|
|
168
|
+
const queryEmb = simpleEmbed(query);
|
|
169
|
+
const scored = this.store.entries.map(entry => ({
|
|
170
|
+
content: entry.content,
|
|
171
|
+
score: cosineSimilarity(queryEmb, entry.embedding),
|
|
172
|
+
source: String(entry.metadata.source ?? 'unknown'),
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
scored.sort((a, b) => b.score - a.score);
|
|
176
|
+
return scored.slice(0, topK);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Build context string for injection into LLM calls */
|
|
180
|
+
async getContext(query: string, topK: number = 3, minScore: number = 0.1): Promise<string> {
|
|
181
|
+
const results = await this.search(query, topK);
|
|
182
|
+
const relevant = results.filter(r => r.score >= minScore);
|
|
183
|
+
if (relevant.length === 0) return '';
|
|
184
|
+
|
|
185
|
+
return `\n\n--- Relevant Knowledge ---\n${relevant.map((r, i) =>
|
|
186
|
+
`[${i + 1}] (source: ${r.source}, relevance: ${(r.score * 100).toFixed(0)}%)\n${r.content}`
|
|
187
|
+
).join('\n\n')}\n--- End Knowledge ---\n`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
getStats(): { totalEntries: number; sources: string[]; updatedAt: string } {
|
|
191
|
+
const sources = [...new Set(this.store.entries.map(e => String(e.metadata.source)))];
|
|
192
|
+
return {
|
|
193
|
+
totalEntries: this.store.entries.length,
|
|
194
|
+
sources,
|
|
195
|
+
updatedAt: this.store.updatedAt,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
clear(): void {
|
|
200
|
+
this.store.entries = [];
|
|
201
|
+
this.save();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
removeSource(source: string): number {
|
|
205
|
+
const before = this.store.entries.length;
|
|
206
|
+
this.store.entries = this.store.entries.filter(e => e.metadata.source !== source);
|
|
207
|
+
this.save();
|
|
208
|
+
return before - this.store.entries.length;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hermes Agent Adapter - Convert OAD → Hermes Agent config
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import type { OADDocument } from '../schema/oad';
|
|
7
|
+
|
|
8
|
+
export interface HermesDeployOptions {
|
|
9
|
+
oad: OADDocument;
|
|
10
|
+
outputDir: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HermesDeployResult {
|
|
14
|
+
outputDir: string;
|
|
15
|
+
files: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface HermesCharacter {
|
|
19
|
+
name: string;
|
|
20
|
+
description: string;
|
|
21
|
+
personality: string;
|
|
22
|
+
system: string;
|
|
23
|
+
bio: string[];
|
|
24
|
+
lore: string[];
|
|
25
|
+
messageExamples: Array<Array<{ user: string; content: { text: string } }>>;
|
|
26
|
+
postExamples: string[];
|
|
27
|
+
topics: string[];
|
|
28
|
+
adjectives: string[];
|
|
29
|
+
style: {
|
|
30
|
+
all: string[];
|
|
31
|
+
chat: string[];
|
|
32
|
+
post: string[];
|
|
33
|
+
};
|
|
34
|
+
plugins: string[];
|
|
35
|
+
settings: {
|
|
36
|
+
model: string;
|
|
37
|
+
voice: { model: string };
|
|
38
|
+
secrets: Record<string, string>;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function oadToHermesCharacter(oad: OADDocument): HermesCharacter {
|
|
43
|
+
const m = oad.metadata;
|
|
44
|
+
const s = oad.spec;
|
|
45
|
+
const prompt = s.systemPrompt ?? 'You are a helpful AI agent.';
|
|
46
|
+
|
|
47
|
+
// Extract personality traits from system prompt
|
|
48
|
+
const lines = prompt.split('\n').filter(l => l.trim());
|
|
49
|
+
const bio = lines.slice(0, 3).map(l => l.replace(/^[-*#]\s*/, '').trim());
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
name: m.name,
|
|
53
|
+
description: m.description ?? `${m.name} - AI Agent`,
|
|
54
|
+
personality: prompt.slice(0, 500),
|
|
55
|
+
system: prompt,
|
|
56
|
+
bio: bio.length > 0 ? bio : [`${m.name} is an AI agent built with OPC Agent framework.`],
|
|
57
|
+
lore: [`Created with OPC Agent v${m.version}`, `Licensed under ${m.license}`],
|
|
58
|
+
messageExamples: [
|
|
59
|
+
[
|
|
60
|
+
{ user: '{{user1}}', content: { text: 'Hello!' } },
|
|
61
|
+
{ user: m.name, content: { text: 'Hi there! How can I help you today?' } },
|
|
62
|
+
],
|
|
63
|
+
],
|
|
64
|
+
postExamples: [],
|
|
65
|
+
topics: s.skills.map(sk => sk.name),
|
|
66
|
+
adjectives: ['helpful', 'knowledgeable', 'professional'],
|
|
67
|
+
style: {
|
|
68
|
+
all: ['Be helpful and professional', 'Use clear language'],
|
|
69
|
+
chat: ['Respond conversationally', 'Be concise but thorough'],
|
|
70
|
+
post: ['Share useful insights', 'Be informative'],
|
|
71
|
+
},
|
|
72
|
+
plugins: s.skills.map(sk => sk.name),
|
|
73
|
+
settings: {
|
|
74
|
+
model: s.model,
|
|
75
|
+
voice: { model: 'en_US-neutral' },
|
|
76
|
+
secrets: {},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function generateHermesSettings(oad: OADDocument): Record<string, any> {
|
|
82
|
+
return {
|
|
83
|
+
name: oad.metadata.name,
|
|
84
|
+
version: oad.metadata.version,
|
|
85
|
+
runtime: {
|
|
86
|
+
provider: oad.spec.provider?.default ?? 'openai',
|
|
87
|
+
model: oad.spec.model,
|
|
88
|
+
temperature: 0.7,
|
|
89
|
+
maxTokens: 2048,
|
|
90
|
+
},
|
|
91
|
+
channels: oad.spec.channels.map(ch => ({
|
|
92
|
+
type: ch.type,
|
|
93
|
+
enabled: true,
|
|
94
|
+
config: ch.config ?? {},
|
|
95
|
+
})),
|
|
96
|
+
memory: {
|
|
97
|
+
enabled: !!oad.spec.memory,
|
|
98
|
+
provider: typeof oad.spec.memory?.longTerm === 'object'
|
|
99
|
+
? oad.spec.memory.longTerm.provider
|
|
100
|
+
: 'in-memory',
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function deployToHermes(options: HermesDeployOptions): HermesDeployResult {
|
|
106
|
+
const { oad, outputDir } = options;
|
|
107
|
+
const files: string[] = [];
|
|
108
|
+
|
|
109
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
110
|
+
|
|
111
|
+
// character.json
|
|
112
|
+
const character = oadToHermesCharacter(oad);
|
|
113
|
+
const charPath = path.join(outputDir, 'character.json');
|
|
114
|
+
fs.writeFileSync(charPath, JSON.stringify(character, null, 2), 'utf-8');
|
|
115
|
+
files.push('character.json');
|
|
116
|
+
|
|
117
|
+
// settings.json
|
|
118
|
+
const settings = generateHermesSettings(oad);
|
|
119
|
+
const settingsPath = path.join(outputDir, 'settings.json');
|
|
120
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf-8');
|
|
121
|
+
files.push('settings.json');
|
|
122
|
+
|
|
123
|
+
// .env template
|
|
124
|
+
const envContent = `# Hermes Agent Environment
|
|
125
|
+
HERMES_CHARACTER=${oad.metadata.name}
|
|
126
|
+
HERMES_MODEL=${oad.spec.model}
|
|
127
|
+
HERMES_PROVIDER=${oad.spec.provider?.default ?? 'openai'}
|
|
128
|
+
# Add your API keys below:
|
|
129
|
+
# OPENAI_API_KEY=
|
|
130
|
+
# DEEPSEEK_API_KEY=
|
|
131
|
+
`;
|
|
132
|
+
fs.writeFileSync(path.join(outputDir, '.env.hermes'), envContent, 'utf-8');
|
|
133
|
+
files.push('.env.hermes');
|
|
134
|
+
|
|
135
|
+
// README
|
|
136
|
+
const readme = `# ${oad.metadata.name} - Hermes Agent
|
|
137
|
+
|
|
138
|
+
Converted from OAD format using \`opc deploy --target hermes\`.
|
|
139
|
+
|
|
140
|
+
## Usage
|
|
141
|
+
|
|
142
|
+
1. Copy \`character.json\` to your Hermes agents directory
|
|
143
|
+
2. Configure \`.env.hermes\` with your API keys
|
|
144
|
+
3. Start Hermes with this character
|
|
145
|
+
|
|
146
|
+
## Files
|
|
147
|
+
|
|
148
|
+
- \`character.json\` - Agent character definition
|
|
149
|
+
- \`settings.json\` - Runtime settings
|
|
150
|
+
- \`.env.hermes\` - Environment template
|
|
151
|
+
`;
|
|
152
|
+
fs.writeFileSync(path.join(outputDir, 'README.md'), readme, 'utf-8');
|
|
153
|
+
files.push('README.md');
|
|
154
|
+
|
|
155
|
+
return { outputDir, files };
|
|
156
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -47,3 +47,21 @@ export { ConnectionPool, RequestBatcher, LazyLoader } from './core/performance';
|
|
|
47
47
|
export type { AnalyticsSnapshot } from './analytics';
|
|
48
48
|
export { t, setLocale, getLocale, detectLocale, addMessages } from './i18n';
|
|
49
49
|
export type { Locale } from './i18n';
|
|
50
|
+
|
|
51
|
+
// v0.5.0+ modules
|
|
52
|
+
export { KnowledgeBase } from './core/knowledge';
|
|
53
|
+
export { deployToHermes } from './deploy/hermes';
|
|
54
|
+
export type { HermesDeployOptions, HermesDeployResult } from './deploy/hermes';
|
|
55
|
+
export { publishAgent, installAgent } from './marketplace';
|
|
56
|
+
export type { AgentManifest, PublishOptions, InstallOptions } from './marketplace';
|
|
57
|
+
|
|
58
|
+
// v0.7.0 modules
|
|
59
|
+
export { createAuthMiddleware, getActiveSessions } from './core/auth';
|
|
60
|
+
export type { AuthConfig, AuthSession } from './core/auth';
|
|
61
|
+
export { HttpSkill } from './skills/http';
|
|
62
|
+
export { WebhookTriggerSkill } from './skills/webhook-trigger';
|
|
63
|
+
export type { WebhookTarget } from './skills/webhook-trigger';
|
|
64
|
+
export { SchedulerSkill } from './skills/scheduler';
|
|
65
|
+
export type { ScheduledTask } from './skills/scheduler';
|
|
66
|
+
export { DocumentSkill } from './skills/document';
|
|
67
|
+
export type { DocumentChunk } from './skills/document';
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Marketplace - Package, publish, and install agents
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import * as crypto from 'crypto';
|
|
7
|
+
import { execSync } from 'child_process';
|
|
8
|
+
|
|
9
|
+
export interface AgentManifest {
|
|
10
|
+
name: string;
|
|
11
|
+
version: string;
|
|
12
|
+
description: string;
|
|
13
|
+
author: string;
|
|
14
|
+
license: string;
|
|
15
|
+
oadVersion: string;
|
|
16
|
+
channels: string[];
|
|
17
|
+
skills: string[];
|
|
18
|
+
files: string[];
|
|
19
|
+
checksum: string;
|
|
20
|
+
publishedAt: string;
|
|
21
|
+
homepage?: string;
|
|
22
|
+
repository?: string;
|
|
23
|
+
tags?: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PublishOptions {
|
|
27
|
+
oadPath: string;
|
|
28
|
+
outputDir?: string;
|
|
29
|
+
includeKnowledge?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface InstallOptions {
|
|
33
|
+
source: string; // local path or URL
|
|
34
|
+
targetDir?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function computeChecksum(filePath: string): string {
|
|
38
|
+
const content = fs.readFileSync(filePath);
|
|
39
|
+
return crypto.createHash('sha256').update(content).digest('hex').slice(0, 16);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function publishAgent(options: PublishOptions): Promise<{ archivePath: string; manifest: AgentManifest }> {
|
|
43
|
+
const { oadPath, outputDir = '.', includeKnowledge = false } = options;
|
|
44
|
+
const absOad = path.resolve(oadPath);
|
|
45
|
+
const baseDir = path.dirname(absOad);
|
|
46
|
+
|
|
47
|
+
if (!fs.existsSync(absOad)) {
|
|
48
|
+
throw new Error(`OAD file not found: ${absOad}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Dynamic import yaml
|
|
52
|
+
const yaml = await import('js-yaml');
|
|
53
|
+
const oadContent = fs.readFileSync(absOad, 'utf-8');
|
|
54
|
+
const oad = yaml.load(oadContent) as any;
|
|
55
|
+
|
|
56
|
+
const name = oad.metadata?.name ?? 'unnamed-agent';
|
|
57
|
+
const version = oad.metadata?.version ?? '0.0.0';
|
|
58
|
+
const safeName = name.toLowerCase().replace(/[^a-z0-9-]/g, '-');
|
|
59
|
+
|
|
60
|
+
// Collect files to package
|
|
61
|
+
const filesToPack: { rel: string; abs: string }[] = [
|
|
62
|
+
{ rel: path.basename(absOad), abs: absOad },
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
// Include common files
|
|
66
|
+
const extras = ['.env.example', 'README.md', 'package.json'];
|
|
67
|
+
for (const f of extras) {
|
|
68
|
+
const fp = path.join(baseDir, f);
|
|
69
|
+
if (fs.existsSync(fp)) {
|
|
70
|
+
filesToPack.push({ rel: f, abs: fp });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Include knowledge base if requested
|
|
75
|
+
if (includeKnowledge) {
|
|
76
|
+
const kbFile = path.join(baseDir, '.opc-knowledge.json');
|
|
77
|
+
if (fs.existsSync(kbFile)) {
|
|
78
|
+
filesToPack.push({ rel: '.opc-knowledge.json', abs: kbFile });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Include prompts directory if exists
|
|
83
|
+
const promptsDir = path.join(baseDir, 'prompts');
|
|
84
|
+
if (fs.existsSync(promptsDir) && fs.statSync(promptsDir).isDirectory()) {
|
|
85
|
+
const promptFiles = fs.readdirSync(promptsDir);
|
|
86
|
+
for (const pf of promptFiles) {
|
|
87
|
+
filesToPack.push({ rel: `prompts/${pf}`, abs: path.join(promptsDir, pf) });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Build manifest
|
|
92
|
+
const manifest: AgentManifest = {
|
|
93
|
+
name: safeName,
|
|
94
|
+
version,
|
|
95
|
+
description: oad.metadata?.description ?? '',
|
|
96
|
+
author: oad.metadata?.author ?? '',
|
|
97
|
+
license: oad.metadata?.license ?? 'Apache-2.0',
|
|
98
|
+
oadVersion: 'opc/v1',
|
|
99
|
+
channels: (oad.spec?.channels ?? []).map((c: any) => c.type),
|
|
100
|
+
skills: (oad.spec?.skills ?? []).map((s: any) => s.name),
|
|
101
|
+
files: filesToPack.map(f => f.rel),
|
|
102
|
+
checksum: '',
|
|
103
|
+
publishedAt: new Date().toISOString(),
|
|
104
|
+
tags: oad.metadata?.marketplace?.tags,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Create staging directory
|
|
108
|
+
const stageDir = path.join(outputDir, `.opc-stage-${safeName}`);
|
|
109
|
+
fs.mkdirSync(stageDir, { recursive: true });
|
|
110
|
+
|
|
111
|
+
for (const f of filesToPack) {
|
|
112
|
+
const dest = path.join(stageDir, f.rel);
|
|
113
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
114
|
+
fs.copyFileSync(f.abs, dest);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Write manifest
|
|
118
|
+
fs.writeFileSync(path.join(stageDir, 'opc-manifest.json'), JSON.stringify(manifest, null, 2));
|
|
119
|
+
|
|
120
|
+
// Create tar.gz
|
|
121
|
+
const archiveName = `${safeName}-${version}.tar.gz`;
|
|
122
|
+
const archivePath = path.join(outputDir, archiveName);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
execSync(`tar -czf "${archivePath}" -C "${stageDir}" .`, { stdio: 'pipe' });
|
|
126
|
+
} catch {
|
|
127
|
+
// Fallback: just zip the directory content list
|
|
128
|
+
// On Windows without tar, create a simple zip-like package
|
|
129
|
+
const packageData = {
|
|
130
|
+
manifest,
|
|
131
|
+
files: filesToPack.map(f => ({
|
|
132
|
+
path: f.rel,
|
|
133
|
+
content: fs.readFileSync(f.abs, 'utf-8'),
|
|
134
|
+
})),
|
|
135
|
+
};
|
|
136
|
+
fs.writeFileSync(
|
|
137
|
+
archivePath.replace('.tar.gz', '.opc.json'),
|
|
138
|
+
JSON.stringify(packageData, null, 2),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Compute checksum
|
|
143
|
+
if (fs.existsSync(archivePath)) {
|
|
144
|
+
manifest.checksum = computeChecksum(archivePath);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Cleanup staging
|
|
148
|
+
fs.rmSync(stageDir, { recursive: true, force: true });
|
|
149
|
+
|
|
150
|
+
// Write final manifest
|
|
151
|
+
fs.writeFileSync(
|
|
152
|
+
path.join(outputDir, 'opc-manifest.json'),
|
|
153
|
+
JSON.stringify(manifest, null, 2),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return { archivePath: fs.existsSync(archivePath) ? archivePath : archivePath.replace('.tar.gz', '.opc.json'), manifest };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function installAgent(options: InstallOptions): Promise<{ dir: string; manifest: AgentManifest }> {
|
|
160
|
+
const { source, targetDir } = options;
|
|
161
|
+
const absSource = path.resolve(source);
|
|
162
|
+
|
|
163
|
+
if (!fs.existsSync(absSource)) {
|
|
164
|
+
throw new Error(`Package not found: ${absSource}`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let manifest: AgentManifest;
|
|
168
|
+
let installDir: string;
|
|
169
|
+
|
|
170
|
+
if (absSource.endsWith('.opc.json')) {
|
|
171
|
+
// JSON package format
|
|
172
|
+
const pkg = JSON.parse(fs.readFileSync(absSource, 'utf-8'));
|
|
173
|
+
manifest = pkg.manifest;
|
|
174
|
+
installDir = targetDir ?? path.join('.', manifest.name);
|
|
175
|
+
fs.mkdirSync(installDir, { recursive: true });
|
|
176
|
+
|
|
177
|
+
for (const f of pkg.files) {
|
|
178
|
+
const dest = path.join(installDir, f.path);
|
|
179
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
180
|
+
fs.writeFileSync(dest, f.content, 'utf-8');
|
|
181
|
+
}
|
|
182
|
+
fs.writeFileSync(path.join(installDir, 'opc-manifest.json'), JSON.stringify(manifest, null, 2));
|
|
183
|
+
} else {
|
|
184
|
+
// tar.gz format
|
|
185
|
+
const tmpDir = path.join(path.dirname(absSource), '.opc-extract-tmp');
|
|
186
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
execSync(`tar -xzf "${absSource}" -C "${tmpDir}"`, { stdio: 'pipe' });
|
|
190
|
+
} catch {
|
|
191
|
+
throw new Error('Failed to extract package. Ensure tar is available.');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const manifestPath = path.join(tmpDir, 'opc-manifest.json');
|
|
195
|
+
if (!fs.existsSync(manifestPath)) {
|
|
196
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
197
|
+
throw new Error('Invalid package: missing opc-manifest.json');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
|
|
201
|
+
installDir = targetDir ?? path.join('.', manifest.name);
|
|
202
|
+
|
|
203
|
+
// Move files
|
|
204
|
+
fs.mkdirSync(installDir, { recursive: true });
|
|
205
|
+
const copyRecursive = (src: string, dest: string) => {
|
|
206
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
207
|
+
for (const entry of entries) {
|
|
208
|
+
const srcPath = path.join(src, entry.name);
|
|
209
|
+
const destPath = path.join(dest, entry.name);
|
|
210
|
+
if (entry.isDirectory()) {
|
|
211
|
+
fs.mkdirSync(destPath, { recursive: true });
|
|
212
|
+
copyRecursive(srcPath, destPath);
|
|
213
|
+
} else {
|
|
214
|
+
fs.copyFileSync(srcPath, destPath);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
copyRecursive(tmpDir, installDir);
|
|
219
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { dir: installDir, manifest };
|
|
223
|
+
}
|
package/src/schema/oad.ts
CHANGED
|
@@ -48,6 +48,12 @@ export const HITLSchema = z.object({
|
|
|
48
48
|
defaultAction: z.enum(['approve', 'deny']).default('deny'),
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
+
export const AuthSchema = z.object({
|
|
52
|
+
enabled: z.boolean().default(false),
|
|
53
|
+
apiKeys: z.array(z.string()).default([]),
|
|
54
|
+
sessionIsolation: z.boolean().default(true),
|
|
55
|
+
});
|
|
56
|
+
|
|
51
57
|
export const ChannelSchema = z.object({
|
|
52
58
|
type: z.enum(['web', 'websocket', 'telegram', 'cli', 'voice', 'webhook']),
|
|
53
59
|
port: z.number().optional(),
|
|
@@ -124,6 +130,7 @@ export const SpecSchema = z.object({
|
|
|
124
130
|
voice: VoiceSchema.optional(),
|
|
125
131
|
webhook: WebhookSchema.optional(),
|
|
126
132
|
hitl: HITLSchema.optional(),
|
|
133
|
+
auth: AuthSchema.optional(),
|
|
127
134
|
});
|
|
128
135
|
|
|
129
136
|
export const OADSchema = z.object({
|