Changesets · Branches · Tags · Compare
| Changeset | 409299863087b08d1a7a4d745c6f065a6aabfc66807910a61837c2a91c27ac2e |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-06-27 17:17 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-06-27 17:17 -07:00 |
| Parents | e40aa61402d6 |
11 files changed
modified core/commands/util.ts +1 −1
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | return { |
| 17 | 17 | content: rif.contents, |
| 18 | 18 | name: `${basename} ${rangeStr}`, |
| 19 | - description: `${relativePathOrBasename} ${rangeStr}`, // This is passed to the LLM - do not pass full URI | |
| 19 | + description: `${relativePathOrBasename} ${rangeStr}`, | |
| 20 | 20 | id: { |
| 21 | 21 | providerTitle: "code", |
| 22 | 22 | itemId: uuidv4(), |
modified core/util/handlebars/handlebarUtils.ts +1 −1
| @@ -90,7 +90,7 @@ | ||
| 90 | 90 | } |
| 91 | 91 | } catch (e) { |
| 92 | 92 | console.error(`Error reading file in prompt file ${filepath}:`, e); |
| 93 | - templateData[letter] = `[Error reading file ${filepath}]`; | |
| 93 | + templateData[letter] = `[Error reading file "${filepath}"]`; | |
| 94 | 94 | } |
| 95 | 95 | } |
| 96 | 96 |
added gui/src/components/mainInput/TipTapEditor/utils/processEditorContent.test.ts +536 −0
| @@ -0,0 +1,536 @@ | ||
| 1 | +import { JSONContent } from "@tiptap/react"; | |
| 2 | +import { ContextItemWithId } from "core"; | |
| 3 | +import { expect, test, vi } from "vitest"; | |
| 4 | +import { processEditorContent } from "./processEditorContent"; | |
| 5 | + | |
| 6 | +describe("processEditorContent", () => { | |
| 7 | + // Create some reusable test data | |
| 8 | + const createContextItem = ( | |
| 9 | + content: string, | |
| 10 | + description: string, | |
| 11 | + fileUri?: string, | |
| 12 | + editing?: boolean, | |
| 13 | + ): ContextItemWithId => ({ | |
| 14 | + id: { providerTitle: "test", itemId: "test-id" }, | |
| 15 | + content, | |
| 16 | + description, | |
| 17 | + name: "Test Item", | |
| 18 | + editing, | |
| 19 | + uri: fileUri ? { type: "file", value: fileUri } : undefined, | |
| 20 | + }); | |
| 21 | + | |
| 22 | + test("processEditorContent should return empty arrays when content is empty", () => { | |
| 23 | + // Empty editor state | |
| 24 | + const emptyEditorState: JSONContent = { | |
| 25 | + type: "doc", | |
| 26 | + content: [], | |
| 27 | + }; | |
| 28 | + | |
| 29 | + const result = processEditorContent(emptyEditorState); | |
| 30 | + | |
| 31 | + expect(result.parts).toEqual([]); | |
| 32 | + expect(result.contextRequests).toEqual([]); | |
| 33 | + expect(result.selectedCode).toEqual([]); | |
| 34 | + expect(result.slashCommandName).toBeUndefined(); | |
| 35 | + }); | |
| 36 | + | |
| 37 | + test("processEditorContent should process text paragraphs", () => { | |
| 38 | + const editorState: JSONContent = { | |
| 39 | + type: "doc", | |
| 40 | + content: [ | |
| 41 | + { | |
| 42 | + type: "paragraph", | |
| 43 | + content: [ | |
| 44 | + { | |
| 45 | + type: "text", | |
| 46 | + text: "Hello world", | |
| 47 | + }, | |
| 48 | + ], | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + type: "paragraph", | |
| 52 | + content: [ | |
| 53 | + { | |
| 54 | + type: "text", | |
| 55 | + text: "This is another paragraph", | |
| 56 | + }, | |
| 57 | + ], | |
| 58 | + }, | |
| 59 | + ], | |
| 60 | + }; | |
| 61 | + | |
| 62 | + const result = processEditorContent(editorState); | |
| 63 | + | |
| 64 | + expect(result.parts).toEqual([ | |
| 65 | + { | |
| 66 | + type: "text", | |
| 67 | + text: "Hello world\nThis is another paragraph", | |
| 68 | + }, | |
| 69 | + ]); | |
| 70 | + expect(result.contextRequests).toEqual([]); | |
| 71 | + expect(result.selectedCode).toEqual([]); | |
| 72 | + }); | |
| 73 | + | |
| 74 | + test("processEditorContent should detect slash commands", () => { | |
| 75 | + const editorState: JSONContent = { | |
| 76 | + type: "doc", | |
| 77 | + content: [ | |
| 78 | + { | |
| 79 | + type: "prompt-block", | |
| 80 | + attrs: { | |
| 81 | + item: { | |
| 82 | + name: "test-command", | |
| 83 | + }, | |
| 84 | + }, | |
| 85 | + }, | |
| 86 | + { | |
| 87 | + type: "paragraph", | |
| 88 | + content: [ | |
| 89 | + { | |
| 90 | + type: "text", | |
| 91 | + text: "Some text after the prompt", | |
| 92 | + }, | |
| 93 | + ], | |
| 94 | + }, | |
| 95 | + ], | |
| 96 | + }; | |
| 97 | + | |
| 98 | + const result = processEditorContent(editorState); | |
| 99 | + | |
| 100 | + expect(result.slashCommandName).toBe("test-command"); | |
| 101 | + expect(result.parts).toEqual([ | |
| 102 | + { | |
| 103 | + type: "text", | |
| 104 | + text: "Some text after the prompt", | |
| 105 | + }, | |
| 106 | + ]); | |
| 107 | + }); | |
| 108 | + | |
| 109 | + test("processEditorContent should handle mentions and collect context requests", () => { | |
| 110 | + const editorState: JSONContent = { | |
| 111 | + type: "doc", | |
| 112 | + content: [ | |
| 113 | + { | |
| 114 | + type: "paragraph", | |
| 115 | + content: [ | |
| 116 | + { | |
| 117 | + type: "text", | |
| 118 | + text: "Hello ", | |
| 119 | + }, | |
| 120 | + { | |
| 121 | + type: "mention", | |
| 122 | + attrs: { | |
| 123 | + id: "fileSearch", | |
| 124 | + label: "@fileSearch", | |
| 125 | + itemType: "contextProvider", | |
| 126 | + }, | |
| 127 | + }, | |
| 128 | + { | |
| 129 | + type: "text", | |
| 130 | + text: " and also ", | |
| 131 | + }, | |
| 132 | + { | |
| 133 | + type: "mention", | |
| 134 | + attrs: { | |
| 135 | + id: "github", | |
| 136 | + label: "@github", | |
| 137 | + itemType: "contextProvider", | |
| 138 | + }, | |
| 139 | + }, | |
| 140 | + ], | |
| 141 | + }, | |
| 142 | + ], | |
| 143 | + }; | |
| 144 | + | |
| 145 | + const result = processEditorContent(editorState); | |
| 146 | + | |
| 147 | + expect(result.parts).toEqual([ | |
| 148 | + { | |
| 149 | + type: "text", | |
| 150 | + text: "Hello @fileSearch and also @github", | |
| 151 | + }, | |
| 152 | + ]); | |
| 153 | + expect(result.contextRequests).toEqual([ | |
| 154 | + { provider: "fileSearch" }, | |
| 155 | + { provider: "github" }, | |
| 156 | + ]); | |
| 157 | + }); | |
| 158 | + | |
| 159 | + test("processEditorContent should handle code blocks", () => { | |
| 160 | + const codeItem = createContextItem( | |
| 161 | + "function test() {\n return 'hello';\n}", | |
| 162 | + "a description", | |
| 163 | + "file:///example.js", | |
| 164 | + ); | |
| 165 | + | |
| 166 | + const editorState: JSONContent = { | |
| 167 | + type: "doc", | |
| 168 | + content: [ | |
| 169 | + { | |
| 170 | + type: "paragraph", | |
| 171 | + content: [ | |
| 172 | + { | |
| 173 | + type: "text", | |
| 174 | + text: "Check this code:", | |
| 175 | + }, | |
| 176 | + ], | |
| 177 | + }, | |
| 178 | + { | |
| 179 | + type: "code-block", | |
| 180 | + attrs: { | |
| 181 | + item: codeItem, | |
| 182 | + }, | |
| 183 | + }, | |
| 184 | + ], | |
| 185 | + }; | |
| 186 | + | |
| 187 | + const result = processEditorContent(editorState); | |
| 188 | + | |
| 189 | + expect(result.parts).toEqual([ | |
| 190 | + { | |
| 191 | + type: "text", | |
| 192 | + text: "Check this code:\n\n```js example.js\nfunction test() {\n return 'hello';\n}\n```\n", | |
| 193 | + }, | |
| 194 | + ]); | |
| 195 | + expect(result.selectedCode).toHaveLength(1); | |
| 196 | + expect(result.selectedCode[0].filepath).toBe("file:///example.js"); | |
| 197 | + }); | |
| 198 | + | |
| 199 | + test("processEditorContent should not include editing code blocks in the text", () => { | |
| 200 | + const editingCodeItem = createContextItem( | |
| 201 | + "function test() {\n return 'hello';\n}", | |
| 202 | + "a description", | |
| 203 | + "file:///editing.js", | |
| 204 | + true, // Set editing to true | |
| 205 | + ); | |
| 206 | + | |
| 207 | + const editorState: JSONContent = { | |
| 208 | + type: "doc", | |
| 209 | + content: [ | |
| 210 | + { | |
| 211 | + type: "paragraph", | |
| 212 | + content: [ | |
| 213 | + { | |
| 214 | + type: "text", | |
| 215 | + text: "Edit this code:", | |
| 216 | + }, | |
| 217 | + ], | |
| 218 | + }, | |
| 219 | + { | |
| 220 | + type: "code-block", | |
| 221 | + attrs: { | |
| 222 | + item: editingCodeItem, | |
| 223 | + }, | |
| 224 | + }, | |
| 225 | + ], | |
| 226 | + }; | |
| 227 | + | |
| 228 | + const result = processEditorContent(editorState); | |
| 229 | + | |
| 230 | + // The code block should not be included in parts because it's marked as editing | |
| 231 | + expect(result.parts).toEqual([ | |
| 232 | + { | |
| 233 | + type: "text", | |
| 234 | + text: "Edit this code:", | |
| 235 | + }, | |
| 236 | + ]); | |
| 237 | + | |
| 238 | + // But it should still be in selectedCode | |
| 239 | + expect(result.selectedCode).toHaveLength(1); | |
| 240 | + expect(result.selectedCode[0].filepath).toBe("file:///editing.js"); | |
| 241 | + }); | |
| 242 | + | |
| 243 | + test("processEditorContent should handle images", () => { | |
| 244 | + const editorState: JSONContent = { | |
| 245 | + type: "doc", | |
| 246 | + content: [ | |
| 247 | + { | |
| 248 | + type: "paragraph", | |
| 249 | + content: [ | |
| 250 | + { | |
| 251 | + type: "text", | |
| 252 | + text: "Here's an image:", | |
| 253 | + }, | |
| 254 | + ], | |
| 255 | + }, | |
| 256 | + { | |
| 257 | + type: "image", | |
| 258 | + attrs: { | |
| 259 | + src: "https://example.com/image.png", | |
| 260 | + }, | |
| 261 | + }, | |
| 262 | + ], | |
| 263 | + }; | |
| 264 | + | |
| 265 | + const result = processEditorContent(editorState); | |
| 266 | + | |
| 267 | + expect(result.parts).toEqual([ | |
| 268 | + { | |
| 269 | + type: "text", | |
| 270 | + text: "Here's an image:", | |
| 271 | + }, | |
| 272 | + { | |
| 273 | + type: "imageUrl", | |
| 274 | + imageUrl: { url: "https://example.com/image.png" }, | |
| 275 | + }, | |
| 276 | + ]); | |
| 277 | + }); | |
| 278 | + | |
| 279 | + test("processEditorContent should handle complex content with multiple elements", () => { | |
| 280 | + const codeItem = createContextItem( | |
| 281 | + "const x = 42;", | |
| 282 | + "a description", | |
| 283 | + "file:///script.ts", | |
| 284 | + ); | |
| 285 | + | |
| 286 | + const editorState: JSONContent = { | |
| 287 | + type: "doc", | |
| 288 | + content: [ | |
| 289 | + { | |
| 290 | + type: "prompt-block", | |
| 291 | + attrs: { | |
| 292 | + item: { | |
| 293 | + name: "explain", | |
| 294 | + }, | |
| 295 | + }, | |
| 296 | + }, | |
| 297 | + { | |
| 298 | + type: "paragraph", | |
| 299 | + content: [ | |
| 300 | + { | |
| 301 | + type: "text", | |
| 302 | + text: "Please explain this code and also check ", | |
| 303 | + }, | |
| 304 | + { | |
| 305 | + type: "mention", | |
| 306 | + attrs: { | |
| 307 | + id: "related", | |
| 308 | + label: "@related", | |
| 309 | + itemType: "similarFiles", | |
| 310 | + }, | |
| 311 | + }, | |
| 312 | + ], | |
| 313 | + }, | |
| 314 | + { | |
| 315 | + type: "code-block", | |
| 316 | + attrs: { | |
| 317 | + item: codeItem, | |
| 318 | + }, | |
| 319 | + }, | |
| 320 | + { | |
| 321 | + type: "paragraph", | |
| 322 | + content: [ | |
| 323 | + { | |
| 324 | + type: "text", | |
| 325 | + text: "What does this constant do?", | |
| 326 | + }, | |
| 327 | + ], | |
| 328 | + }, | |
| 329 | + { | |
| 330 | + type: "image", | |
| 331 | + attrs: { | |
| 332 | + src: "https://example.com/diagram.png", | |
| 333 | + }, | |
| 334 | + }, | |
| 335 | + ], | |
| 336 | + }; | |
| 337 | + | |
| 338 | + const result = processEditorContent(editorState); | |
| 339 | + | |
| 340 | + expect(result.slashCommandName).toBe("explain"); | |
| 341 | + expect(result.contextRequests).toEqual([{ provider: "similarFiles" }]); | |
| 342 | + expect(result.selectedCode).toHaveLength(1); | |
| 343 | + expect(result.selectedCode[0].filepath).toBe("file:///script.ts"); | |
| 344 | + | |
| 345 | + // `\n\n\`\`\`${extension} ${relativePathOrBasename}\n${contextItem.content}\n\`\`\`\n`; | |
| 346 | + | |
| 347 | + expect(result.parts).toEqual([ | |
| 348 | + { | |
| 349 | + type: "text", | |
| 350 | + text: "Please explain this code and also check @related\n\n```ts script.ts\nconst x = 42;\n```\n\nWhat does this constant do?", | |
| 351 | + }, | |
| 352 | + { | |
| 353 | + type: "imageUrl", | |
| 354 | + imageUrl: { url: "https://example.com/diagram.png" }, | |
| 355 | + }, | |
| 356 | + ]); | |
| 357 | + }); | |
| 358 | + | |
| 359 | + test("processEditorContent should merge consecutive text parts", () => { | |
| 360 | + const editorState: JSONContent = { | |
| 361 | + type: "doc", | |
| 362 | + content: [ | |
| 363 | + { | |
| 364 | + type: "paragraph", | |
| 365 | + content: [ | |
| 366 | + { | |
| 367 | + type: "text", | |
| 368 | + text: "First paragraph", | |
| 369 | + }, | |
| 370 | + ], | |
| 371 | + }, | |
| 372 | + { | |
| 373 | + type: "paragraph", | |
| 374 | + content: [ | |
| 375 | + { | |
| 376 | + type: "text", | |
| 377 | + text: "Second paragraph", | |
| 378 | + }, | |
| 379 | + ], | |
| 380 | + }, | |
| 381 | + { | |
| 382 | + type: "image", | |
| 383 | + attrs: { | |
| 384 | + src: "https://example.com/image.png", | |
| 385 | + }, | |
| 386 | + }, | |
| 387 | + { | |
| 388 | + type: "paragraph", | |
| 389 | + content: [ | |
| 390 | + { | |
| 391 | + type: "text", | |
| 392 | + text: "Third paragraph", | |
| 393 | + }, | |
| 394 | + ], | |
| 395 | + }, | |
| 396 | + ], | |
| 397 | + }; | |
| 398 | + | |
| 399 | + const result = processEditorContent(editorState); | |
| 400 | + | |
| 401 | + // First two paragraphs should be merged, but the third should be separate | |
| 402 | + // because there's an image in between | |
| 403 | + expect(result.parts).toEqual([ | |
| 404 | + { | |
| 405 | + type: "text", | |
| 406 | + text: "First paragraph\nSecond paragraph", | |
| 407 | + }, | |
| 408 | + { | |
| 409 | + type: "imageUrl", | |
| 410 | + imageUrl: { url: "https://example.com/image.png" }, | |
| 411 | + }, | |
| 412 | + { | |
| 413 | + type: "text", | |
| 414 | + text: "Third paragraph", | |
| 415 | + }, | |
| 416 | + ]); | |
| 417 | + }); | |
| 418 | + | |
| 419 | + test("processEditorContent should handle unexpected content types gracefully", () => { | |
| 420 | + // Testing with an unknown node type | |
| 421 | + const editorState: JSONContent = { | |
| 422 | + type: "doc", | |
| 423 | + content: [ | |
| 424 | + { | |
| 425 | + type: "paragraph", | |
| 426 | + content: [ | |
| 427 | + { | |
| 428 | + type: "text", | |
| 429 | + text: "Normal text", | |
| 430 | + }, | |
| 431 | + ], | |
| 432 | + }, | |
| 433 | + { | |
| 434 | + type: "unknown-type", // This type doesn't exist | |
| 435 | + content: [ | |
| 436 | + { | |
| 437 | + type: "text", | |
| 438 | + text: "This should be ignored", | |
| 439 | + }, | |
| 440 | + ], | |
| 441 | + }, | |
| 442 | + ], | |
| 443 | + }; | |
| 444 | + | |
| 445 | + // Spy on console.warn to verify it's called | |
| 446 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | |
| 447 | + | |
| 448 | + const result = processEditorContent(editorState); | |
| 449 | + | |
| 450 | + // Only the normal paragraph should be processed | |
| 451 | + expect(result.parts).toEqual([ | |
| 452 | + { | |
| 453 | + type: "text", | |
| 454 | + text: "Normal text", | |
| 455 | + }, | |
| 456 | + ]); | |
| 457 | + | |
| 458 | + // Console.warn should be called for the unknown type | |
| 459 | + expect(warnSpy).toHaveBeenCalledWith( | |
| 460 | + "Unexpected content type", | |
| 461 | + "unknown-type", | |
| 462 | + ); | |
| 463 | + | |
| 464 | + warnSpy.mockRestore(); | |
| 465 | + }); | |
| 466 | + | |
| 467 | + test("processEditorContent should handle missing attrs in code blocks", () => { | |
| 468 | + // Testing with a code block that has no item attribute | |
| 469 | + const editorState: JSONContent = { | |
| 470 | + type: "doc", | |
| 471 | + content: [ | |
| 472 | + { | |
| 473 | + type: "code-block", | |
| 474 | + // Missing attrs property | |
| 475 | + }, | |
| 476 | + ], | |
| 477 | + }; | |
| 478 | + | |
| 479 | + // Spy on console.warn to verify it's called | |
| 480 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | |
| 481 | + | |
| 482 | + const result = processEditorContent(editorState); | |
| 483 | + | |
| 484 | + // No parts should be created | |
| 485 | + expect(result.parts).toEqual([]); | |
| 486 | + expect(result.selectedCode).toEqual([]); | |
| 487 | + | |
| 488 | + // Console.warn should be called for the missing item attribute | |
| 489 | + expect(warnSpy).toHaveBeenCalledWith("codeBlock has no item attribute"); | |
| 490 | + | |
| 491 | + warnSpy.mockRestore(); | |
| 492 | + }); | |
| 493 | + | |
| 494 | + test("processEditorContent should handle paragraphs with unexpected child types", () => { | |
| 495 | + // Testing with a paragraph that has an unknown child type | |
| 496 | + const editorState: JSONContent = { | |
| 497 | + type: "doc", | |
| 498 | + content: [ | |
| 499 | + { | |
| 500 | + type: "paragraph", | |
| 501 | + content: [ | |
| 502 | + { | |
| 503 | + type: "text", | |
| 504 | + text: "Normal text ", | |
| 505 | + }, | |
| 506 | + { | |
| 507 | + type: "unknown-child", // This type doesn't exist | |
| 508 | + text: "This should be ignored", | |
| 509 | + }, | |
| 510 | + ], | |
| 511 | + }, | |
| 512 | + ], | |
| 513 | + }; | |
| 514 | + | |
| 515 | + // Spy on console.warn to verify it's called | |
| 516 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | |
| 517 | + | |
| 518 | + const result = processEditorContent(editorState); | |
| 519 | + | |
| 520 | + // Only the normal text should be processed | |
| 521 | + expect(result.parts).toEqual([ | |
| 522 | + { | |
| 523 | + type: "text", | |
| 524 | + text: "Normal text ", | |
| 525 | + }, | |
| 526 | + ]); | |
| 527 | + | |
| 528 | + // Console.warn should be called for the unknown child type | |
| 529 | + expect(warnSpy).toHaveBeenCalledWith( | |
| 530 | + "Unexpected child type", | |
| 531 | + "unknown-child", | |
| 532 | + ); | |
| 533 | + | |
| 534 | + warnSpy.mockRestore(); | |
| 535 | + }); | |
| 536 | +}); |
added gui/src/components/mainInput/TipTapEditor/utils/processEditorContent.ts +119 −0
| @@ -0,0 +1,119 @@ | ||
| 1 | +import Image from "@tiptap/extension-image"; | |
| 2 | +import { Paragraph } from "@tiptap/extension-paragraph"; | |
| 3 | +import { Text } from "@tiptap/extension-text"; | |
| 4 | +import { JSONContent } from "@tiptap/react"; | |
| 5 | +import { | |
| 6 | + ContextItemWithId, | |
| 7 | + MessagePart, | |
| 8 | + RangeInFile, | |
| 9 | + TextMessagePart, | |
| 10 | +} from "core"; | |
| 11 | +import { ctxItemToRifWithContents } from "core/commands/util"; | |
| 12 | +import { getUriDescription } from "core/util/uri"; | |
| 13 | +import { CodeBlock, Mention, PromptBlock } from "../extensions"; | |
| 14 | +import { GetContextRequest } from "./types"; | |
| 15 | + | |
| 16 | +interface MentionAttrs { | |
| 17 | + label: string; | |
| 18 | + id: string; | |
| 19 | + itemType?: string; | |
| 20 | + query?: string; | |
| 21 | +} | |
| 22 | + | |
| 23 | +function resolvePromptBlock(p: JSONContent): string | undefined { | |
| 24 | + return p.attrs?.item.name; | |
| 25 | +} | |
| 26 | + | |
| 27 | +function resolveParagraph(p: JSONContent): [string, GetContextRequest[]] { | |
| 28 | + const contextRequests: GetContextRequest[] = []; | |
| 29 | + | |
| 30 | + const text = (p.content || []) | |
| 31 | + .map((child) => { | |
| 32 | + switch (child.type) { | |
| 33 | + case Text.name: | |
| 34 | + return child.text; | |
| 35 | + case Mention.name: | |
| 36 | + const attrs = child.attrs as MentionAttrs; | |
| 37 | + contextRequests.push({ | |
| 38 | + provider: | |
| 39 | + attrs.itemType === "contextProvider" ? attrs.id : attrs.itemType!, | |
| 40 | + }); | |
| 41 | + return child.attrs?.renderInlineAs ?? child.attrs?.label; | |
| 42 | + | |
| 43 | + default: | |
| 44 | + console.warn("Unexpected child type", child.type); | |
| 45 | + return ""; | |
| 46 | + } | |
| 47 | + }) | |
| 48 | + .join("") | |
| 49 | + .trimStart(); | |
| 50 | + | |
| 51 | + return [text, contextRequests]; | |
| 52 | +} | |
| 53 | + | |
| 54 | +export function processEditorContent(editorState: JSONContent) { | |
| 55 | + const contextRequests: GetContextRequest[] = []; | |
| 56 | + const selectedCode: RangeInFile[] = []; | |
| 57 | + let slashCommandName: string | undefined; | |
| 58 | + | |
| 59 | + const parts: MessagePart[] = []; | |
| 60 | + for (const p of editorState?.content || []) { | |
| 61 | + switch (p.type) { | |
| 62 | + case PromptBlock.name: | |
| 63 | + slashCommandName = resolvePromptBlock(p); | |
| 64 | + break; | |
| 65 | + case Paragraph.name: | |
| 66 | + const [text, ctxItems] = resolveParagraph(p); | |
| 67 | + | |
| 68 | + contextRequests.push(...ctxItems); | |
| 69 | + | |
| 70 | + if (text) { | |
| 71 | + // Merge with previous text part if possible | |
| 72 | + if (parts[parts.length - 1]?.type === "text") { | |
| 73 | + (parts[parts.length - 1] as TextMessagePart).text += "\n" + text; | |
| 74 | + } else { | |
| 75 | + parts.push({ type: "text", text }); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + break; | |
| 79 | + case CodeBlock.name: | |
| 80 | + if (!p.attrs?.item) { | |
| 81 | + console.warn("codeBlock has no item attribute"); | |
| 82 | + break; | |
| 83 | + } | |
| 84 | + | |
| 85 | + const contextItem = p.attrs.item as ContextItemWithId; | |
| 86 | + const rif = ctxItemToRifWithContents(contextItem, true); | |
| 87 | + selectedCode.push(rif); | |
| 88 | + | |
| 89 | + // If editing, only include in selectedCode | |
| 90 | + if (contextItem.editing) { | |
| 91 | + break; | |
| 92 | + } | |
| 93 | + | |
| 94 | + const { extension, relativePathOrBasename } = getUriDescription( | |
| 95 | + rif.filepath, | |
| 96 | + window.workspacePaths ?? [], | |
| 97 | + ); | |
| 98 | + const codeText = `\n\`\`\`${extension} ${relativePathOrBasename}\n${contextItem.content}\n\`\`\`\n`; | |
| 99 | + | |
| 100 | + if (parts[parts.length - 1]?.type === "text") { | |
| 101 | + (parts[parts.length - 1] as TextMessagePart).text += "\n" + codeText; | |
| 102 | + } else { | |
| 103 | + parts.push({ type: "text", text: codeText }); | |
| 104 | + } | |
| 105 | + break; | |
| 106 | + case Image.name: | |
| 107 | + parts.push({ | |
| 108 | + type: "imageUrl", | |
| 109 | + imageUrl: { url: p.attrs?.src }, | |
| 110 | + }); | |
| 111 | + break; | |
| 112 | + default: { | |
| 113 | + console.warn("Unexpected content type", p.type); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + return { parts, contextRequests, selectedCode, slashCommandName }; | |
| 119 | +} |
added gui/src/components/mainInput/TipTapEditor/utils/renderPromptv1.test.ts +128 −0
| @@ -0,0 +1,128 @@ | ||
| 1 | +import { SlashCommandDescWithSource } from "core"; | |
| 2 | +import { beforeEach, describe, it, vi } from "vitest"; | |
| 3 | +import { | |
| 4 | + MockIdeMessenger, | |
| 5 | + MockResponseHandler, | |
| 6 | +} from "../../../../context/MockIdeMessenger"; | |
| 7 | +import { getRenderedV1Prompt } from "./renderPromptv1"; | |
| 8 | + | |
| 9 | +function getTestPromptV1Command(body: string): SlashCommandDescWithSource { | |
| 10 | + return { | |
| 11 | + prompt: body, | |
| 12 | + name: "test-prompt-name", | |
| 13 | + description: "test-prompt-description", | |
| 14 | + isLegacy: false, | |
| 15 | + source: "prompt-file-v1", | |
| 16 | + }; | |
| 17 | +} | |
| 18 | +describe("get rendered v1 prompt file", () => { | |
| 19 | + const ideMessenger = new MockIdeMessenger(); | |
| 20 | + const firstDir = ideMessenger.responses["getWorkspaceDirs"]?.[0] ?? ""; | |
| 21 | + const customGetContextItems: MockResponseHandler< | |
| 22 | + "context/getContextItems" | |
| 23 | + > = async ({ name }) => { | |
| 24 | + if (name === "diff") { | |
| 25 | + return [ | |
| 26 | + { | |
| 27 | + id: { | |
| 28 | + itemId: "id", | |
| 29 | + providerTitle: "diff", | |
| 30 | + }, | |
| 31 | + name: "name", | |
| 32 | + description: "description", | |
| 33 | + content: "Diff provider content", | |
| 34 | + }, | |
| 35 | + ]; | |
| 36 | + } | |
| 37 | + return []; | |
| 38 | + }; | |
| 39 | + beforeEach(async () => { | |
| 40 | + vi.clearAllMocks(); | |
| 41 | + ideMessenger.resetMocks(); | |
| 42 | + }); | |
| 43 | + | |
| 44 | + it("should render a prompt file with no attachments or templated input", async () => { | |
| 45 | + const rawContent = "Simple prompt with no attachments"; | |
| 46 | + const command = getTestPromptV1Command(rawContent); | |
| 47 | + const rendered = await getRenderedV1Prompt( | |
| 48 | + ideMessenger, | |
| 49 | + command, | |
| 50 | + "User input", | |
| 51 | + [], | |
| 52 | + ); | |
| 53 | + expect(rendered).toBe("Simple prompt with no attachments\n\nUser input"); | |
| 54 | + }); | |
| 55 | + | |
| 56 | + it("should render a prompt file templated input", async () => { | |
| 57 | + const rawContent = "{{{ input }}} Simple prompt with no attachments"; | |
| 58 | + const command = getTestPromptV1Command(rawContent); | |
| 59 | + const rendered = await getRenderedV1Prompt( | |
| 60 | + ideMessenger, | |
| 61 | + command, | |
| 62 | + "User input", | |
| 63 | + [], | |
| 64 | + ); | |
| 65 | + expect(rendered).toBe("User input Simple prompt with no attachments"); | |
| 66 | + }); | |
| 67 | + | |
| 68 | + it("Should handle file attachments", async () => { | |
| 69 | + ideMessenger.responses["readFile"] = "Test file content"; | |
| 70 | + const rawContent = "Content with URL {{{ testFile.txt }}}"; | |
| 71 | + const command = getTestPromptV1Command(rawContent); | |
| 72 | + const rendered = await getRenderedV1Prompt( | |
| 73 | + ideMessenger, | |
| 74 | + command, | |
| 75 | + "User input", | |
| 76 | + [], | |
| 77 | + ); | |
| 78 | + expect(rendered).toBe("Content with URL Test file content\n\nUser input"); | |
| 79 | + }); | |
| 80 | + | |
| 81 | + it("should handle supported providers", async () => { | |
| 82 | + ideMessenger.responseHandlers["context/getContextItems"] = | |
| 83 | + customGetContextItems; | |
| 84 | + const rawContent = "Tell me about {{{ diff }}} please"; | |
| 85 | + const command = getTestPromptV1Command(rawContent); | |
| 86 | + const rendered = await getRenderedV1Prompt( | |
| 87 | + ideMessenger, | |
| 88 | + command, | |
| 89 | + "User input", | |
| 90 | + [], | |
| 91 | + ); | |
| 92 | + expect(rendered).toBe( | |
| 93 | + "Tell me about Diff provider content please\n\nUser input", | |
| 94 | + ); | |
| 95 | + }); | |
| 96 | + | |
| 97 | + it("should render file read errors within prompt", async () => { | |
| 98 | + ideMessenger.responses["fileExists"] = false; | |
| 99 | + const rawContent = "Content with provider {{{ nonexistent }}}"; | |
| 100 | + const command = getTestPromptV1Command(rawContent); | |
| 101 | + const rendered = await getRenderedV1Prompt( | |
| 102 | + ideMessenger, | |
| 103 | + command, | |
| 104 | + "User input", | |
| 105 | + [], | |
| 106 | + ); | |
| 107 | + expect(rendered).toBe( | |
| 108 | + `Content with provider [Error reading file "nonexistent"]\n\nUser input`, | |
| 109 | + ); | |
| 110 | + }); | |
| 111 | + | |
| 112 | + it("should handle multiple attachments", async () => { | |
| 113 | + ideMessenger.responseHandlers["context/getContextItems"] = | |
| 114 | + customGetContextItems; | |
| 115 | + ideMessenger.responses["readFile"] = "Test file content"; | |
| 116 | + const rawContent = "Content with {{{ diff }}} and {{{ file.txt }}}"; | |
| 117 | + const command = getTestPromptV1Command(rawContent); | |
| 118 | + const rendered = await getRenderedV1Prompt( | |
| 119 | + ideMessenger, | |
| 120 | + command, | |
| 121 | + "User input", | |
| 122 | + [], | |
| 123 | + ); | |
| 124 | + expect(rendered).toBe( | |
| 125 | + "Content with Diff provider content and Test file content\n\nUser input", | |
| 126 | + ); | |
| 127 | + }); | |
| 128 | +}); |
modified gui/src/components/mainInput/TipTapEditor/utils/renderPromptv1.ts +9 −2
| @@ -30,7 +30,9 @@ | ||
| 30 | 30 | if (response.status === "success") { |
| 31 | 31 | return response.content.map((item) => item.content).join("\n\n"); |
| 32 | 32 | } else { |
| 33 | - throw new Error(`Failed to get context items from provider: ${name}`); | |
| 33 | + throw new Error( | |
| 34 | + `Failed to get context items from provider "${name}": ${response.error}`, | |
| 35 | + ); | |
| 34 | 36 | } |
| 35 | 37 | }, |
| 36 | 38 | ], |
| @@ -40,7 +42,7 @@ | ||
| 40 | 42 | return resolveRelativePathInDir(path, ideMessenger.ide, workspaceDirs); |
| 41 | 43 | }; |
| 42 | 44 | const handlebars = await import("handlebars"); |
| 43 | - return await renderTemplatedString( | |
| 45 | + let rendered = await renderTemplatedString( | |
| 44 | 46 | handlebars, |
| 45 | 47 | command.prompt, |
| 46 | 48 | { input: userInput }, |
| @@ -48,4 +50,9 @@ | ||
| 48 | 50 | ideMessenger.ide.readFile.bind(ideMessenger.ide), |
| 49 | 51 | getUriFromPath, |
| 50 | 52 | ); |
| 53 | + // For prompt file append the input if it isn't rendered | |
| 54 | + if (!rendered.includes(userInput)) { | |
| 55 | + rendered = rendered + `\n\n${userInput}`; | |
| 56 | + } | |
| 57 | + return rendered.trim(); | |
| 51 | 58 | } |
modified gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.test.ts +54 −103
| @@ -1,7 +1,6 @@ | ||
| 1 | 1 | import { SlashCommandDescWithSource } from "core"; |
| 2 | -import { setUpTestDir, tearDownTestDir } from "core/test/testDir"; | |
| 3 | -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | |
| 4 | -import { IdeMessenger, IIdeMessenger } from "../../../../context/IdeMessenger"; | |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| 3 | +import { MockIdeMessenger } from "../../../../context/MockIdeMessenger"; | |
| 5 | 4 | import { getPromptV2ContextRequests } from "./renderPromptv2"; |
| 6 | 5 | |
| 7 | 6 | function getTestPromptV2Command(body: string): SlashCommandDescWithSource { |
| @@ -14,116 +13,68 @@ | ||
| 14 | 13 | }; |
| 15 | 14 | } |
| 16 | 15 | describe("getPromptV2ContextRequests", () => { |
| 17 | - let ideMessenger: IIdeMessenger; | |
| 18 | - const mockGetContextItems = vi.fn().mockResolvedValue([ | |
| 19 | - { | |
| 20 | - name: "custom", | |
| 21 | - content: "Custom provider content", | |
| 22 | - description: "Custom provider", | |
| 23 | - }, | |
| 24 | - ]); | |
| 16 | + const ideMessenger = new MockIdeMessenger(); | |
| 17 | + const firstDir = ideMessenger.responses["getWorkspaceDirs"]?.[0] ?? ""; | |
| 25 | 18 | beforeEach(async () => { |
| 26 | - // Mock the ideMessenger | |
| 27 | 19 | vi.clearAllMocks(); |
| 28 | - setUpTestDir(); | |
| 29 | - ideMessenger = new IdeMessenger(); | |
| 30 | - vi.spyOn(ideMessenger.ide, "readFile").mockImplementation(() => { | |
| 31 | - return Promise.resolve("Example Content"); | |
| 32 | - }); | |
| 33 | - }); | |
| 34 | - | |
| 35 | - afterEach(() => { | |
| 36 | - tearDownTestDir(); | |
| 20 | + ideMessenger.resetMocks(); | |
| 37 | 21 | }); |
| 38 | 22 | |
| 39 | 23 | it("should render a prompt file with no attachments", async () => { |
| 40 | 24 | const rawContent = "Simple prompt with no attachments"; |
| 41 | 25 | const command = getTestPromptV2Command(rawContent); |
| 42 | 26 | const requests = await getPromptV2ContextRequests(ideMessenger, command); |
| 27 | + expect(requests).toHaveLength(0); | |
| 28 | + }); | |
| 29 | + | |
| 30 | + it("should handle URL attachments", async () => { | |
| 31 | + const rawContent = "Content with URL @https://example.com"; | |
| 32 | + const command = getTestPromptV2Command(rawContent); | |
| 33 | + const requests = await getPromptV2ContextRequests(ideMessenger, command); | |
| 34 | + expect(requests).toHaveLength(1); | |
| 35 | + expect(requests[0].provider).toBe("url"); | |
| 36 | + expect(requests[0].query).toBe("https://example.com"); | |
| 37 | + }); | |
| 38 | + | |
| 39 | + it("should resolve file attachments", async () => { | |
| 40 | + const rawContent = "Content with file attachment @test.txt"; | |
| 41 | + const command = getTestPromptV2Command(rawContent); | |
| 42 | + const requests = await getPromptV2ContextRequests(ideMessenger, command); | |
| 43 | 43 | |
| 44 | + expect(requests).toHaveLength(1); | |
| 45 | + expect(requests[0].provider).toBe("file"); | |
| 46 | + expect(requests[0].query).toBe(`${firstDir}/test.txt`); | |
| 47 | + }); | |
| 48 | + | |
| 49 | + it("should handle nested prompt files", async () => { | |
| 50 | + ideMessenger.responses["readFile"] = "Contents @myfile.txt"; | |
| 51 | + const rawContent = "Main prompt with nested @test.prompt"; | |
| 52 | + const command = getTestPromptV2Command(rawContent); | |
| 53 | + const requests = await getPromptV2ContextRequests(ideMessenger, command); | |
| 54 | + expect(requests).toHaveLength(2); | |
| 55 | + expect(requests[0].provider).toBe("file"); | |
| 56 | + expect(requests[0].query).toBe(`${firstDir}/myfile.txt`); | |
| 57 | + expect(requests[1].provider).toBe("file"); | |
| 58 | + expect(requests[1].query).toBe(`${firstDir}/test.prompt`); | |
| 59 | + }); | |
| 60 | + | |
| 61 | + it("should ignore unsupported providers", async () => { | |
| 62 | + ideMessenger.responses["fileExists"] = false; | |
| 63 | + const rawContent = "Content with custom provider @unsupported"; | |
| 64 | + const command = getTestPromptV2Command(rawContent); | |
| 65 | + const requests = await getPromptV2ContextRequests(ideMessenger, command); | |
| 44 | 66 | expect(requests).toHaveLength(0); |
| 45 | 67 | }); |
| 46 | 68 | |
| 47 | - // it("should handle URL attachments", async () => { | |
| 48 | - // const rawContent = "Content with URL @https://example.com"; | |
| 49 | - | |
| 50 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 51 | - // rawContent, | |
| 52 | - // extras, | |
| 53 | - // ); | |
| 54 | - | |
| 55 | - // expect(contextItems).toHaveLength(1); | |
| 56 | - // expect(renderedPrompt).toContain("Example Content"); | |
| 57 | - // }); | |
| 58 | - | |
| 59 | - // it("should resolve file attachments", async () => { | |
| 60 | - // const rawContent = "Content with file attachment @test.txt"; | |
| 61 | - | |
| 62 | - // addToTestDir([["test.txt", "File content"]]); | |
| 63 | - | |
| 64 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 65 | - // rawContent, | |
| 66 | - // extras, | |
| 67 | - // ); | |
| 68 | - | |
| 69 | - // expect(contextItems).toHaveLength(1); | |
| 70 | - // expect(renderedPrompt).toContain("```test.txt\nFile content\n```"); | |
| 71 | - // expect(renderedPrompt).toContain("Content with file attachment @test.txt"); | |
| 72 | - // }); | |
| 73 | - | |
| 74 | - // it("should handle nested prompt files", async () => { | |
| 75 | - // const rawContent = "Main prompt with nested @nested.prompt"; | |
| 76 | - // addToTestDir([ | |
| 77 | - // ["nested.prompt", "Nested prompt content with @CustomProvider"], | |
| 78 | - // ]); | |
| 79 | - | |
| 80 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 81 | - // rawContent, | |
| 82 | - // extras, | |
| 83 | - // ); | |
| 84 | - | |
| 85 | - // expect(renderedPrompt).toContain("Custom provider content"); | |
| 86 | - // }); | |
| 87 | - | |
| 88 | - // it("should handle custom context providers", async () => { | |
| 89 | - // const rawContent = "Content with custom provider @CustomProvider"; | |
| 90 | - | |
| 91 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 92 | - // rawContent, | |
| 93 | - // extras, | |
| 94 | - // ); | |
| 95 | - | |
| 96 | - // expect(contextItems).toHaveLength(1); | |
| 97 | - // expect(renderedPrompt).toContain("Custom provider content"); | |
| 98 | - // expect(mockGetContextItems).toHaveBeenCalled(); | |
| 99 | - // }); | |
| 100 | - | |
| 101 | - // it("should handle multiple attachments", async () => { | |
| 102 | - // const rawContent = "Content with @file1.txt and @file2.txt"; | |
| 103 | - // addToTestDir([ | |
| 104 | - // ["file1.txt", "Content 1"], | |
| 105 | - // ["file2.txt", "Content 2"], | |
| 106 | - // ]); | |
| 107 | - | |
| 108 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 109 | - // rawContent, | |
| 110 | - // extras, | |
| 111 | - // ); | |
| 112 | - | |
| 113 | - // expect(contextItems).toHaveLength(2); | |
| 114 | - // expect(renderedPrompt).toContain("```file1.txt\nContent 1\n```"); | |
| 115 | - // expect(renderedPrompt).toContain("```file2.txt\nContent 2\n```"); | |
| 116 | - // }); | |
| 117 | - | |
| 118 | - // it("should handle failed attachment resolutions gracefully", async () => { | |
| 119 | - // const rawContent = "Content with @nonexistent"; | |
| 120 | - | |
| 121 | - // const [contextItems, renderedPrompt] = await getPromptV2ContextAttrs( | |
| 122 | - // rawContent, | |
| 123 | - // extras, | |
| 124 | - // ); | |
| 125 | - | |
| 126 | - // expect(contextItems).toHaveLength(0); | |
| 127 | - // expect(renderedPrompt).toBe("Content with @nonexistent\n\nUser input"); | |
| 128 | - // }); | |
| 69 | + it("should handle multiple attachments", async () => { | |
| 70 | + const rawContent = "Content with @file1.txt and @file2.txt"; | |
| 71 | + const command = getTestPromptV2Command(rawContent); | |
| 72 | + const requests = await getPromptV2ContextRequests(ideMessenger, command); | |
| 73 | + | |
| 74 | + expect(requests).toHaveLength(2); | |
| 75 | + expect(requests[0].provider).toBe("file"); | |
| 76 | + expect(requests[0].query).toBe(`${firstDir}/file1.txt`); | |
| 77 | + expect(requests[1].provider).toBe("file"); | |
| 78 | + expect(requests[1].query).toBe(`${firstDir}/file2.txt`); | |
| 79 | + }); | |
| 129 | 80 | }); |
modified gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.ts +3 −2
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | |
| 44 | 44 | // Files |
| 45 | 45 | const resolvedFileUri = await resolveRelativePathInDir( |
| 46 | - match[0], | |
| 46 | + match[1], | |
| 47 | 47 | ideMessenger.ide, |
| 48 | 48 | workspaceDirs, |
| 49 | 49 | ); |
| @@ -54,10 +54,11 @@ | ||
| 54 | 54 | return []; |
| 55 | 55 | } |
| 56 | 56 | visitedFiles.add(resolvedFileUri); |
| 57 | + // As well as any context providers it references | |
| 57 | 58 | await addContextRequestsRecursive(contents); |
| 58 | 59 | } |
| 59 | 60 | contextRequests.push({ |
| 60 | - provider: name, | |
| 61 | + provider: "file", | |
| 61 | 62 | query: resolvedFileUri, |
| 62 | 63 | }); |
| 63 | 64 | } |
added gui/src/components/mainInput/TipTapEditor/utils/renderSlashCommand.ts +155 −0
| @@ -0,0 +1,155 @@ | ||
| 1 | +import { MessagePart, RangeInFile, SlashCommandDescWithSource } from "core"; | |
| 2 | +import { stripImages } from "core/util/messageContent"; | |
| 3 | +import { IIdeMessenger } from "../../../../context/IdeMessenger"; | |
| 4 | +import { getRenderedV1Prompt } from "./renderPromptv1"; | |
| 5 | +import { getPromptV2ContextRequests } from "./renderPromptv2"; | |
| 6 | +import { GetContextRequest } from "./types"; | |
| 7 | + | |
| 8 | +/* | |
| 9 | +Slash commands can come from many sources. Render based on the source. | |
| 10 | +*/ | |
| 11 | +export async function renderSlashCommandPrompt( | |
| 12 | + ideMessenger: IIdeMessenger, | |
| 13 | + commandName: string | undefined, | |
| 14 | + parts: MessagePart[], | |
| 15 | + availableSlashCommands: SlashCommandDescWithSource[], | |
| 16 | + selectedCode: RangeInFile[], | |
| 17 | +): Promise<{ | |
| 18 | + slashedParts: MessagePart[]; | |
| 19 | + legacyCommandWithInput?: { | |
| 20 | + command: SlashCommandDescWithSource; | |
| 21 | + input: string; | |
| 22 | + }; | |
| 23 | + contextRequests: GetContextRequest[]; | |
| 24 | +}> { | |
| 25 | + const NO_COMMAND = { | |
| 26 | + slashedParts: parts, | |
| 27 | + legacyCommandWithInput: undefined, | |
| 28 | + contextRequests: [], | |
| 29 | + }; | |
| 30 | + if (!commandName) { | |
| 31 | + return NO_COMMAND; | |
| 32 | + } | |
| 33 | + const command = availableSlashCommands.find((c) => c.name === commandName); | |
| 34 | + if (!command) { | |
| 35 | + return NO_COMMAND; | |
| 36 | + } | |
| 37 | + | |
| 38 | + const nonTextParts = parts.filter((part) => part.type !== "text"); | |
| 39 | + const textParts = parts.filter((part) => part.type === "text"); | |
| 40 | + const slashedParts: MessagePart[] = [...nonTextParts]; | |
| 41 | + | |
| 42 | + const userInput = stripImages(textParts).trimStart(); | |
| 43 | + | |
| 44 | + const legacyCommandWithInput = command.isLegacy | |
| 45 | + ? { | |
| 46 | + command, | |
| 47 | + input: userInput, | |
| 48 | + } | |
| 49 | + : undefined; | |
| 50 | + | |
| 51 | + const contextRequests: GetContextRequest[] = []; | |
| 52 | + | |
| 53 | + switch (command.source) { | |
| 54 | + case "built-in-legacy": | |
| 55 | + case "config-ts-slash-command": | |
| 56 | + /** | |
| 57 | + * For legacy slash commands, we simply insert the text "/{name}" in front of the message | |
| 58 | + * And then parsing for this is done in core | |
| 59 | + */ | |
| 60 | + slashedParts.push({ | |
| 61 | + type: "text", | |
| 62 | + text: `/${command.name}${command.prompt ? " " + command.prompt : ""}${userInput ? " " + userInput : ""}`, | |
| 63 | + }); | |
| 64 | + break; | |
| 65 | + case "mcp-prompt": | |
| 66 | + // TODO add support for mcp prompt args using command.mcpArgs | |
| 67 | + const args: { [key: string]: string } = {}; | |
| 68 | + if (command.mcpArgs) { | |
| 69 | + command.mcpArgs.forEach((arg, i) => { | |
| 70 | + args[arg.name] = ""; | |
| 71 | + }); | |
| 72 | + } | |
| 73 | + const response = await ideMessenger.request("mcp/getPrompt", { | |
| 74 | + serverName: command.mcpServerName!, | |
| 75 | + promptName: command.name, | |
| 76 | + args: args, | |
| 77 | + }); | |
| 78 | + if (response.status === "success") { | |
| 79 | + slashedParts.push({ | |
| 80 | + type: "text", | |
| 81 | + text: `${response.content.prompt}${userInput ? "\n\n" + userInput : ""}`, | |
| 82 | + }); | |
| 83 | + } else { | |
| 84 | + throw new Error( | |
| 85 | + `Failed to get MCP prompt for slash command ${command.name}`, | |
| 86 | + ); | |
| 87 | + } | |
| 88 | + break; | |
| 89 | + case "prompt-file-v1": | |
| 90 | + case "prompt-file-v2": | |
| 91 | + case "yaml-prompt-block": | |
| 92 | + if (!command.prompt) { | |
| 93 | + console.warn(`Invalid/empty prompt from slash command ${command.name}`); | |
| 94 | + break; | |
| 95 | + } | |
| 96 | + let renderedPrompt: string; | |
| 97 | + if ( | |
| 98 | + command.source === "prompt-file-v1" || | |
| 99 | + command.prompt.includes("{{{ input }}}") | |
| 100 | + ) { | |
| 101 | + renderedPrompt = await getRenderedV1Prompt( | |
| 102 | + ideMessenger, | |
| 103 | + command, | |
| 104 | + userInput, | |
| 105 | + selectedCode, | |
| 106 | + ); | |
| 107 | + } else { | |
| 108 | + const promptFileCtxRequests = await getPromptV2ContextRequests( | |
| 109 | + ideMessenger, | |
| 110 | + command, | |
| 111 | + ); | |
| 112 | + contextRequests.push(...promptFileCtxRequests); | |
| 113 | + renderedPrompt = [command.prompt, userInput].join("\n\n"); | |
| 114 | + } | |
| 115 | + | |
| 116 | + if (renderedPrompt) { | |
| 117 | + slashedParts.push({ | |
| 118 | + type: "text", | |
| 119 | + text: renderedPrompt.trim(), // Includes user input | |
| 120 | + }); | |
| 121 | + } else { | |
| 122 | + console.warn( | |
| 123 | + `Invalid/empty prompt + input from slash command ${command.name}`, | |
| 124 | + ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + break; | |
| 128 | + case "built-in": | |
| 129 | + case "invokable-rule": | |
| 130 | + case "json-custom-command": | |
| 131 | + if (!command.prompt) { | |
| 132 | + console.warn(`Slash command ${command.name} is missing prompt`); | |
| 133 | + break; | |
| 134 | + } | |
| 135 | + const rendered = | |
| 136 | + `${command.prompt}${userInput ? "\n\n" + userInput : ""}`.trim(); | |
| 137 | + if (rendered) { | |
| 138 | + slashedParts.push({ | |
| 139 | + type: "text", | |
| 140 | + text: rendered, | |
| 141 | + }); | |
| 142 | + } else { | |
| 143 | + console.warn( | |
| 144 | + `Invalid/empty prompt + input from slash command ${command.name}`, | |
| 145 | + ); | |
| 146 | + } | |
| 147 | + default: | |
| 148 | + break; | |
| 149 | + } | |
| 150 | + return { | |
| 151 | + slashedParts, | |
| 152 | + legacyCommandWithInput, | |
| 153 | + contextRequests, | |
| 154 | + }; | |
| 155 | +} |
modified gui/src/components/mainInput/TipTapEditor/utils/resolveEditorContent.ts +2 −284
| @@ -1,7 +1,4 @@ | ||
| 1 | 1 | import { Dispatch } from "@reduxjs/toolkit"; |
| 2 | -import Image from "@tiptap/extension-image"; | |
| 3 | -import { Paragraph } from "@tiptap/extension-paragraph"; | |
| 4 | -import { Text } from "@tiptap/extension-text"; | |
| 5 | 2 | import { JSONContent } from "@tiptap/react"; |
| 6 | 3 | import { |
| 7 | 4 | ContextItemWithId, |
| @@ -11,25 +8,14 @@ | ||
| 11 | 8 | MessagePart, |
| 12 | 9 | RangeInFile, |
| 13 | 10 | SlashCommandDescWithSource, |
| 14 | - TextMessagePart, | |
| 15 | 11 | } from "core"; |
| 16 | -import { ctxItemToRifWithContents } from "core/commands/util"; | |
| 17 | 12 | import { stripImages } from "core/util/messageContent"; |
| 18 | -import { getUriFileExtension } from "core/util/uri"; | |
| 19 | 13 | import { IIdeMessenger } from "../../../../context/IdeMessenger"; |
| 20 | 14 | import { setIsGatheringContext } from "../../../../redux/slices/sessionSlice"; |
| 21 | -import { CodeBlock, Mention, PromptBlock } from "../extensions"; | |
| 22 | -import { getRenderedV1Prompt } from "./renderPromptv1"; | |
| 23 | -import { getPromptV2ContextRequests } from "./renderPromptv2"; | |
| 15 | +import { processEditorContent } from "./processEditorContent"; | |
| 16 | +import { renderSlashCommandPrompt } from "./renderSlashCommand"; | |
| 24 | 17 | import { GetContextRequest } from "./types"; |
| 25 | 18 | |
| 26 | -interface MentionAttrs { | |
| 27 | - label: string; | |
| 28 | - id: string; | |
| 29 | - itemType?: string; | |
| 30 | - query?: string; | |
| 31 | -} | |
| 32 | - | |
| 33 | 19 | interface ResolveEditorContentInput { |
| 34 | 20 | editorState: JSONContent; |
| 35 | 21 | modifiers: InputModifiers; |
| @@ -115,243 +101,6 @@ | ||
| 115 | 101 | }; |
| 116 | 102 | } |
| 117 | 103 | |
| 118 | -/** | |
| 119 | - * Processes editor content and extracts parts, context items, code, and slash commands | |
| 120 | - */ | |
| 121 | -function processEditorContent(editorState: JSONContent) { | |
| 122 | - const contextRequests: GetContextRequest[] = []; | |
| 123 | - const selectedCode: RangeInFile[] = []; | |
| 124 | - let slashCommandName: string | undefined; | |
| 125 | - | |
| 126 | - const parts = (editorState?.content || []).reduce<MessagePart[]>( | |
| 127 | - (parts, p) => { | |
| 128 | - switch (p.type) { | |
| 129 | - case PromptBlock.name: { | |
| 130 | - slashCommandName = resvolePromptBlock(p); | |
| 131 | - } | |
| 132 | - | |
| 133 | - case Paragraph.name: { | |
| 134 | - const [text, ctxItems] = resolveParagraph(p); | |
| 135 | - | |
| 136 | - contextRequests.push(...ctxItems); | |
| 137 | - | |
| 138 | - if (!text) return parts; | |
| 139 | - | |
| 140 | - // Merge with previous text part if possible | |
| 141 | - if (parts[parts.length - 1]?.type === "text") { | |
| 142 | - (parts[parts.length - 1] as TextMessagePart).text += "\n" + text; | |
| 143 | - return parts; | |
| 144 | - } | |
| 145 | - | |
| 146 | - return [...parts, { type: "text", text }]; | |
| 147 | - } | |
| 148 | - | |
| 149 | - case CodeBlock.name: { | |
| 150 | - if (!p.attrs?.item) { | |
| 151 | - console.warn("codeBlock has no item attribute"); | |
| 152 | - return parts; | |
| 153 | - } | |
| 154 | - | |
| 155 | - const contextItem = p.attrs.item as ContextItemWithId; | |
| 156 | - const rif = ctxItemToRifWithContents(contextItem, true); | |
| 157 | - selectedCode.push(rif); | |
| 158 | - | |
| 159 | - // If editing, only include in selectedCode | |
| 160 | - if (contextItem.editing) { | |
| 161 | - return parts; | |
| 162 | - } | |
| 163 | - | |
| 164 | - const fileExtension = getUriFileExtension(rif.filepath); | |
| 165 | - const codeText = [ | |
| 166 | - "\n\n```", | |
| 167 | - fileExtension, | |
| 168 | - " ", | |
| 169 | - contextItem.description, | |
| 170 | - "\n", | |
| 171 | - contextItem.content, | |
| 172 | - "\n```", | |
| 173 | - ].join(""); | |
| 174 | - | |
| 175 | - if (parts[parts.length - 1]?.type === "text") { | |
| 176 | - (parts[parts.length - 1] as TextMessagePart).text += | |
| 177 | - "\n" + codeText; | |
| 178 | - return parts; | |
| 179 | - } | |
| 180 | - | |
| 181 | - return [...parts, { type: "text", text: codeText }]; | |
| 182 | - } | |
| 183 | - | |
| 184 | - case Image.name: { | |
| 185 | - return [ | |
| 186 | - ...parts, | |
| 187 | - { | |
| 188 | - type: "imageUrl", | |
| 189 | - imageUrl: { url: p.attrs?.src }, | |
| 190 | - }, | |
| 191 | - ]; | |
| 192 | - } | |
| 193 | - | |
| 194 | - default: { | |
| 195 | - console.warn("Unexpected content type", p.type); | |
| 196 | - return parts; | |
| 197 | - } | |
| 198 | - } | |
| 199 | - }, | |
| 200 | - [], | |
| 201 | - ); | |
| 202 | - | |
| 203 | - return { parts, contextRequests, selectedCode, slashCommandName }; | |
| 204 | -} | |
| 205 | - | |
| 206 | -/** | |
| 207 | - * Processes slash commands and prepares the command with input | |
| 208 | - */ | |
| 209 | -async function renderSlashCommandPrompt( | |
| 210 | - ideMessenger: IIdeMessenger, | |
| 211 | - commandName: string | undefined, | |
| 212 | - parts: MessagePart[], | |
| 213 | - availableSlashCommands: SlashCommandDescWithSource[], | |
| 214 | - selectedCode: RangeInFile[], | |
| 215 | -): Promise<{ | |
| 216 | - slashedParts: MessagePart[]; | |
| 217 | - legacyCommandWithInput?: { | |
| 218 | - command: SlashCommandDescWithSource; | |
| 219 | - input: string; | |
| 220 | - }; | |
| 221 | - contextRequests: GetContextRequest[]; | |
| 222 | -}> { | |
| 223 | - const NO_COMMAND = { | |
| 224 | - slashedParts: parts, | |
| 225 | - legacyCommandWithInput: undefined, | |
| 226 | - contextRequests: [], | |
| 227 | - }; | |
| 228 | - if (!commandName) { | |
| 229 | - return NO_COMMAND; | |
| 230 | - } | |
| 231 | - const command = availableSlashCommands.find((c) => c.name === commandName); | |
| 232 | - if (!command) { | |
| 233 | - return NO_COMMAND; | |
| 234 | - } | |
| 235 | - | |
| 236 | - const nonTextParts = parts.filter((part) => part.type !== "text"); | |
| 237 | - const textParts = parts.filter((part) => part.type === "text"); | |
| 238 | - const slashedParts: MessagePart[] = [...nonTextParts]; | |
| 239 | - | |
| 240 | - const userInput = stripImages(textParts).trimStart(); | |
| 241 | - | |
| 242 | - const legacyCommandWithInput = command.isLegacy | |
| 243 | - ? { | |
| 244 | - command, | |
| 245 | - input: userInput, | |
| 246 | - } | |
| 247 | - : undefined; | |
| 248 | - | |
| 249 | - const contextRequests: GetContextRequest[] = []; | |
| 250 | - | |
| 251 | - switch (command.source) { | |
| 252 | - case "built-in-legacy": | |
| 253 | - case "config-ts-slash-command": | |
| 254 | - /** | |
| 255 | - * For legacy slash commands, we simply insert the text "/{name}" in front of the message | |
| 256 | - * And then parsing for this is done in core | |
| 257 | - */ | |
| 258 | - slashedParts.push({ | |
| 259 | - type: "text", | |
| 260 | - text: `/${command.name}${command.prompt ? " " + command.prompt : ""}${userInput ? " " + userInput : ""}`, | |
| 261 | - }); | |
| 262 | - break; | |
| 263 | - case "mcp-prompt": | |
| 264 | - // TODO add support for mcp prompt args using command.mcpArgs | |
| 265 | - const args: { [key: string]: string } = {}; | |
| 266 | - if (command.mcpArgs) { | |
| 267 | - command.mcpArgs.forEach((arg, i) => { | |
| 268 | - args[arg.name] = ""; | |
| 269 | - }); | |
| 270 | - } | |
| 271 | - const response = await ideMessenger.request("mcp/getPrompt", { | |
| 272 | - serverName: command.mcpServerName!, | |
| 273 | - promptName: command.name, | |
| 274 | - args: args, | |
| 275 | - }); | |
| 276 | - if (response.status === "success") { | |
| 277 | - slashedParts.push({ | |
| 278 | - type: "text", | |
| 279 | - text: `${response.content.prompt}${userInput ? "\n\n" + userInput : ""}`, | |
| 280 | - }); | |
| 281 | - } else { | |
| 282 | - throw new Error( | |
| 283 | - `Failed to get MCP prompt for slash command ${command.name}`, | |
| 284 | - ); | |
| 285 | - } | |
| 286 | - break; | |
| 287 | - case "prompt-file-v1": | |
| 288 | - case "prompt-file-v2": | |
| 289 | - case "yaml-prompt-block": | |
| 290 | - if (!command.prompt) { | |
| 291 | - console.warn(`Invalid/empty prompt from slash command ${command.name}`); | |
| 292 | - break; | |
| 293 | - } | |
| 294 | - let renderedPrompt: string; | |
| 295 | - if ( | |
| 296 | - command.source === "prompt-file-v1" || | |
| 297 | - command.prompt.includes("{{{ input }}}") | |
| 298 | - ) { | |
| 299 | - renderedPrompt = await getRenderedV1Prompt( | |
| 300 | - ideMessenger, | |
| 301 | - command, | |
| 302 | - userInput, | |
| 303 | - selectedCode, | |
| 304 | - ); | |
| 305 | - } else { | |
| 306 | - const promptFileCtxRequests = await getPromptV2ContextRequests( | |
| 307 | - ideMessenger, | |
| 308 | - command, | |
| 309 | - ); | |
| 310 | - contextRequests.push(...promptFileCtxRequests); | |
| 311 | - renderedPrompt = [command.prompt, userInput].join("\n\n"); | |
| 312 | - } | |
| 313 | - | |
| 314 | - if (renderedPrompt) { | |
| 315 | - slashedParts.push({ | |
| 316 | - type: "text", | |
| 317 | - text: renderedPrompt.trim(), // Includes user input | |
| 318 | - }); | |
| 319 | - } else { | |
| 320 | - console.warn( | |
| 321 | - `Invalid/empty prompt + input from slash command ${command.name}`, | |
| 322 | - ); | |
| 323 | - } | |
| 324 | - | |
| 325 | - break; | |
| 326 | - case "built-in": | |
| 327 | - case "invokable-rule": | |
| 328 | - case "json-custom-command": | |
| 329 | - if (!command.prompt) { | |
| 330 | - console.warn(`Slash command ${command.name} is missing prompt`); | |
| 331 | - break; | |
| 332 | - } | |
| 333 | - const rendered = | |
| 334 | - `${command.prompt}${userInput ? "\n\n" + userInput : ""}`.trim(); | |
| 335 | - if (rendered) { | |
| 336 | - slashedParts.push({ | |
| 337 | - type: "text", | |
| 338 | - text: rendered, | |
| 339 | - }); | |
| 340 | - } else { | |
| 341 | - console.warn( | |
| 342 | - `Invalid/empty prompt + input from slash command ${command.name}`, | |
| 343 | - ); | |
| 344 | - } | |
| 345 | - default: | |
| 346 | - break; | |
| 347 | - } | |
| 348 | - return { | |
| 349 | - slashedParts, | |
| 350 | - legacyCommandWithInput, | |
| 351 | - contextRequests, | |
| 352 | - }; | |
| 353 | -} | |
| 354 | - | |
| 355 | 104 | /** |
| 356 | 105 | * Gathers context items from various sources |
| 357 | 106 | */ |
| @@ -468,34 +217,3 @@ | ||
| 468 | 217 | ); |
| 469 | 218 | return deduplicatedOutputs; |
| 470 | 219 | } |
| 471 | - | |
| 472 | -function resvolePromptBlock(p: JSONContent): string | undefined { | |
| 473 | - return p.attrs?.item.name; | |
| 474 | -} | |
| 475 | - | |
| 476 | -function resolveParagraph(p: JSONContent): [string, GetContextRequest[]] { | |
| 477 | - const contextRequests: GetContextRequest[] = []; | |
| 478 | - | |
| 479 | - const text = (p.content || []) | |
| 480 | - .map((child) => { | |
| 481 | - switch (child.type) { | |
| 482 | - case Text.name: | |
| 483 | - return child.text; | |
| 484 | - case Mention.name: | |
| 485 | - const attrs = child.attrs as MentionAttrs; | |
| 486 | - contextRequests.push({ | |
| 487 | - provider: | |
| 488 | - attrs.itemType === "contextProvider" ? attrs.id : attrs.itemType!, | |
| 489 | - }); | |
| 490 | - return child.attrs?.renderInlineAs ?? child.attrs?.label; | |
| 491 | - | |
| 492 | - default: | |
| 493 | - console.warn("Unexpected child type", child.type); | |
| 494 | - return ""; | |
| 495 | - } | |
| 496 | - }) | |
| 497 | - .join("") | |
| 498 | - .trimStart(); | |
| 499 | - | |
| 500 | - return [text, contextRequests]; | |
| 501 | -} |
modified gui/src/context/MockIdeMessenger.ts +23 −1
| @@ -18,6 +18,14 @@ | ||
| 18 | 18 | [K in keyof FromWebviewProtocol]: FromWebviewProtocol[K][1]; |
| 19 | 19 | }>; |
| 20 | 20 | |
| 21 | +export type MockResponseHandler<T extends keyof FromWebviewProtocol> = ( | |
| 22 | + input: FromWebviewProtocol[T][0], | |
| 23 | +) => Promise<FromWebviewProtocol[T][1]>; | |
| 24 | + | |
| 25 | +type MockResponseHandlers = Partial<{ | |
| 26 | + [K in keyof FromWebviewProtocol]: MockResponseHandler<K>; | |
| 27 | +}>; | |
| 28 | + | |
| 21 | 29 | const DEFAULT_MOCK_CORE_RESPONSES: MockResponses = { |
| 22 | 30 | fileExists: true, |
| 23 | 31 | getCurrentFile: { |
| @@ -66,6 +74,7 @@ | ||
| 66 | 74 | "chatDescriber/describe": "Session summary", |
| 67 | 75 | applyToFile: undefined, |
| 68 | 76 | acceptDiff: undefined, |
| 77 | + readFile: "File contents", | |
| 69 | 78 | }; |
| 70 | 79 | |
| 71 | 80 | const DEFAULT_CHAT_RESPONSE: ChatMessage[] = [ |
| @@ -119,6 +128,7 @@ | ||
| 119 | 128 | } |
| 120 | 129 | |
| 121 | 130 | responses: MockResponses = { ...DEFAULT_MOCK_CORE_RESPONSES }; |
| 131 | + responseHandlers: MockResponseHandlers = {}; | |
| 122 | 132 | chatResponse: ChatMessage[] = DEFAULT_CHAT_RESPONSE; |
| 123 | 133 | chatStreamDelay: number = 0; |
| 124 | 134 | setChatResponseText(text: string): void { |
| @@ -164,6 +174,14 @@ | ||
| 164 | 174 | messageType: T, |
| 165 | 175 | data: FromWebviewProtocol[T][0], |
| 166 | 176 | ): Promise<WebviewSingleProtocolMessage<T>> { |
| 177 | + if (this.responseHandlers[messageType]) { | |
| 178 | + const content = await this.responseHandlers[messageType](data); | |
| 179 | + return { | |
| 180 | + status: "success", | |
| 181 | + content, | |
| 182 | + done: true, | |
| 183 | + }; | |
| 184 | + } | |
| 167 | 185 | if (messageType in this.responses) { |
| 168 | 186 | const content = this.responses[messageType]; |
| 169 | 187 | return { |
| @@ -173,7 +191,10 @@ | ||
| 173 | 191 | }; |
| 174 | 192 | } |
| 175 | 193 | console.error(messageType); |
| 176 | - throw new Error("MockIdeMessenger: No response defined for " + messageType); | |
| 194 | + throw new Error( | |
| 195 | + "MockIdeMessenger: No response handler or response defined for " + | |
| 196 | + messageType, | |
| 197 | + ); | |
| 177 | 198 | } |
| 178 | 199 | |
| 179 | 200 | respond<T extends keyof ToWebviewProtocol>( |
| @@ -195,6 +216,7 @@ | ||
| 195 | 216 | |
| 196 | 217 | resetMocks(): void { |
| 197 | 218 | this.responses = { ...DEFAULT_MOCK_CORE_RESPONSES }; |
| 219 | + this.responseHandlers = {}; | |
| 198 | 220 | this.chatResponse = DEFAULT_CHAT_RESPONSE; |
| 199 | 221 | this.chatStreamDelay = 0; |
| 200 | 222 | } |