This commit is contained in:
2025-12-12 16:41:31 +08:00
parent 32db5ec306
commit 9cd1de87dd

View File

@@ -3,6 +3,7 @@ import { NocoLifeService } from './services/life.ts';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { useContextKey } from '@kevisual/context'; import { useContextKey } from '@kevisual/context';
import { BaseChat, AIUtils } from '@kevisual/ai'; import { BaseChat, AIUtils } from '@kevisual/ai';
import { pick } from 'es-toolkit'
app.route({ app.route({
path: 'noco-life', path: 'noco-life',
key: 'config-update', key: 'config-update',
@@ -18,17 +19,20 @@ app.route({
const config = await nocoLifeService.getLifeConfig() const config = await nocoLifeService.getLifeConfig()
if (question) { if (question) {
const ai: BaseChat = useContextKey('ai'); const ai: BaseChat = useContextKey('ai');
const originConfig = JSON.stringify(config); const pickData = pick(config, ['baseURL', 'token', 'baseId', 'tableId']);
const originConfig = JSON.stringify(pickData);
await ai.chat([ await ai.chat([
{ role: 'system', content: `你是一个多维表格配置助理,你的任务是帮助用户解析多维表格的配置信息。用户会提供配置信息,你需要从中提取出 baseURL, token, baseId, tableId 等字段,并以 JSON 格式返回。如果某个字段缺失,可以不返回该字段。请确保返回的 JSON 格式正确且易于解析。` }, {
role: 'system', content: `你是一个多维表格配置助理,你的任务是帮助用户解析多维表格的配置信息。用户会提供配置信息,你需要从中提取出 baseURL, token, baseId, tableId 等字段,并以 JSON 格式返回。如果某个字段缺失,可以不返回该字段。请确保返回的 JSON 格式正确且易于解析。
返回的数据例子: { "baseURL": "https://example.com", "token": "abc123", "baseId": "base_01", "tableId": "table_01" }` },
{ role: 'user', content: `当前已有的多维表格配置信息是: ${originConfig}` },
{ role: 'user', content: question }, { role: 'user', content: question },
{ role: 'user', content: `当前已有的多维表格配置信息是: ${originConfig}` }
]) ])
let msg = AIUtils.extractJsonFromMarkdown(ai.responseText || ''); let msg = AIUtils.extractJsonFromMarkdown(ai.responseText || '');
if (msg == null) { if (msg == null) {
ctx.throw(500, 'AI 返回结果解析失败'); ctx.throw(500, 'AI 返回结果解析失败');
} }
data = msg; data = { ...data, ...config, ...msg };
} }
if (!data?.baseURL || !data?.token || !data?.baseId) { if (!data?.baseURL || !data?.token || !data?.baseId) {
ctx.throw(400, '缺少参数 baseURL, token, baseId, tableId'); ctx.throw(400, '缺少参数 baseURL, token, baseId, tableId');