Line data Source code
1 : import type { SkillReference } from '@/workbench/extensions/agent/types/skillReference'
2 : import type { WorkflowReference } from '@/workbench/extensions/agent/types/workflowReference'
3 :
4 0 : export function promptReferenceParts(
5 : text: string,
6 : workflows: WorkflowReference[],
7 : skill?: SkillReference
8 : ) {
9 0 : const references: (
10 : | { type: 'workflow'; reference: WorkflowReference }
11 : | { type: 'skill'; reference: SkillReference }
12 : )[] = workflows.map((reference) => ({ type: 'workflow', reference }))
13 0 : if (skill)
14 0 : references.splice(skill.workflowIndex ?? workflows.length, 0, {
15 : type: 'skill',
16 : reference: skill
17 : })
18 0 : references.sort((a, b) => a.reference.textOffset - b.reference.textOffset)
19 0 : const parts: (
20 : | { type: 'text'; text: string }
21 : | (typeof references)[number]
22 : )[] = []
23 0 : let offset = 0
24 0 : for (const item of references) {
25 0 : const next = Math.max(
26 : offset,
27 : Math.min(text.length, item.reference.textOffset)
28 : )
29 0 : if (next > offset)
30 0 : parts.push({ type: 'text', text: text.slice(offset, next) })
31 0 : parts.push(item)
32 0 : offset = next
33 : }
34 0 : if (offset < text.length)
35 0 : parts.push({ type: 'text', text: text.slice(offset) })
36 0 : return parts
37 : }
|