SCSAI 调用流程文档

SCSAI 调用流程文档

一、SCSAI 调用完整流程

1.1 初始化流程(前端)

文件位置/app/lib/SCSAI/client.ts

``typescript

// 加载SCSAI JS库

export async function loadSCSAIJs(): Promise {

return new Promise((resolve, reject) => {

// 检查是否已加载

if (typeof window !== 'undefined' && (window as any).sciot) {

console.log('SCSAI already loaded');

resolve(true);

return;

}

const script = document.createElement('script');

script.src = '/Scripts/SCSAI.agent.js';

script.onload = () => {

try {

const agent = new (window as any).agent();

(window as any).sciot = agent;

console.log('SCSAI initialized successfully');

resolve(true);

} catch (error) {

console.error('Failed to initialize SCSAI:', error);

reject(error);

}

};

script.onerror = (error) => {

console.error('Failed to load SCSAI script:', error);

reject(new Error('Failed to load SCSAI script'));

};

document.head.appendChild(script);

});

}

// 初始化SCSAI

export async function initSCSAI(): Promise {

try {

await loadSCSAIJs();

const isLoggedIn = await (window as any).sciot.isLoggedIn();

if (isLoggedIn) {

const user = await (window as any).sciot.getUser();

console.log('SCSAI User:', user);

return true;

} else {

console.log('Not logged in to SCSAI');

return false;

}

} catch (error) {

console.error('SCSAI initialization error:', error);

return false;

}

}

// 获取全局sciot对象

export function getSciot() {

if (typeof window !== 'undefined' && (window as any).sciot) {

return (window as any).sciot;

}

throw new Error('SCSAI not initialized. Call initSCSAI() first.');

}

`

1.2 AML操作流程

文件位置/app/lib/SCSAI/aml.ts

`typescript

import { getSciot } from './client';

// 执行AML

export async function executeAml(aml: string): Promise<{ success: boolean; data?: any; error?: string }> {

try {

console.log('Executing AML:', aml);

const sciot = getSciot();

const result = await sciot.applyAML(aml);

console.log('AML result:', result);

return { success: true, data: result };

} catch (error: any) {

console.error('AML execution error:', error);

return { success: false, error: error.message };

}

}

// 解析AML结果为JSON

export function parseAmlResult(xmlString: string): any[] {

const parser = new DOMParser();

const xmlDoc = parser.parseFromString(xmlString, 'text/xml');

// 检查解析错误

const parserError = xmlDoc.querySelector('parsererror');

if (parserError) {

throw new Error('XML parsing error: ' + parserError.textContent);

}

// 检查AML错误

const errorElement = xmlDoc.querySelector('Error');

if (errorElement) {

throw new Error('AML error: ' + errorElement.textContent);

}

// 提取Item元素

const items: any[] = [];

xmlDoc.querySelectorAll('Item').forEach(item => {

const obj: any = {};

// 提取属性

if (item.getAttribute('type')) obj._type = item.getAttribute('type');

if (item.getAttribute('id')) obj._id = item.getAttribute('id');

// 提取子元素

item.querySelectorAll(':scope > *').forEach(child => {

obj[child.tagName] = child.textContent;

});

items.push(obj);

});

return items;

}

// 带解析的AML执行

export async function executeAndParseAml(aml: string): Promise<{ success: boolean; data?: any[]; error?: string }> {

const result = await executeAml(aml);

if (!result.success) {

return result;

}

try {

const items = parseAmlResult(result.data);

return { success: true, data: items };

} catch (error: any) {

return { success: false, error: error.message };

}

}

`

1.3 降级策略

文件位置/app/lib/SCSAI/fallback.ts

`typescript

import { executeAndParseAml } from './aml';

import { getLocalData } from '../db/local-data';

// 带降级策略的AML操作

export async function executeAmlWithFallback(

aml: string,

itemType: string,

localKey?: string

): Promise<{ success: boolean; data?: any[]; error?: string; source: 'SCSAI' | 'local' }> {

try {

// 1. 尝试SCSAI调用

const result = await executeAndParseAml(aml);

if (result.success && result.data && result.data.length > 0) {

console.log('Using SCSAI data for', itemType);

return { ...result, source: 'SCSAI' };

}

} catch (error: any) {

console.warn('SCSAI call failed:', error.message);

}

// 2. 降级到本地数据

console.log('Using local data for', itemType);

const localData = localKey ? getLocalData(itemType, localKey) : getLocalData(itemType);

return {

success: true,

data: localData,

source: 'local'

};

}

`

二、调用流程总结

`

  1. loadSCSAIJs() → 加载 /Scripts/SCSAI.agent.js
  2. new agent() → 创建agent实例
  3. window.sciot → 设置全局对象
  4. sciot.applyAML() → 执行AML操作
  5. parseAmlResult() → 解析XML结果
  6. 降级策略 → SCSAI失败时使用本地数据

`

三、快速验证

在浏览器控制台执行:

`javascript

// 1. 加载SCSAI

fetch('/Scripts/SCSAI.agent.js').then(r => r.text()).then(s => eval(s));

// 2. 创建实例

const agent = new agent();

// 3. 设置全局对象

window.sciot = agent;

// 4. 测试查询

window.sciot.applyAML('')

.then(r => console.log(r))

.catch(e => console.error(e));

``

四、关键文件位置

文件位置用途
SCSAI-client.ts/app/lib/SCSAI/client.tsSCSAI初始化和连接
SCSAI-aml.ts/app/lib/SCSAI/aml.tsAML执行和解析
SCSAI-fallback.ts/app/lib/SCSAI/fallback.ts降级策略
SCSAI-connection.ts/app/lib/SCSAI/connection.tsSCSAI连接配置
← 返回案例列表
分享:
🤖 Try Now →
🤖
🎁