@adminforth/bulk-ai-flow 1.0.1

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/index.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { AdminForthPlugin, Filters } from "adminforth";
2
+ import type { IAdminForth, IHttpServer, AdminForthResourcePages, AdminForthResourceColumn, AdminForthDataTypes, AdminForthResource } from "adminforth";
3
+ import type { PluginOptions } from './types.js';
4
+ import { json } from "stream/consumers";
5
+ import Handlebars from 'handlebars';
6
+
7
+
8
+ export default class BulkVisionPlugin extends AdminForthPlugin {
9
+ options: PluginOptions;
10
+
11
+ constructor(options: PluginOptions) {
12
+ super(options, import.meta.url);
13
+ this.options = options;
14
+ }
15
+
16
+ // Compile Handlebars templates in outputFields using record fields as context
17
+ private compileOutputFieldsTemplates(record: any): Record<string, string>[] {
18
+ return this.options.outputFields.map((fieldObj) => {
19
+ const compiled: Record<string, string> = {};
20
+ for (const [key, templateStr] of Object.entries(fieldObj)) {
21
+ try {
22
+ const tpl = Handlebars.compile(String(templateStr));
23
+ compiled[key] = tpl(record);
24
+ } catch {
25
+ compiled[key] = String(templateStr);
26
+ }
27
+ }
28
+ return compiled;
29
+ });
30
+ }
31
+
32
+ async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
33
+ super.modifyResourceConfig(adminforth, resourceConfig);
34
+
35
+ //check if options names are provided
36
+ const columns = this.resourceConfig.columns;
37
+ let columnEnums = [];
38
+ for (const field of this.options.outputFields) {
39
+ for (const [key, value] of Object.entries(field)) {
40
+ const column = columns.find(c => c.name.toLowerCase() === key.toLowerCase());
41
+ if (column) {
42
+ if(column.enum){
43
+ (field as any)[key] = `${value} Select ${key} from the list (USE ONLY VALUE FIELD. USE ONLY VALUES FROM THIS LIST): ${JSON.stringify(column.enum)}`;
44
+ columnEnums.push({
45
+ name: key,
46
+ enum: column.enum,
47
+ });
48
+ }
49
+ } else {
50
+ throw new Error(`⚠️ No column found for key "${key}"`);
51
+ }
52
+ }
53
+ }
54
+
55
+ const pageInjection = {
56
+ file: this.componentPath('visionAction.vue'),
57
+ meta: {
58
+ pluginInstanceId: this.pluginInstanceId,
59
+ outputFields: this.options.outputFields,
60
+ actionName: this.options.actionName,
61
+ confirmMessage: this.options.confirmMessage,
62
+ columnEnums: columnEnums,
63
+ }
64
+ }
65
+
66
+ if (!this.resourceConfig.options.pageInjections) {
67
+ this.resourceConfig.options.pageInjections = {};
68
+ }
69
+ if (!this.resourceConfig.options.pageInjections.list) {
70
+ this.resourceConfig.options.pageInjections.list = {};
71
+ }
72
+
73
+ this.resourceConfig.options.pageInjections.list.threeDotsDropdownItems = [pageInjection];
74
+ }
75
+
76
+ validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
77
+ // optional method where you can safely check field types after database discovery was performed
78
+ }
79
+
80
+ instanceUniqueRepresentation(pluginOptions: any) : string {
81
+ // optional method to return unique string representation of plugin instance.
82
+ // Needed if plugin can have multiple instances on one resource
83
+ return `single`;
84
+ }
85
+
86
+ setupEndpoints(server: IHttpServer) {
87
+ server.endpoint({
88
+ method: 'POST',
89
+ path: `/plugin/${this.pluginInstanceId}/analyze`,
90
+ handler: async ({ body, adminUser, headers }) => {
91
+ const selectedIds = body.selectedIds || [];
92
+ const tasks = selectedIds.map(async (ID) => {
93
+ // Fetch the record using the provided ID
94
+ const record = await this.adminforth.resource(this.resourceConfig.resourceId).get( [Filters.EQ('id', ID)] );
95
+
96
+ //recieve image URLs to analyze
97
+ const attachmentFiles = await this.options.attachFiles({ record: record });
98
+ //create prompt for OpenAI
99
+ const compiledOutputFields = this.compileOutputFieldsTemplates(record);
100
+ const prompt = `Analyze the following image(s) and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
101
+ Do NOT return array of objects. Do NOT include any Markdown, code blocks, explanations, or extra text. Only return valid JSON.
102
+ Each object must contain the following fields: ${JSON.stringify(compiledOutputFields)} Use the exact field names. If it's number field - return only number.
103
+ Image URLs:`;
104
+
105
+ //send prompt to OpenAI and get response
106
+ const chatResponse = await this.options.adapter.generate({ prompt, inputFileUrls: attachmentFiles });
107
+
108
+ const resp: any = (chatResponse as any).response;
109
+ const topLevelError = (chatResponse as any).error;
110
+ if (topLevelError || resp?.error) {
111
+ throw new Error(`ERROR: ${JSON.stringify(topLevelError || resp?.error)}`);
112
+ }
113
+
114
+ const textOutput = resp?.output?.[0]?.content?.[0]?.text ?? resp?.output_text ?? resp?.choices?.[0]?.message?.content;
115
+ if (!textOutput || typeof textOutput !== 'string') {
116
+ throw new Error('Unexpected AI response format');
117
+ }
118
+
119
+ //parse response and update record
120
+ const resData = JSON.parse(textOutput);
121
+
122
+ return resData;
123
+ });
124
+
125
+ const result = await Promise.all(tasks);
126
+
127
+ return { result };
128
+ }
129
+ });
130
+ server.endpoint({
131
+ method: 'POST',
132
+ path: `/plugin/${this.pluginInstanceId}/get_records`,
133
+ handler: async ( body ) => {
134
+ let records = [];
135
+ for( const record of body.body.record ) {
136
+ records.push(await this.adminforth.resource(this.resourceConfig.resourceId).get( [Filters.EQ('id', record)] ));
137
+ records[records.length - 1]._label = this.resourceConfig.recordLabel(records[records.length - 1]);
138
+ }
139
+ return {
140
+ records,
141
+ };
142
+ }
143
+ });
144
+ server.endpoint({
145
+ method: 'POST',
146
+ path: `/plugin/${this.pluginInstanceId}/get_images`,
147
+ handler: async ( body ) => {
148
+ let images = [];
149
+ if(body.body.record){
150
+ for( const record of body.body.record ) {
151
+ images.push(await this.options.attachFiles({ record: record }));
152
+ }
153
+ }
154
+ return {
155
+ images,
156
+ };
157
+ }
158
+ });
159
+ server.endpoint({
160
+ method: 'POST',
161
+ path: `/plugin/${this.pluginInstanceId}/update_fields`,
162
+ handler: async ( body ) => {
163
+ const selectedIds = body.body.selectedIds || [];
164
+ const fieldsToUpdate = body.body.fields || {};
165
+ const updates = selectedIds.map((ID, idx) =>
166
+ this.adminforth
167
+ .resource(this.resourceConfig.resourceId)
168
+ .update(ID, fieldsToUpdate[idx])
169
+ );
170
+
171
+ await Promise.all(updates);
172
+
173
+ return { ok: true };
174
+ }
175
+ });
176
+ }
177
+ }
178
+
179
+
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@adminforth/bulk-ai-flow",
3
+ "version": "1.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "type": "module",
10
+ "scripts": {
11
+ "build": "tsc && rsync -av --exclude 'node_modules' custom dist/"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": "",
17
+ "devDependencies": {
18
+ "@types/node": "latest",
19
+ "typescript": "^5.7.3"
20
+ },
21
+ "dependencies": {
22
+ "@types/handlebars": "^4.0.40",
23
+ "adminforth": "^2.4.0-next.101",
24
+ "handlebars": "^4.7.8"
25
+ }
26
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include*/
4
+ "module": "node16", /* Specify what module code is generated. */
5
+ "outDir": "./dist", /* Specify an output folder for all emitted files. */
6
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. */
7
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
8
+ "strict": false, /* Enable all strict type-checking options. */
9
+ "skipLibCheck": true, /* Skip type checking all .d.ts files. */
10
+ },
11
+ "exclude": ["node_modules", "dist", "custom"], /* Exclude files from compilation. */
12
+ }
13
+
package/types.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { ImageVisionAdapter, AdminUser, IAdminForth, StorageAdapter } from "adminforth";
2
+
3
+
4
+ export interface PluginOptions {
5
+ actionName: string,
6
+ confirmMessage: string,
7
+ adapter: ImageVisionAdapter,
8
+ outputFields: Record<string, string>[],
9
+ attachFiles?: ({ record }: {
10
+ record: any,
11
+ }) => string[] | Promise<string[]>,
12
+ }