Line data Source code
1 : import type {
2 : ComposerInsertionPoint,
3 : ComposerPrompt,
4 : ComposerReference
5 : } from '@/workbench/extensions/agent/types/composerPrompt'
6 : import {
7 : composerReferenceKey,
8 : composerReferenceName
9 : } from '@/workbench/extensions/agent/types/composerPrompt'
10 : import type { PromptSnapshot } from '@/workbench/extensions/agent/types/workflowReference'
11 : import { assetReferenceText, nodeReferenceText } from './agentMessageText'
12 : import { promptReferenceParts } from './promptReferenceParts'
13 : import { serializeSkillReference } from './skillReferenceText'
14 :
15 0 : export function insertComposerReference(
16 : prompt: ComposerPrompt,
17 : reference: ComposerReference,
18 : insertion: ComposerInsertionPoint
19 : ): { prompt: ComposerPrompt; insertion: ComposerInsertionPoint } {
20 0 : const textOffset = Math.min(
21 : prompt.text.length,
22 : Math.max(0, insertion.textOffset)
23 : )
24 0 : const referenceIndex = Math.min(
25 : prompt.references.length,
26 : Math.max(0, insertion.referenceIndex)
27 : )
28 0 : const needsSpace = prompt.text[textOffset] !== ' '
29 0 : const references = prompt.references.map((item, index) => ({
30 : ...item,
31 : textOffset:
32 : needsSpace &&
33 : (item.textOffset > textOffset ||
34 : (item.textOffset === textOffset && index >= referenceIndex))
35 : ? item.textOffset + 1
36 : : item.textOffset
37 : }))
38 0 : references.splice(referenceIndex, 0, { ...reference, textOffset })
39 0 : return {
40 : prompt: {
41 : text: needsSpace
42 : ? `${prompt.text.slice(0, textOffset)} ${prompt.text.slice(textOffset)}`
43 : : prompt.text,
44 : references
45 : },
46 : insertion: {
47 : textOffset: textOffset + 1,
48 : referenceIndex: referenceIndex + 1
49 : }
50 : }
51 : }
52 :
53 0 : export function composerPromptForSend(prompt: ComposerPrompt): PromptSnapshot {
54 0 : let text = ''
55 0 : let offset = 0
56 0 : const workflowReferences: PromptSnapshot['workflowReferences'] = []
57 0 : let skillReference: PromptSnapshot['skillReference']
58 0 : for (const reference of prompt.references) {
59 0 : text += prompt.text.slice(offset, reference.textOffset)
60 0 : offset = reference.textOffset
61 0 : if (reference.kind === 'workflow') {
62 0 : const { kind: _kind, ...workflow } = reference
63 0 : workflowReferences.push({ ...workflow, textOffset: text.length })
64 0 : } else if (reference.kind === 'skill') {
65 0 : const {
66 : kind: _kind,
67 : scope: _scope,
68 : resolvePastedName: _resolvePastedName,
69 : ...skill
70 : } = reference
71 0 : skillReference = {
72 : ...skill,
73 : textOffset: text.length,
74 : workflowIndex: workflowReferences.length
75 : }
76 0 : } else if (reference.kind !== 'error') {
77 0 : const name = composerReferenceName(reference)
78 0 : text +=
79 : reference.kind === 'node'
80 : ? nodeReferenceText(name)
81 : : assetReferenceText(name)
82 : }
83 : }
84 0 : return {
85 : text: text + prompt.text.slice(offset),
86 : workflowReferences,
87 : ...(skillReference ? { skillReference } : {})
88 : }
89 : }
90 :
91 0 : export function sameComposerReferenceOrder(
92 : a: ComposerPrompt,
93 : b: ComposerPrompt
94 : ): boolean {
95 0 : return (
96 : a.references.length === b.references.length &&
97 : a.references.every(
98 : (reference, index) =>
99 : composerReferenceKey(reference) ===
100 : composerReferenceKey(b.references[index])
101 : )
102 : )
103 : }
104 :
105 0 : export function composerPromptForSubmission(
106 : prompt: ComposerPrompt
107 : ): Pick<PromptSnapshot, 'text' | 'workflowReferences'> {
108 0 : const snapshot = composerPromptForSend(prompt)
109 0 : let text = ''
110 0 : const workflowReferences: PromptSnapshot['workflowReferences'] = []
111 0 : for (const part of promptReferenceParts(
112 : snapshot.text,
113 : snapshot.workflowReferences,
114 : snapshot.skillReference
115 : )) {
116 0 : if (part.type === 'text') text += part.text
117 0 : else if (part.type === 'skill')
118 0 : text += serializeSkillReference(part.reference)
119 0 : else workflowReferences.push({ ...part.reference, textOffset: text.length })
120 : }
121 0 : return { text, workflowReferences }
122 : }
|