-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add global plugins #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const globalPlugins = [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ import CodeInjector from './modules/codeInjector.js'; | |
| import ExpressServer from './servers/express.js'; | ||
| import OpenApiRegistry from './servers/openapi.js'; | ||
| // import FastifyServer from './servers/fastify.js'; | ||
| import { ADMINFORTH_VERSION, listify, suggestIfTypo, RateLimiter, RAMLock, getClientIp, isProbablyUUIDColumn, convertPeriodToSeconds, hookResponseError } from './modules/utils.js'; | ||
| import { ADMINFORTH_VERSION, listify, suggestIfTypo, RateLimiter, RAMLock, getClientIp, isProbablyUUIDColumn, convertPeriodToSeconds, hookResponseError, formatHugePluginError } from './modules/utils.js'; | ||
| import { | ||
| type AdminForthConfig, | ||
| type IAdminForth, | ||
|
|
@@ -237,13 +237,18 @@ class AdminForth implements IAdminForth { | |
| activatePlugins() { | ||
| afLogger.trace('🔌🔌🔌 Activating plugins'); | ||
| const allPluginInstances = []; | ||
| const globalPlugins = this.config.globalPlugins || []; | ||
| for (let resource of this.config.resources) { | ||
| afLogger.trace(`🔌 Checking plugins for resource: ${resource.resourceId}`); | ||
| for (let pluginInstance of resource.plugins || []) { | ||
| afLogger.trace(`🔌 Found plugin: ${pluginInstance.constructor.name} for resource ${resource.resourceId}`); | ||
| if (pluginInstance.pluginsScope === 'global') { | ||
| throw new Error(formatHugePluginError(`Move plugin ${pluginInstance.constructor.name} to index.ts config.globalPlugins array`)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move plugin ${pluginInstance.constructor.name} to index.ts config.globalPlugins array -> Details: Previously adminforth had only resource-level plugins. To keep project structure clean, in recent version of adminforth we introduced globalPlugins. Global plugins are installed on whole application and not only one resource (like agent, audit logs etc) |
||
| } | ||
| allPluginInstances.push({pi: pluginInstance, resource}); | ||
| } | ||
| } | ||
| allPluginInstances.push(...globalPlugins.map((pluginInstance) => ({pi: pluginInstance, resource: null}))); | ||
| afLogger.trace(`🔌 Total plugins to activate: ${allPluginInstances.length}`); | ||
|
|
||
| let activationLoopCounter = 0; | ||
|
|
@@ -272,8 +277,12 @@ class AdminForth implements IAdminForth { | |
| unactivatedPlugins.forEach( | ||
| ({pi: pluginInstance, resource}, index) => { | ||
| afLogger.trace(`Activating plugin: ${pluginInstance.constructor.name}`); | ||
| afLogger.trace(`🔌 Activating plugin ${index + 1}/${unactivatedPlugins.length}: ${pluginInstance.constructor.name} for resource ${resource.resourceId}`); | ||
| pluginInstance.modifyResourceConfig(this, resource, allPluginInstances); | ||
| afLogger.trace(`🔌 Activating plugin ${index + 1}/${unactivatedPlugins.length}: ${pluginInstance.constructor.name} for resource ${resource ? resource.resourceId : 'global'}`); | ||
| if (pluginInstance.pluginsScope === 'global'){ | ||
| pluginInstance.modifyGlobalConfig(this); | ||
| } else { | ||
| pluginInstance.modifyResourceConfig(this, resource, allPluginInstances); | ||
| } | ||
| afLogger.trace(`🔌 Plugin ${pluginInstance.constructor.name} modifyResourceConfig completed`); | ||
|
|
||
| const plugin = this.activatedPlugins.find((p) => p.pluginInstanceId === pluginInstance.pluginInstanceId); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import CompletionAdapterOpenAIResponses from '../adapters/adminforth-completion-adapter-openai-responses/index.js'; | ||
| import AdminForthAgent from '../plugins/adminforth-agent/index.js'; | ||
| import AdminForthPlugin from '../adminforth/basePlugin.js'; | ||
|
|
||
| const OVH_AI_ENDPOINTS_BASE_URL = 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1'; | ||
| const ovhAiEndpointsAccessToken = process.env.OVH_AI_ENDPOINTS_ACCESS_TOKEN; | ||
| const openAiResponsesApiKey = ovhAiEndpointsAccessToken || process.env.OPENAI_API_KEY; | ||
| const usesOvhAiEndpoints = Boolean(ovhAiEndpointsAccessToken); | ||
|
|
||
| function createAgentCompletionAdapter( | ||
| model: string, | ||
| effort: 'low' | 'medium' | 'xhigh', | ||
| ) { | ||
| return new CompletionAdapterOpenAIResponses({ | ||
| openAiApiKey: openAiResponsesApiKey as string, | ||
| baseUrl: usesOvhAiEndpoints ? OVH_AI_ENDPOINTS_BASE_URL : undefined, | ||
| model: usesOvhAiEndpoints ? 'gpt-oss-120b' : model, | ||
| extraRequestBodyParameters: { | ||
| ...(usesOvhAiEndpoints ? { store: false } : {}), | ||
| reasoning: { | ||
| effort, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export const globalPlugins = [ | ||
| new AdminForthAgent({ | ||
| placeholderMessages: async ({ adminUser, httpExtra }) => { | ||
| return [ | ||
| "What is a cars count in SQLite", | ||
| "Build average car price by days chart in SQLite", | ||
| ] | ||
| }, | ||
| modes: [ | ||
| { | ||
| name: 'Balanced', | ||
| completionAdapter: createAgentCompletionAdapter('gpt-5.4-mini', 'medium'), | ||
| }, | ||
| { | ||
| name: 'Fast', | ||
| completionAdapter: createAgentCompletionAdapter('gpt-5.4-mini', 'low'), | ||
| }, | ||
| { | ||
| name: 'Smart Thinking', | ||
| completionAdapter: createAgentCompletionAdapter('gpt-5.4', 'xhigh'), | ||
| }, | ||
| ], | ||
| maxTokens: 10000, | ||
| reasoning: 'none', | ||
| sessionResource: { | ||
| resourceId: 'sessions', | ||
| idField: 'id', | ||
| titleField: 'title', | ||
| turnsField: 'turns', | ||
| askerIdField: 'asker_id', | ||
| createdAtField: 'created_at', | ||
| }, | ||
| turnResource: { | ||
| resourceId: 'turns', | ||
| idField: 'id', | ||
| sessionIdField: 'session_id', | ||
| createdAtField: 'created_at', | ||
| promptField: 'prompt', | ||
| responseField: 'response', | ||
| debugField: 'dubbug', | ||
| }, | ||
| }), | ||
| ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to make your plugin to be global and add it to the main
index.tsinglobalPluginssection, you need to usepluginsScope: 'global'andmodifyGlobalConfiginstead ofmodifyResourceConfig->
Each plugin should define a scope: resource-level or global level.
globalPluginsof root adminforth config. They might modify configs of many resources like default AuditLog plugin or use several database resources to store data like default Agent plugin.To make your plugin global, you need to use
pluginsScope: 'global'andmodifyGlobalConfiginstead ofmodifyResourceConfig