AML Template Management Document

AML Template Management Document

1. Template Storage

1.1 Database Tables

File location: /app/lib/db/index.ts

``typescript

// AML模板表

database.exec(

CREATE TABLE IF NOT EXISTS \"AmlTemplate\" (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT UNIQUE NOT NULL,

type TEXT NOT NULL,

item_type TEXT NOT NULL,

template TEXT NOT NULL,

description TEXT,

parameters TEXT,

created_at TEXT DEFAULT CURRENT_TIMESTAMP,

updated_at TEXT DEFAULT CURRENT_TIMESTAMP

)

);

// AML对象表

database.exec(

CREATE TABLE IF NOT EXISTS \"AmlObject\" (

id INTEGER PRIMARY KEY AUTOINCREMENT,

item_type TEXT NOT NULL,

name TEXT,

aml TEXT NOT NULL,

schema TEXT,

created_at TEXT DEFAULT CURRENT_TIMESTAMP,

updated_at TEXT DEFAULT CURRENT_TIMESTAMP

)

);

`

1.2 Table Structures

AmlTemplate Table

| Field | Type | Description |

|------|------|------|

| id | INTEGER | Primary key |

| name | TEXT | Template name |

| type | TEXT | Operation type (get/add/edit/delete) |

| item_type | TEXT | Object type |

| template | TEXT | AML template |

| description | TEXT | Description |

| parameters | TEXT | Parameter definition (JSON) |

AmlObject Table

| Field | Type | Description |

|------|------|------|

| id | INTEGER | Primary key |

| item_type | TEXT | Object type |

| name | TEXT | Object name |

| aml | TEXT | AML content |

| schema | TEXT | Object schema (JSON) |

2. Template API

2.1 Management API

File location: /app/api/aml/templates/route.ts

`typescript

import { NextRequest, NextResponse } from 'next/server';

import { getDatabase } from '@/app/lib/db';

// GET /api/aml/templates?type=get&item_type=ECR

export async function GET(request: NextRequest) {

const { searchParams } = new URL(request.url);

const type = searchParams.get('type');

const itemType = searchParams.get('item_type');

const db = getDatabase();

let sql = 'SELECT * FROM "AmlTemplate" WHERE 1=1';

const params: any[] = [];

if (type) {

sql += ' AND type = ?';

params.push(type);

}

if (itemType) {

sql += ' AND item_type = ?';

params.push(itemType);

}

const templates = db.prepare(sql).all(...params);

return NextResponse.json({

success: true,

data: templates

});

}

// POST /api/aml/templates

export async function POST(request: NextRequest) {

const { name, type, item_type, template, description, parameters } = await request.json();

if (!name || !type || !item_type || !template) {

return NextResponse.json(

{ success: false, message: 'Missing required fields' },

{ status: 400 }

);

}

const db = getDatabase();

const result = db.prepare(

INSERT INTO "AmlTemplate" (name, type, item_type, template, description, parameters)

VALUES (?, ?, ?, ?, ?, ?)

).run(name, type, item_type, template, description || '', parameters || '{}');

return NextResponse.json({

success: true,

data: { id: result.lastInsertRowid }

});

}

`

2.2 Object API

File location: /app/api/aml/objects/route.ts

`typescript

import { NextRequest, NextResponse } from 'next/server';

import { getDatabase } from '@/app/lib/db';

// GET /api/aml/objects

export async function GET(request: NextRequest) {

const { searchParams } = new URL(request.url);

const itemType = searchParams.get('item_type');

const db = getDatabase();

let sql = 'SELECT * FROM "AmlObject"';

const params: any[] = [];

if (itemType) {

sql += ' WHERE item_type = ?';

params.push(itemType);

}

const objects = db.prepare(sql).all(...params);

return NextResponse.json({

success: true,

data: objects

});

}

// POST /api/aml/objects (batch import)

export async function POST(request: NextRequest) {

const { items } = await request.json();

if (!items || !Array.isArray(items)) {

return NextResponse.json(

{ success: false, message: 'Invalid items' },

{ status: 400 }

);

}

const db = getDatabase();

let imported = 0;

let updated = 0;

const insert = db.prepare(

INSERT OR REPLACE INTO "AmlObject" (item_type, name, aml, schema)

VALUES (?, ?, ?, ?)

);

for (const item of items) {

const result = insert.run(item.item_type, item.name || '', item.aml, item.schema || '{}');

if (result.changes > 0) {

imported++;

}

}

return NextResponse.json({

success: true,

data: { imported, updated }

});

}

`

3. Template Usage

3.1 Template Rendering

`typescript

// File location: /app/lib/aml/render.ts

export function renderTemplate(template: string, params: Record): string {

let result = template;

// Replace simple variable {name}

Object.keys(params).forEach(key => {

const value = params[key];

if (value !== undefined && value !== null) {

result = result.replace(new RegExp(\\{${key}\\}, 'g'), value);

}

});

// Replace condition variable {condition?:}

result = result.replace(/\{(\w+)\?([^:]+):([^}]+)\}/g, (match, condition, truePart, falsePart) => {

return params[condition] ? truePart : falsePart;

});

return result;

}

// Usage example

const template = '{status?{status}:}';

const rendered = renderTemplate(template, { status: 'pending_approval' });

// Result: pending_approval

`

3.2 Template Execution

`typescript

// File location: /app/lib/aml/execute.ts

import { renderTemplate } from './render';

import { executeAmlWithFallback } from '../SCSAI/fallback';

export async function executeTemplate(

templateId: number,

params: Record

): Promise {

const db = getDatabase();

const template = db.prepare('SELECT * FROM "AmlTemplate" WHERE id = ?').get(templateId) as any;

if (!template) {

throw new Error('Template not found');

}

const renderedAml = renderTemplate(template.template, params);

return await executeAmlWithFallback(renderedAml, template.item_type);

}

`

4. Quick Creation Example

4.1 Creating the Part Object Class

`typescript

// Generate AML via the large model

const prompt = Generate AML for creating a car Part:

Object type: Part

Fields:

  • part_number: PART-CAR-001
  • name: Car
  • description: A complete car
  • vehicle_type: SUV
  • brand: Mercedes-Benz
  • model: GLC-300
  • color: Black
  • price: 500000;

const result = await fetch('/api/ai-proxy', {

method: 'POST',

headers: { 'Content-Type': 'application/json' },

body: JSON.stringify({ messages: [{ role: 'user', content: prompt }] })

});

// Parse AML and save

const aml = parseAmlResult(result.data);

await saveAmlObject('Part', aml, {

name: '汽车',

schema: { fields: [...] }

});

`

4.2 Save to Database

`typescript

// Save object definition

db.prepare(

INSERT INTO "AmlObject" (item_type, name, aml, schema)

VALUES (?, ?, ?, ?)

).run('Part', '汽车', aml, JSON.stringify(schema));

``

5. File Location Summary

FunctionFile Location
Database initialization/app/lib/db/index.ts
Template API/app/api/aml/templates/route.ts
Object API/app/api/aml/objects/route.ts
Template rendering/app/lib/aml/render.ts
Template execution/app/lib/aml/execute.ts
← 返回案例列表

左帮右臂工业智能体,把刚读到的方法跑起来

微信扫小程序、打开网页版 ylxt.chat,或下载桌面客户端,三种方式任选,马上用上。

🌐 网页版 ylxt.chat 💻 桌面版下载 📞 电话咨询
左帮右臂小程序码
微信扫码,体验小程序
分享:
🤖 Try Now →
🤖
🎁