-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.ts
More file actions
379 lines (339 loc) · 14.3 KB
/
Copy pathplugin.ts
File metadata and controls
379 lines (339 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type { Plugin, PluginContext } from '@objectstack/core';
import type { IAIService, IAIConversationService, IDataEngine, IMetadataService, LLMAdapter } from '@objectstack/spec/contracts';
import { AIService } from './ai-service.js';
import type { AIServiceConfig } from './ai-service.js';
import { buildAIRoutes } from './routes/ai-routes.js';
import { buildAgentRoutes } from './routes/agent-routes.js';
import { ObjectQLConversationService } from './conversation/objectql-conversation-service.js';
import { AiConversationObject, AiMessageObject } from './objects/index.js';
import { registerDataTools } from './tools/data-tools.js';
import { registerMetadataTools } from './tools/metadata-tools.js';
import { AgentRuntime } from './agent-runtime.js';
import { DATA_CHAT_AGENT, METADATA_ASSISTANT_AGENT } from './agents/index.js';
import { VercelLLMAdapter } from './adapters/vercel-adapter.js';
import { MemoryLLMAdapter } from './adapters/memory-adapter.js';
/**
* Configuration options for the AIServicePlugin.
*/
export interface AIServicePluginOptions {
/** LLM adapter to use (defaults to MemoryLLMAdapter). */
adapter?: LLMAdapter;
/** Enable debug logging. */
debug?: boolean;
/** Explicit conversation service override. When set, auto-detection is skipped. */
conversationService?: IAIConversationService;
}
/**
* AIServicePlugin — Kernel plugin for the unified AI capability service.
*
* Lifecycle:
* 1. **init** — Creates {@link AIService}, registers as `'ai'` service.
* If an existing AI service is already registered, it is replaced.
* 2. **start** — Triggers `'ai:ready'` hook so other plugins can register
* tools or extend the service. Registers REST/SSE routes.
* 3. **destroy** — Cleans up references.
*
* @example
* ```ts
* import { LiteKernel } from '@objectstack/core';
* import { AIServicePlugin } from '@objectstack/service-ai';
*
* const kernel = new LiteKernel();
* kernel.use(new AIServicePlugin());
* await kernel.bootstrap();
*
* const ai = kernel.getService<IAIService>('ai');
* const result = await ai.chat([{ role: 'user', content: 'Hello' }]);
* ```
*/
export class AIServicePlugin implements Plugin {
name = 'com.objectstack.service-ai';
version = '1.0.0';
type = 'standard' as const;
dependencies: string[] = ['com.objectstack.engine.objectql']; // manifest service required
private service?: AIService;
private readonly options: AIServicePluginOptions;
constructor(options: AIServicePluginOptions = {}) {
this.options = options;
}
/**
* Auto-detect LLM provider from environment variables.
*
* Priority order:
* 1. AI_GATEWAY_MODEL → Vercel AI Gateway
* 2. OPENAI_API_KEY → OpenAI
* 3. ANTHROPIC_API_KEY → Anthropic
* 4. GOOGLE_GENERATIVE_AI_API_KEY → Google
* 5. Fallback → MemoryLLMAdapter
*
* Returns the adapter and a description for logging.
*/
private async detectAdapter(ctx: PluginContext): Promise<{ adapter: LLMAdapter; description: string }> {
// 1. Vercel AI Gateway — works with any provider via gateway('provider/model')
const gatewayModel = process.env.AI_GATEWAY_MODEL;
if (gatewayModel) {
try {
const gatewayPkg = '@ai-sdk/gateway';
const { gateway } = await import(/* webpackIgnore: true */ gatewayPkg);
const adapter = new VercelLLMAdapter({ model: gateway(gatewayModel) });
return { adapter, description: `Vercel AI Gateway (model: ${gatewayModel})` };
} catch (err) {
ctx.logger.warn(
`[AI] Failed to load @ai-sdk/gateway for AI_GATEWAY_MODEL=${gatewayModel}, trying next provider`,
err instanceof Error ? { error: err.message } : undefined
);
}
}
// 2. Direct provider SDKs
const providerConfigs: Array<{
envKey: string;
pkg: string;
factory: string;
defaultModel: string;
displayName: string;
}> = [
{
envKey: 'OPENAI_API_KEY',
pkg: '@ai-sdk/openai',
factory: 'openai',
defaultModel: 'gpt-4o',
displayName: 'OpenAI'
},
{
envKey: 'ANTHROPIC_API_KEY',
pkg: '@ai-sdk/anthropic',
factory: 'anthropic',
defaultModel: 'claude-sonnet-4-20250514',
displayName: 'Anthropic'
},
{
envKey: 'GOOGLE_GENERATIVE_AI_API_KEY',
pkg: '@ai-sdk/google',
factory: 'google',
defaultModel: 'gemini-2.0-flash',
displayName: 'Google'
},
];
for (const { envKey, pkg, factory, defaultModel, displayName } of providerConfigs) {
if (process.env[envKey]) {
try {
const mod = await import(/* webpackIgnore: true */ pkg);
const createModel = mod[factory] ?? mod.default;
if (typeof createModel === 'function') {
const modelId = process.env.AI_MODEL ?? defaultModel;
const adapter = new VercelLLMAdapter({ model: createModel(modelId) });
return { adapter, description: `${displayName} (model: ${modelId})` };
}
} catch (err) {
ctx.logger.warn(
`[AI] Failed to load ${pkg} for ${envKey}, trying next provider`,
err instanceof Error ? { error: err.message } : undefined
);
}
}
}
// 3. Fallback to MemoryLLMAdapter
ctx.logger.warn('[AI] No LLM provider configured via environment variables. Falling back to MemoryLLMAdapter (echo mode). Set AI_GATEWAY_MODEL, OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_GENERATIVE_AI_API_KEY to use a real LLM.');
return { adapter: new MemoryLLMAdapter(), description: 'MemoryLLMAdapter (echo mode - for testing only)' };
}
async init(ctx: PluginContext): Promise<void> {
// Check if there is an existing AI service (e.g. from dev-plugin)
let hasExisting = false;
try {
const existing = ctx.getService<IAIService>('ai');
if (existing && typeof existing.chat === 'function') {
hasExisting = true;
ctx.logger.debug('[AI] Found existing AI service, replacing');
}
} catch {
// No existing service — that's fine
}
// Determine conversation service: explicit > auto-detect IDataEngine > InMemory fallback
let conversationService: IAIConversationService | undefined = this.options.conversationService;
if (!conversationService) {
try {
const engine = ctx.getService<IDataEngine>('data');
if (engine && typeof engine.find === 'function') {
conversationService = new ObjectQLConversationService(engine);
ctx.logger.info('[AI] Using ObjectQLConversationService (IDataEngine detected)');
}
} catch {
// No data engine — fall back to InMemory
}
}
// Determine LLM adapter: explicit > auto-detect from env > MemoryLLMAdapter fallback
let adapter: LLMAdapter;
let adapterDescription: string;
if (this.options.adapter) {
// User provided an explicit adapter
adapter = this.options.adapter;
adapterDescription = `${adapter.name} (explicitly configured)`;
} else {
// Auto-detect from environment variables
const detected = await this.detectAdapter(ctx);
adapter = detected.adapter;
adapterDescription = detected.description;
}
// Log the selected adapter
ctx.logger.info(`[AI] Using LLM adapter: ${adapterDescription}`);
const config: AIServiceConfig = {
adapter,
logger: ctx.logger,
conversationService,
};
this.service = new AIService(config);
// Register or replace the AI service
if (hasExisting) {
ctx.replaceService('ai', this.service);
} else {
ctx.registerService('ai', this.service);
}
// Register AI system objects via the manifest service.
ctx.getService<{ register(m: any): void }>('manifest').register({
id: 'com.objectstack.service-ai',
name: 'AI Service',
version: '1.0.0',
type: 'plugin',
namespace: 'ai',
objects: [AiConversationObject, AiMessageObject],
});
if (this.options.debug) {
ctx.hook('ai:beforeChat', async (messages: unknown) => {
ctx.logger.debug('[AI] Before chat', { messages });
});
}
// Contribute navigation items to the Setup App (if SetupPlugin is loaded).
try {
const setupNav = ctx.getService<{ contribute(c: any): void }>('setupNav');
if (setupNav) {
setupNav.contribute({
areaId: 'area_ai',
items: [
{ id: 'nav_ai_conversations', type: 'object', label: { key: 'setup.nav.ai_conversations', defaultValue: 'Conversations' }, objectName: 'conversations', icon: 'message-square', order: 10 },
{ id: 'nav_ai_messages', type: 'object', label: { key: 'setup.nav.ai_messages', defaultValue: 'Messages' }, objectName: 'messages', icon: 'messages-square', order: 20 },
],
});
ctx.logger.info('[AI] Navigation items contributed to Setup App');
}
} catch {
// SetupPlugin not loaded — skip silently
}
ctx.logger.info('[AI] Service initialized');
}
async start(ctx: PluginContext): Promise<void> {
if (!this.service) return;
// ── Auto-register built-in tools & agents when services are available ──
let metadataService: IMetadataService | undefined;
try {
metadataService = ctx.getService<IMetadataService>('metadata');
} catch (e: any) {
ctx.logger.debug('[AI] Metadata service not available');
}
// Data tools require only the data engine
try {
const dataEngine = ctx.getService<IDataEngine>('data');
if (dataEngine) {
registerDataTools(this.service.toolRegistry, { dataEngine });
ctx.logger.info('[AI] Built-in data tools registered');
// Register data tools as metadata (for Studio visibility)
if (metadataService) {
const { DATA_TOOL_DEFINITIONS } = await import('./tools/data-tools.js');
for (const toolDef of DATA_TOOL_DEFINITIONS) {
const toolExists =
typeof metadataService.exists === 'function'
? await metadataService.exists('tool', toolDef.name)
: false;
if (!toolExists) {
await metadataService.register('tool', toolDef.name, toolDef);
}
}
ctx.logger.info(`[AI] ${DATA_TOOL_DEFINITIONS.length} data tools registered as metadata`);
}
// Register the built-in data_chat agent (requires metadata service)
if (metadataService) {
try {
const agentExists =
typeof metadataService.exists === 'function'
? await metadataService.exists('agent', DATA_CHAT_AGENT.name)
: false;
if (!agentExists) {
await metadataService.register('agent', DATA_CHAT_AGENT.name, DATA_CHAT_AGENT);
ctx.logger.info('[AI] data_chat agent registered');
} else {
ctx.logger.debug('[AI] data_chat agent already exists, skipping auto-registration');
}
} catch (err) {
ctx.logger.warn('[AI] Failed to register data_chat agent', err instanceof Error ? { error: err.message, stack: err.stack } : { error: String(err) });
}
}
}
} catch {
ctx.logger.debug('[AI] Data engine not available, skipping data tools');
}
// Metadata tools require only the metadata service
if (metadataService) {
try {
registerMetadataTools(this.service.toolRegistry, { metadataService });
ctx.logger.info('[AI] Built-in metadata tools registered');
// Register metadata tools as metadata (for Studio visibility)
const { METADATA_TOOL_DEFINITIONS } = await import('./tools/metadata-tools.js');
for (const toolDef of METADATA_TOOL_DEFINITIONS) {
const toolExists =
typeof metadataService.exists === 'function'
? await metadataService.exists('tool', toolDef.name)
: false;
if (!toolExists) {
await metadataService.register('tool', toolDef.name, toolDef);
}
}
ctx.logger.info(`[AI] ${METADATA_TOOL_DEFINITIONS.length} metadata tools registered as metadata`);
// Register the built-in metadata_assistant agent
try {
const agentExists =
typeof metadataService.exists === 'function'
? await metadataService.exists('agent', METADATA_ASSISTANT_AGENT.name)
: false;
if (!agentExists) {
await metadataService.register('agent', METADATA_ASSISTANT_AGENT.name, METADATA_ASSISTANT_AGENT);
ctx.logger.info('[AI] metadata_assistant agent registered');
} else {
ctx.logger.debug('[AI] metadata_assistant agent already exists, skipping auto-registration');
}
} catch (err) {
ctx.logger.warn('[AI] Failed to register metadata_assistant agent', err instanceof Error ? { error: err.message, stack: err.stack } : { error: String(err) });
}
} catch (err) {
ctx.logger.debug('[AI] Failed to register metadata tools', err instanceof Error ? err : undefined);
}
}
// Trigger hook to notify AI service is ready — other plugins can register tools
await ctx.trigger('ai:ready', this.service);
// Build and expose route definitions
const routes = buildAIRoutes(this.service, this.service.conversationService, ctx.logger);
// Build agent routes if metadata service is available
if (metadataService) {
const agentRuntime = new AgentRuntime(metadataService);
const agentRoutes = buildAgentRoutes(this.service, agentRuntime, ctx.logger);
routes.push(...agentRoutes);
ctx.logger.info(`[AI] Agent routes registered (${agentRoutes.length} routes)`);
} else {
ctx.logger.debug('[AI] Metadata service not available, skipping agent routes');
}
// Trigger hook so HTTP server plugins can mount these routes
await ctx.trigger('ai:routes', routes);
// Cache routes on the kernel so HttpDispatcher can find them
const kernel = ctx.getKernel();
if (kernel) {
(kernel as any).__aiRoutes = routes;
}
ctx.logger.info(
`[AI] Service started — adapter="${this.service.adapterName}", ` +
`tools=${this.service.toolRegistry.size}, ` +
`routes=${routes.length}`,
);
}
async destroy(): Promise<void> {
this.service = undefined;
}
}