1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
import { schema } from '../../../../views/nodes/formattedText/schema_rts';
import { v4 as uuidv4 } from 'uuid';
import { gptTutorialAPICall } from '../../../../apis/gpt/TutorialGPT';
import { parsedDoc } from '../chatboxcomponents/ChatBox';
import { Id } from '../../../../../fields/FieldSymbols';
import { Doc } from '../../../../../fields/Doc';
import { RichTextField } from '../../../../../fields/RichTextField';
import { DocumentViewInternal } from '../../DocumentView';
import { Docs } from '../../../../documents/Documents';
import { OpenWhere } from '../../OpenWhere';
import { CollectionFreeFormView } from '../../../collections/collectionFreeForm';
const generateTutorialNodeToolParams = [
{
name: 'query',
type: 'string',
description: 'The user query that asks how to use the environment',
required: true,
},
] as const;
const generateTutorialNodeToolInfo: ToolInfo<typeof generateTutorialNodeToolParams> = {
name: 'generateTutorialNode',
description: "Generates a tutorial text node based on the user's query about Dash functionality. Use this when the user asks for help or tutorials on how to use Dash features.",
parameterRules: generateTutorialNodeToolParams,
citationRules: "No citation needed for this tool's output.",
};
const applyFormatting = (markdownText: string): { doc: any; plainText: string } => {
const lines = markdownText.split('\n');
const nodes: any[] = [];
let plainText = '';
let i = 0;
let currentListItems: any[] = [];
let currentParagraph: any[] = [];
let currentOrderedListItems: any[] = [];
let inOrderedList = false;
let inBulletList = false;
const processBoldText = (text: string) => {
const boldRegex = /\*\*(.*?)\*\*/g;
const parts = [];
let lastIndex = 0;
let match;
while ((match = boldRegex.exec(text)) !== null) {
if (match.index > lastIndex) {
parts.push(schema.text(text.substring(lastIndex, match.index)));
}
parts.push(schema.text(match[1], [schema.marks.strong.create()]));
lastIndex = match.index + match[0].length;
}
if (lastIndex < text.length) {
parts.push(schema.text(text.substring(lastIndex)));
}
return parts.length > 0 ? parts : [schema.text(text)];
};
const flushListItems = () => {
if (currentListItems.length > 0) {
nodes.push(schema.nodes.ordered_list.create({ mapStyle: 'bullet' }, currentListItems));
nodes.push(schema.nodes.paragraph.create());
currentListItems = [];
inBulletList = false;
}
if (currentOrderedListItems.length > 0) {
nodes.push(schema.nodes.ordered_list.create({ mapStyle: 'number' }, currentOrderedListItems));
nodes.push(schema.nodes.paragraph.create());
currentOrderedListItems = [];
inOrderedList = false;
}
};
const flushParagraph = () => {
if (currentParagraph.length > 0) {
nodes.push(schema.nodes.paragraph.create({}, currentParagraph));
currentParagraph = [];
}
};
const processHeader = (line: string) => {
const headerMatch = line.match(/^(#{1,6})\s+(.+)$/);
if (headerMatch) {
const level = Math.min(headerMatch[1].length, 6); // Cap at h6
const textContent = headerMatch[2];
flushParagraph();
nodes.push(schema.nodes.heading.create({ level }, processBoldText(textContent)));
plainText += textContent + '\n';
return true;
}
return false;
};
while (i < lines.length) {
const line = lines[i].trim();
if (line) {
if (processHeader(line)) {
flushListItems();
flushParagraph();
} else if (line.startsWith('- ')) {
flushParagraph();
if (!inBulletList) {
flushListItems();
inBulletList = true;
}
const textContent = line.replace('- ', '');
currentListItems.push(schema.nodes.list_item.create({}, schema.nodes.paragraph.create({}, processBoldText(textContent))));
plainText += textContent + '\n';
} else if (/^\d+\.\s+/.test(line)) {
flushParagraph();
if (!inOrderedList) {
flushListItems();
inOrderedList = true;
}
const textContent = line.replace(/^\d+\.\s+/, '');
currentOrderedListItems.push(schema.nodes.list_item.create({}, schema.nodes.paragraph.create({}, processBoldText(textContent))));
plainText += textContent + '\n';
} else {
flushListItems();
currentParagraph = currentParagraph.concat(processBoldText(line));
plainText += line + '\n';
}
} else {
flushListItems();
flushParagraph();
nodes.push(schema.nodes.paragraph.create());
plainText += '\n';
}
i++;
}
flushListItems();
flushParagraph();
const doc = schema.nodes.doc.create({}, nodes);
return { doc, plainText: plainText.trim() };
};
export class GPTTutorialTool extends BaseTool<typeof generateTutorialNodeToolParams> {
private _createDocInDash: (doc: parsedDoc) => Doc | undefined;
constructor(createDocInDash: (doc: parsedDoc) => Doc | undefined) {
super(generateTutorialNodeToolInfo);
this._createDocInDash = createDocInDash;
}
async execute(args: ParametersType<typeof generateTutorialNodeToolParams>): Promise<Observation[]> {
const chunkId = uuidv4();
try {
const query = (args.query || '').trim();
if (!query) {
return [{ type: 'text', text: `<chunk chunk_id="${chunkId}" chunk_type="error">Please provide a query.</chunk>` }];
}
const markdown = await gptTutorialAPICall(query);
const { doc, plainText } = applyFormatting(markdown);
// Build the ProseMirror‐in‐JSON + plain-text for RichTextField
const rtfData = {
doc: (doc as any).toJSON ? (doc as any).toJSON() : doc,
selection: { type: 'text', anchor: 0, head: 0 },
storedMarks: [],
};
const rtf = new RichTextField(JSON.stringify(rtfData), plainText);
// Create and show the TextDocument directly:
const formattedDoc = Docs.Create.TextDocument(rtf, {
title: 'Tutorial Node',
_width: 600,
_layout_fitWidth: true,
_layout_autoHeight: true,
text_fontSize: '16px',
});
DocumentViewInternal.addDocTabFunc(formattedDoc, OpenWhere.addRight);
// If user asked about linking/pinning/presentation, also fire the in-app tutorial:
const q = query.toLowerCase();
if (q.includes('link')) {
Doc.IsInfoUIDisabled = false;
CollectionFreeFormView.showTutorial('links');
} else if (q.includes('presentation')) {
Doc.IsInfoUIDisabled = false;
CollectionFreeFormView.showTutorial('presentation');
} else if (q.includes('pin')) {
Doc.IsInfoUIDisabled = false;
CollectionFreeFormView.showTutorial('pins');
}
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="tutorial_node_creation">Created tutorial node with ID ${formattedDoc[Id]}.</chunk>`,
},
];
} catch (error) {
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">Error generating tutorial node: ${error}</chunk>`,
},
];
}
}
}
|