84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
import { neode } from '@/app.ts';
|
|
import { getSession } from '@/modules/neo4j.ts';
|
|
import Neode from 'neode';
|
|
|
|
export const PromptNeo = neode.model('Prompt', {
|
|
id: {
|
|
type: 'uuid',
|
|
primary: true,
|
|
},
|
|
title: {
|
|
type: 'string',
|
|
},
|
|
description: 'string',
|
|
// profile: { type: 'object', optional: true }, // 用于存储 JSON 对象
|
|
prompt: 'string',
|
|
// inputVariables: { type: 'array', item },
|
|
// tags: { type: 'array', items: 'string', optional: true } // 定义字符串数组
|
|
inputVariables: { type: 'string', default: JSON.stringify([]) },
|
|
localVariables: { type: 'string', default: JSON.stringify([]) },
|
|
|
|
// 定义可单向或双向的关系
|
|
relatedPrompts: {
|
|
type: 'relationship',
|
|
relationship: 'RELATED_TO',
|
|
target: 'Prompt', // 指向自身
|
|
direction: 'out', // 默认是单向的
|
|
properties: {
|
|
created_at: 'datetime',
|
|
bidirectional: 'boolean', // 用来标记该关系是否为双向
|
|
},
|
|
eager: true, // 自动加载相关的 Prompts
|
|
},
|
|
});
|
|
export async function createRelationship(promptA: Neode.Node<unknown>, promptB: Neode.Node<unknown>, isBidirectional = false) {
|
|
// 创建单向关系
|
|
await promptA.relateTo(promptB, 'RELATED_TO', { created_at: new Date(), bidirectional: isBidirectional });
|
|
|
|
// 如果是双向关系,创建反向关系
|
|
if (isBidirectional) {
|
|
await promptB.relateTo(promptA, 'RELATED_TO', { created_at: new Date(), bidirectional: true });
|
|
}
|
|
}
|
|
export async function createRelationship2(promptId1, promptId2, isBidirectional = false) {
|
|
const query = `
|
|
MATCH (p1:Prompt {id: $id1}), (p2:Prompt {id: $id2})
|
|
CREATE (p1)-[r:RELATED_TO {created_at: $createdAt, bidirectional: $bidirectional}]->(p2)
|
|
RETURN r
|
|
`;
|
|
|
|
const result = await getSession().run(query, {
|
|
id1: promptId1,
|
|
id2: promptId2,
|
|
createdAt: new Date().toISOString(),
|
|
bidirectional: isBidirectional,
|
|
});
|
|
|
|
return result.records[0].get('r');
|
|
}
|
|
export async function createPrompt(promptData) {
|
|
const session = getSession();
|
|
const query = `
|
|
CREATE (p:Prompt {
|
|
id: $id,
|
|
title: $title,
|
|
description: $description,
|
|
prompt: $prompt,
|
|
inputVariables: $inputVariables,
|
|
localVariables: $localVariables
|
|
})
|
|
RETURN p
|
|
`;
|
|
|
|
const result = await session.run(query, {
|
|
id: promptData.id,
|
|
title: promptData.title,
|
|
description: promptData.description,
|
|
prompt: promptData.prompt,
|
|
inputVariables: JSON.stringify(promptData.inputVariables || []),
|
|
localVariables: JSON.stringify(promptData.localVariables || []),
|
|
});
|
|
|
|
return result.records[0].get('p');
|
|
}
|