Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat: add Otter — AI chat copilot with NL-to-SQL, tool calling, and dashboard generation (#1119)

Aman Agarwal682b32b48aad
diff --git a/docs/docs.json b/docs/docs.json
index 594d1097..4d2bce71 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -123,6 +123,12 @@
}
]
},
+ {
+ "group": "Otter (AI Copilot)",
+ "pages": [
+ "latest/openlit/chat"
+ ]
+ },
{
"group": "Prompts and Experiments",
"pages": [
diff --git a/docs/images/otter.svg b/docs/images/otter.svg
new file mode 100644
index 00000000..5573e115
--- /dev/null
+++ b/docs/images/otter.svg
@@ -0,0 +1,5 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="427 171 445 445">
+ <g>
+ <path d="M 477.31 545.63 C474.34,547.09 469.62,548.39 466.31,548.66 C461.36,549.08 460.02,548.8 457.28,546.82 C455.5,545.55 453.36,542.8 452.53,540.73 C448.77,531.44 451.8,519.33 464.55,492.8 C483.57,453.18 493.35,425.78 497.5,400.43 C499.49,388.28 499.4,383.37 497.12,380.15 C486.08,364.61 481.11,346.98 482.42,328 C482.78,322.77 483.48,316.72 483.98,314.55 C484.83,310.8 484.68,310.37 480.85,306.22 C463.91,287.84 473.71,261.31 497.75,260.48 L 504.03 260.26 L 513.27 251.33 C524.64,240.33 534.33,233.41 548,226.52 C570.64,215.12 590.83,210.12 617.61,209.29 C638.19,208.65 650.65,209.92 668.5,214.49 C695.54,221.41 718.34,233.76 737,251.59 L 745.5 259.71 L 752.76 260.3 C767.58,261.49 776.07,269.52 776.83,283.05 C777.34,291.95 775.12,298.57 769.59,304.68 L 765.33 309.39 L 766.81 315.94 C767.71,319.88 768.28,327.3 768.25,334.5 C768.18,349.62 765.93,358.82 758.89,372.6 L 753.74 382.7 L 755.44 392.1 C756.37,397.27 758.9,406.83 761.07,413.35 C763.23,419.87 765,425.6 765,426.08 C765,426.56 763.54,427.7 761.75,428.61 C759.96,429.53 756.42,432.01 753.87,434.13 L 749.24 437.99 L 748.16 435.24 C745.45,428.36 741.99,413.6 740.89,404.25 C740.23,398.61 739.41,394 739.08,394 C738.74,394 736.76,395.16 734.68,396.57 C729.29,400.24 724.53,400.85 723.04,398.08 C722,396.12 722.9,394.95 733.25,384.72 C743.03,375.05 745.18,372.32 748.77,365 C755.04,352.21 756.51,345.68 756.43,331 C756.36,318.28 755.23,312.05 750.92,300.51 C749,295.38 748.81,295.21 743.16,293.62 C739.97,292.73 735.14,292 732.41,292 L 727.45 292 L 727.84 296.17 C728.84,306.63 723.76,318.34 714.62,326.6 C706.88,333.61 700.26,336.16 688.26,336.77 C677.19,337.33 671.55,335.82 662.22,329.79 C658.77,327.55 655.81,325.9 655.64,326.11 C655.48,326.33 654.76,327.96 654.05,329.75 C652.42,333.85 641.94,343.71 635.72,347 C631.55,349.21 631,349.88 631,352.8 C631,362.63 642.61,371.15 653.91,369.6 C659.79,368.79 665.49,365.32 671.12,359.1 C675.96,353.77 678.69,352.83 681.43,355.57 C683.79,357.93 683.35,360.39 679.72,365.14 C675.18,371.09 668.97,375.59 662.44,377.65 C658.27,378.96 656.43,380.18 654.92,382.64 C651.48,388.2 642.43,395.7 636.95,397.52 C629.63,399.95 617.11,399.27 610.62,396.09 C605.48,393.57 595.52,384.22 594.48,380.94 C594.14,379.87 593.11,379 592.18,379 C588.57,379 577.75,372.84 572.99,368.09 C568.9,363.99 568,362.46 568,359.58 C568,356.01 569.97,354 573.45,354 C574.32,354 577.52,356.65 580.57,359.9 C589.97,369.92 599.58,372.32 609.68,367.16 C614.37,364.77 616.47,362.52 618.46,357.75 C621.11,351.41 620.57,349.8 614.88,347 C607.96,343.6 598.64,334.96 595.99,329.48 L 593.81 324.98 L 589.99 328.01 C587.89,329.67 583.55,332.26 580.34,333.77 C575.04,336.25 573.44,336.5 563,336.5 C552.3,336.5 551.09,336.3 545.65,333.59 C537.12,329.33 529.65,322.07 525.82,314.28 C523.02,308.6 522.58,306.67 522.55,299.74 L 522.5 291.75 L 515.5 292.34 C503.68,293.32 501.72,294.34 499.16,300.86 C493.96,314.13 492.4,334.91 495.63,347.94 C501.95,373.42 525.05,396.04 558.75,409.75 C563.06,411.51 564,412.33 564,414.32 C564,417.91 561.69,419.35 557.26,418.53 C550.88,417.35 534.94,410.21 525.87,404.47 C521.12,401.46 517.02,399 516.76,399 C516.49,399 515.96,402.94 515.57,407.75 C515.19,412.56 513.76,423.02 512.39,431 C503.88,480.71 505.18,498.7 518.35,513.66 C520.55,516.16 521.89,518.48 521.39,518.94 C520.9,519.39 518.7,520.32 516.5,521.02 C509,523.37 501.51,527.79 492,535.44 C486.77,539.65 480.16,544.23 477.31,545.63 ZM 742.7 572.93 C725.71,578.34 722.67,578.22 707,571.52 C691.39,564.86 670.04,559.17 650.57,556.48 C641.85,555.28 638.02,555.12 636.33,555.9 C631.96,557.89 620.88,560 614.83,560 C605.43,560 592,555.96 592,553.14 C592,551.17 598.4,549.69 610.12,548.95 C620.15,548.32 624.66,547.51 632.06,545.03 C650.89,538.72 663.42,530.03 662.81,523.69 L 662.5 520.5 L 653.38 520.8 C646.05,521.04 644.14,520.81 643.69,519.62 C642.85,517.44 644.72,516.47 652.39,515.12 C663.84,513.09 669,509.54 669,503.69 C669,499.25 667.09,498.7 654.76,499.6 C648.57,500.06 643.1,500.44 642.61,500.46 C642.12,500.48 641.22,499.7 640.61,498.72 C639.06,496.23 640.51,495.54 650.49,494.06 C659.72,492.69 662.7,490.77 661.49,486.97 C659.71,481.35 640.2,478.21 626.74,481.38 C616.45,483.81 610.79,487.22 600.07,497.48 C590.3,506.83 585.22,510 580,510 C576.92,510 574.5,507.75 575.31,505.64 C575.6,504.88 577.92,503.25 580.46,502.02 C583.06,500.76 586.91,497.58 589.29,494.72 C598.47,483.67 604.34,478.1 609.95,475.11 C616.65,471.53 615.69,474.37 619.05,448.5 C620.2,439.7 621.52,432.11 621.99,431.63 C622.46,431.15 625.69,430.46 629.17,430.1 C636.14,429.37 638,428.6 638,426.44 C638,425.47 639.11,425 641.43,425 C643.89,425 645.41,424.3 646.82,422.5 C648.88,419.89 651.86,419.26 653.65,421.05 C654.23,421.63 658.71,422.95 663.6,423.97 C668.5,424.99 676.1,427.03 680.5,428.49 C692.25,432.4 715.27,444.09 724.81,450.99 C729.39,454.29 733.56,457 734.09,457 C734.62,457 734.41,455.76 733.62,454.25 C729.48,446.28 717.94,435.38 707.5,429.57 C699.07,424.88 683.54,419.27 675.12,417.88 C667.08,416.55 664.72,414.85 667.32,412.25 C668.8,410.77 669.76,410.74 676.71,411.91 C694.86,414.95 715.88,424.29 726.27,433.91 C733.01,440.15 739.1,447.95 741.71,453.69 C742.66,455.79 743.53,457.65 743.64,457.84 C743.75,458.02 747.09,454.75 751.07,450.57 C766.85,433.95 787.92,425.59 815.97,424.8 L 826.45 424.5 L 826.98 427.5 C827.44,430.12 827.98,430.54 831.21,430.81 C834.87,431.11 836.97,433.62 835.53,435.95 C834.77,437.18 839.46,439.62 844.36,440.54 C846.45,440.93 848.4,441.89 848.7,442.67 C849.29,444.19 849.22,444.53 843.16,471.33 L 839.13 489.15 L 842.46 493.51 C847.94,500.7 849.51,506.11 849.41,517.5 C849.32,528.99 847.24,535.76 840.96,545 C837,550.84 834.17,553.29 826.97,557.1 C823.07,559.17 820.96,559.5 810.35,559.69 C792.15,560.01 769.09,564.52 742.7,572.93 ZM 494 286.05 C494,288.38 494.29,288.42 497.84,286.58 C501.55,284.66 514.83,282 520.71,282 C525.3,282 525.62,281.81 528.34,277.5 C532.05,271.62 538.16,266.38 545,263.2 C549.58,261.07 552.35,260.57 561.5,260.23 C573.93,259.77 579.83,261.32 588.74,267.38 C592.73,270.09 600.85,280.11 602.14,283.91 C602.38,284.6 604.2,284.43 607.5,283.41 C610.25,282.56 616.78,281.61 622,281.3 C631.11,280.77 633.12,281.04 645.5,284.5 C647.1,284.95 647.9,284.22 649.49,280.88 C652.47,274.58 659.17,267.72 665.75,264.21 C684.74,254.09 710.73,260.22 721.58,277.37 C724.34,281.73 724.35,281.74 731.42,282.28 C735.31,282.57 741.3,283.39 744.72,284.1 C748.14,284.81 751.26,285.07 751.65,284.68 C752.04,284.3 751.04,282.61 749.44,280.94 C747.01,278.41 746.78,277.75 748.02,276.97 C750.17,275.6 753.57,275.79 757.65,277.5 C762.23,279.41 762.28,279.4 760.97,276.95 C758.91,273.1 754,271.01 747,270.99 L 740.5 270.97 L 730.79 261.57 C714.87,246.18 696.1,234.95 675,228.21 C629.67,213.73 578.32,219.5 541.83,243.17 C533.17,248.79 520.62,259.56 514.59,266.56 C509.84,272.05 509.77,272.09 505.41,271.43 C499.12,270.49 494.47,271.65 490.48,275.15 C485.96,279.12 486.05,280.27 490.71,277.89 C494.61,275.9 498.63,275.48 500.77,276.83 C501.67,277.41 500.89,278.56 498.02,280.87 C495.8,282.65 494,284.97 494,286.05 ZM 676.35 326.62 C678.72,327.38 683.73,327.86 688,327.72 C705.32,327.14 718.01,314.57 717.99,298 C717.97,280.97 704.96,269 686.5,269 C673.6,269 664,274.03 658.29,283.77 C654.84,289.66 653.91,301.45 656.43,307.51 C660.23,316.63 667.71,323.81 676.35,326.62 ZM 551.36 325.92 C559.37,329 569.02,328.7 576.5,325.15 C588.95,319.22 596.04,307.48 594.59,295.17 C593.32,284.38 588.09,276.87 578.45,272 C574.15,269.83 572.19,269.5 563.5,269.51 C551.79,269.51 547.05,271.21 540.3,277.8 C533.55,284.39 532.5,287.12 532.5,298 C532.51,305.88 532.88,308.21 534.71,311.63 C537.66,317.15 545.05,323.49 551.36,325.92 ZM 710 561.32 C710.5,561.54 710.96,561.75 711.37,561.94 C712.61,562.53 713.45,562.92 714.08,562.71 C715.59,562.21 715.98,558.31 718.03,545.5 C720.46,530.36 730.83,469.51 731.52,466.34 C731.9,464.61 730.67,463.2 725.25,459.19 C710.98,448.62 692.11,439.24 674.5,433.96 C661.64,430.1 652.24,428.6 648,429.71 L 644.5 430.64 L 649.25 432.32 C651.86,433.24 656.03,434.71 658.5,435.58 C660.97,436.45 663,437.59 663,438.11 C663,440.25 659.4,440.13 651.18,437.71 C644.68,435.8 641.18,435.29 637.25,435.7 C634.36,436.01 632,436.55 632,436.92 C632,437.29 634.14,438.15 636.75,438.85 C639.36,439.55 650.5,443.52 661.5,447.68 C672.5,451.83 689.83,458.37 700,462.19 C710.17,466.02 718.8,469.45 719.17,469.83 C719.53,470.2 719.2,474.1 718.42,478.5 C717.13,485.83 715.78,494.49 708.42,542.88 C707.05,551.89 706.06,559.39 706.22,559.54 C706.37,559.68 708.08,560.49 710,561.32 ZM 798.42 545.12 C803.06,547.32 805.95,548 810.63,548 C822,548 829.55,542.68 835.02,530.8 C837.89,524.59 838.37,522.46 838.35,516.02 C838.33,507.89 836.36,502.52 831.83,498.3 C823.7,490.71 801.47,488.29 797.38,494.55 C794.84,498.41 797.34,501.27 805.17,503.46 C813.23,505.71 817,507.65 817,509.56 C817,512.16 814.24,512.29 805.97,510.05 C795.35,507.19 789.97,507.55 788.2,511.25 C785.63,516.64 789.98,521.43 801.07,525.45 C805.84,527.18 807.5,528.26 807.5,529.64 C807.5,531.91 802.94,532.06 795.9,530.01 C789.87,528.25 788,529.03 788,533.33 C788,538.06 791.24,541.72 798.42,545.12 ZM 734.41 562.16 L 733.88 564.82 L 737.87 563.65 C740.07,563 742.12,562.21 742.44,561.89 C742.99,561.35 753.3,515.81 759.99,484.43 L 763.2 469.35 L 779.35 462.98 C798.12,455.58 817.34,449.09 828,446.54 C834.93,444.88 839.24,443 836.1,443 C835.39,443 833.53,442.09 831.97,440.98 L 829.12 438.95 L 815.93 440.97 C796.67,443.93 772.13,452.57 758.88,461.08 C754.51,463.88 754.09,464.53 752.6,470.79 C749.22,485.04 743,515.47 739.06,537 C736.8,549.38 734.71,560.7 734.41,562.16 ZM 693.58 312.1 C688.61,314.36 683.5,314.45 679.5,312.34 C669.88,307.26 666.61,298.61 671.04,289.92 C676.07,280.07 690.22,278.24 698.78,286.34 C706.97,294.1 704.32,307.22 693.58,312.1 ZM 572.41 311.19 C567.6,314 561.2,314.39 556.46,312.18 C548.32,308.37 544.57,300.7 546.96,292.74 C552.2,275.26 580.03,278.85 579.98,297 C579.96,302.54 576.68,308.7 572.41,311.19 ZM 603.42 308.15 L 602.88 310.81 L 607.11 309.35 C613.9,307.01 629.28,306.54 638,308.4 L 638.01 308.4 C644.5,309.78 646.34,310.18 646.62,309.68 C646.74,309.48 646.61,309.14 646.43,308.67 L 646.43 308.67 C646.15,307.94 645.6,304.46 645.21,300.93 C644.58,295.23 644.19,294.38 641.71,293.19 C634.07,289.53 615.66,289.7 607.64,293.5 C604.77,294.86 604.48,295.45 604.23,300.25 C604.08,303.14 603.72,306.69 603.42,308.15 ZM 615.26 387.03 C623.63,390.83 634.77,389.15 641.26,383.1 L 645.26 379.36 L 639.24 377.17 C635.62,375.85 631.6,373.41 629.19,371.07 L 625.18 367.17 L 620.62 371.54 C617.88,374.16 614.36,376.38 611.79,377.12 C609.43,377.79 607.11,378.68 606.63,379.09 C605.35,380.2 610.36,384.81 615.26,387.03 ZM 757.05 454.39 C755.16,456.37 753.89,458 754.25,458 C754.6,458 757.77,456.19 761.29,453.98 C773.24,446.49 791.62,440.35 813.27,436.61 C823.82,434.79 824.84,434.28 821.97,432.27 C817.36,429.03 792.1,433.37 777.79,439.85 C770.39,443.19 762.36,448.82 757.05,454.39 ZM 730.43 351.43 C729.56,352.29 728.78,352.97 728.68,352.93 C719.66,349.24 716.79,348.69 705.74,348.54 C692.98,348.38 690.17,347.4 693.33,344.24 C696.25,341.32 712.91,341.22 722.51,344.07 C730.74,346.51 733.13,348.73 730.43,351.43 ZM 528 350.8 C524.98,352.01 522.51,352.98 522.5,352.97 C522.5,352.95 521.71,352.48 520.75,351.92 C513.58,347.75 530.05,341.55 546.69,342.16 C556.45,342.52 559.3,343.71 557.45,346.65 C556.64,347.93 554.24,348.27 544.97,348.42 C535.81,348.57 532.39,349.05 528,350.8 ZM 722.56 366.81 C721.84,369.06 718.62,369.81 717.49,367.98 C716.25,365.98 707.26,362.13 700.94,360.9 C692.97,359.35 690.81,357.76 693.28,355.3 C694.78,353.79 695.75,353.72 701.43,354.7 C708.27,355.87 716.93,359.42 720.88,362.67 C722.64,364.12 723.07,365.19 722.56,366.81 ZM 537.05 365.56 L 536.91 365.63 C531.84,368.32 530.31,369.14 529.29,368.71 C528.84,368.53 528.5,368.1 527.98,367.48 C526.24,365.38 528.89,362.59 536.13,358.94 C542.1,355.92 553.68,353.28 556.38,354.32 C558.84,355.26 558.4,358.87 555.75,359.47 C554.51,359.75 551.26,360.46 548.52,361.05 C545.78,361.63 540.62,363.66 537.05,365.56 ZM 619.25 320.78 C620.49,321.38 622.76,321.9 624.3,321.93 C627.81,322.02 633.26,319.28 632.74,317.69 C632.3,316.38 623,315.53 619.3,316.46 C616.37,317.2 616.34,319.38 619.25,320.78 ZM 561.15 295.4 C564.75,297.91 568.69,294.88 567.58,290.44 C567.11,288.58 566.3,288 564.18,288 C559.04,288 557.16,292.6 561.15,295.4 ZM 683.09 294.61 C684.68,296.52 688.97,296.02 690.39,293.75 C692.11,291.01 689.95,288.5 685.89,288.5 C683.11,288.5 682.44,288.92 682.17,290.81 C681.99,292.07 682.4,293.78 683.09,294.61 Z" fill="currentColor" />
+ </g>
+</svg>
\ No newline at end of file
diff --git a/docs/latest/openlit/chat.mdx b/docs/latest/openlit/chat.mdx
new file mode 100644
index 00000000..8d1b8581
--- /dev/null
+++ b/docs/latest/openlit/chat.mdx
@@ -0,0 +1,188 @@
+---
+title: 'Otter — AI Chat Copilot'
+description: 'Meet Otter, your AI copilot for querying data, managing resources, and creating dashboards'
+---
+
+Meet **Otter** 🦦 — your AI-powered observability copilot built into OpenLIT. Otter lets you interact with your observability platform using natural language. It can query your telemetry data, create and manage platform resources, and generate complete dashboards — all through conversation.
+
+## Meet Otter
+
+Otter is always just a click away via the floating chat button (bottom-right corner of every page) or from the **Chat** entry in the sidebar. Otter paddles through your traces, metrics, and logs so you don't have to.
+
+<Frame>
+ <img src="/images/otter.svg" width="80" />
+</Frame>
+
+## Key Features
+
+- **Natural Language Queries**: Ask questions about your traces, metrics, costs, and tokens — the AI converts them to ClickHouse SQL and executes them
+- **Resource Management**: Create and manage rules, contexts, prompts, vault secrets, and custom models through conversation
+- **Dashboard Generation**: Describe the dashboard you want and get a complete importable layout with working queries
+- **SQL Execution**: Generated queries run automatically with results displayed inline as tables and charts
+- **Save as Widget**: Save any query result as a reusable dashboard widget
+
+## Get Started
+
+<Steps>
+ <Step title="Configure AI Provider">
+ Navigate to **Chat → Settings** (gear icon in chat sidebar) and configure:
+ 1. **Provider** — Select from 14 supported providers (OpenAI, Anthropic, Google, etc.)
+ 2. **Model** — Choose a model from the provider's list (includes custom models from Manage Models)
+ 3. **API Key** — Select a vault secret containing your provider's API key
+
+ <Info>
+ Store your API key in the **Vault** first, then select it in Chat Settings.
+ </Info>
+ </Step>
+
+ <Step title="Start a Conversation">
+ Click **New Chat** in the sidebar or use the floating chat button available on any page.
+
+ Try example prompts like:
+ - "What are my top 5 most expensive models this week?"
+ - "Show me error rate by provider over the last 24 hours"
+ - "Create a rule that triggers when request duration exceeds 5 seconds"
+ </Step>
+
+ <Step title="View Results">
+ - **SQL queries** appear in a collapsible accordion with syntax highlighting
+ - **Query results** display automatically as tables, bar charts, line charts, or pie charts
+ - **Entity cards** link to created resources (rules, contexts, prompts)
+ - **Dashboard cards** show an import button for generated dashboards
+ </Step>
+</Steps>
+
+## Data Queries
+
+Ask questions about your OpenTelemetry data and the AI generates ClickHouse SQL with dynamic time filters.
+
+### Supported Tables
+
+| Table | Description |
+|-------|-------------|
+| `otel_traces` | Traces and spans (LLM calls, API requests, etc.) |
+| `otel_metrics_gauge` | Gauge metrics (GPU temperature, memory, etc.) |
+| `otel_metrics_sum` | Counter/sum metrics (token counts, etc.) |
+| `otel_metrics_histogram` | Histogram metrics (request durations, etc.) |
+
+### Example Queries
+
+```
+"How many requests did I get today?"
+"What is my total cost breakdown by provider?"
+"Show me the slowest requests in the last hour"
+"Which models have the highest error rate?"
+```
+
+### Save as Widget
+
+Click **Save as Widget** on any query result to:
+1. Choose a widget type (Stat Card, Bar Chart, Line Chart, Pie Chart, Area Chart, Table)
+2. Select a dashboard to add it to
+3. The query is automatically converted to use dynamic time filters (`{{filter.timeLimit.start/end}}`)
+
+## Resource Management
+
+The chat can create and manage platform resources through natural language. All operations execute immediately.
+
+### Supported Operations
+
+| Resource | Operations |
+|----------|-----------|
+| **Rules** | Create, update, delete, list, get details, link/unlink entities |
+| **Contexts** | Create, update, delete, list |
+| **Prompts** | Create, update version, delete, list |
+| **Vault Secrets** | Create, update, delete, list |
+| **Custom Models** | Create, update, delete, list |
+
+### Examples
+
+```
+"Create a vault secret called OPENAI_KEY with value sk-..."
+"Create a rule named high-cost-alert that triggers when cost exceeds 0.1"
+"Create a prompt template for customer feedback summarization"
+"Link the customer-context to the high-cost-alert rule"
+"List all my vault secrets"
+```
+
+<Note>
+ Vault key names are automatically normalized to UPPER_SNAKE_CASE (e.g., "my openai key" becomes "MY_OPENAI_KEY").
+</Note>
+
+## Dashboard Generation
+
+Describe the dashboard you want and the AI generates a complete importable layout.
+
+### How it Works
+
+1. Ask: *"Create a GPU monitoring dashboard with utilization, memory, and temperature widgets"*
+2. The AI generates a dashboard JSON with:
+ - Widget definitions (type, title, SQL query, display properties)
+ - Grid layout positions
+ - Dynamic time filters (`{{filter.timeLimit.start/end}}`)
+3. A **dashboard card** appears with:
+ - Dashboard title and widget count
+ - Widget preview chips
+ - **Import Dashboard** button
+ - **Download JSON** button
+4. Click **Import Dashboard** to create it instantly
+
+### Dashboard Queries
+
+All generated queries use the Mustache template pattern for dynamic time filtering:
+
+```sql
+WITH
+ parseDateTimeBestEffort('{{filter.timeLimit.start}}') AS start_time,
+ parseDateTimeBestEffort('{{filter.timeLimit.end}}') AS end_time
+SELECT ...
+FROM otel_traces
+WHERE Timestamp >= start_time AND Timestamp <= end_time
+```
+
+This ensures dashboards respond to the time filter controls in the UI.
+
+## Cost & Token Tracking
+
+Each conversation tracks:
+- **Per-message**: Input tokens, output tokens, estimated cost
+- **Per-conversation**: Accumulated totals displayed in the sidebar
+
+The cost formula:
+```
+cost = (input_tokens / 1M) × input_price + (output_tokens / 1M) × output_price
+```
+
+Hover over the info icon next to the chat input to see your current provider, model, and pricing details.
+
+## Conversation Management
+
+- **URL-based navigation**: Each conversation has a unique URL (`/chat?id=<uuid>`)
+- **Auto-generated titles**: Titles are generated after the first message exchange
+- **Persistent history**: Conversations and messages are stored in ClickHouse
+- **Search**: Filter conversations by title in the sidebar
+- **Delete**: Hover over a conversation and click the trash icon
+
+## Configuration
+
+### Chat Settings
+
+Access via the gear icon in the chat sidebar or navigate to `/chat/settings`.
+
+| Setting | Description |
+|---------|-------------|
+| **Provider** | LLM provider (OpenAI, Anthropic, Google, Mistral, Cohere, Groq, etc.) |
+| **Model** | Model from the provider's list (includes custom models) |
+| **API Key** | Vault secret reference for the provider's API key |
+
+### Supported Providers
+
+OpenAI, Anthropic, Google, Mistral, Cohere, Groq, Perplexity, Azure OpenAI, Together AI, Fireworks AI, DeepSeek, xAI, Hugging Face, Replicate
+
+## Security
+
+- All SQL queries execute with `readonly` mode — no write operations possible
+- Queries are validated against an allowlist of tables (`otel_traces`, `otel_metrics_*`)
+- DDL/DML statements (INSERT, UPDATE, DELETE, DROP, etc.) are rejected
+- API keys are stored in the Vault and never sent to the client
+- Query results have a LIMIT cap of 10,000 rows
diff --git a/src/client/src/__tests__/lib/platform/chat/config.test.ts b/src/client/src/__tests__/lib/platform/chat/config.test.ts
new file mode 100644
index 00000000..478c4b09
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/config.test.ts
@@ -0,0 +1,110 @@
+jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));
+jest.mock('@/lib/platform/chat/table-details', () => ({
+ OPENLIT_CHAT_CONFIG_TABLE: 'openlit_chat_config',
+}));
+jest.mock('@/lib/platform/vault', () => ({
+ getSecrets: jest.fn(),
+}));
+jest.mock('@/utils/sanitizer', () => ({
+ __esModule: true,
+ default: {
+ sanitizeValue: jest.fn((v: string) => v),
+ },
+}));
+
+import { getChatConfig, upsertChatConfig, getChatConfigWithApiKey } from '@/lib/platform/chat/config';
+import { dataCollector } from '@/lib/platform/common';
+import { getSecrets } from '@/lib/platform/vault';
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ (dataCollector as jest.Mock).mockResolvedValue({ data: [], err: null });
+});
+
+describe('getChatConfig', () => {
+ it('returns undefined when no config exists', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ data: [] });
+ const { data } = await getChatConfig();
+ expect(data).toBeUndefined();
+ });
+
+ it('returns config when it exists', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({
+ data: [{ provider: 'openai', model: 'gpt-4o', vaultId: 'v1' }],
+ });
+ const { data } = await getChatConfig();
+ expect(data?.provider).toBe('openai');
+ expect(data?.model).toBe('gpt-4o');
+ });
+
+ it('passes databaseConfigId to dataCollector', async () => {
+ await getChatConfig('db-123');
+ expect(dataCollector).toHaveBeenCalledWith(
+ expect.objectContaining({ query: expect.stringContaining('openlit_chat_config') }),
+ 'query',
+ 'db-123'
+ );
+ });
+
+ it('returns error when dataCollector fails', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: 'connection error' });
+ const { err } = await getChatConfig();
+ expect(err).toBe('connection error');
+ });
+});
+
+describe('upsertChatConfig', () => {
+ it('inserts config via dataCollector insert', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ const { data } = await upsertChatConfig({
+ provider: 'anthropic',
+ model: 'claude-3',
+ vaultId: 'v2',
+ });
+ expect(data).toBe('Chat configuration saved successfully');
+ expect(dataCollector).toHaveBeenCalledWith(
+ expect.objectContaining({ table: 'openlit_chat_config' }),
+ 'insert',
+ undefined
+ );
+ });
+
+ it('returns error on failure', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: 'insert failed' });
+ const { err } = await upsertChatConfig({
+ provider: 'openai',
+ model: 'gpt-4o',
+ vaultId: 'v1',
+ });
+ expect(err).toBe('insert failed');
+ });
+});
+
+describe('getChatConfigWithApiKey', () => {
+ it('returns error when no config exists', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ data: [] });
+ const { err } = await getChatConfigWithApiKey();
+ expect(err).toContain('not found');
+ });
+
+ it('returns error when vault secret not found', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({
+ data: [{ provider: 'openai', model: 'gpt-4o', vaultId: 'v1' }],
+ });
+ (getSecrets as jest.Mock).mockResolvedValue({ data: [] });
+ const { err } = await getChatConfigWithApiKey();
+ expect(err).toContain('not found');
+ });
+
+ it('returns config with apiKey when vault secret exists', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({
+ data: [{ provider: 'openai', model: 'gpt-4o', vaultId: 'v1' }],
+ });
+ (getSecrets as jest.Mock).mockResolvedValue({
+ data: [{ id: 'v1', value: 'sk-test-key' }],
+ });
+ const { data } = await getChatConfigWithApiKey();
+ expect(data?.apiKey).toBe('sk-test-key');
+ expect(data?.provider).toBe('openai');
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/conversation.test.ts b/src/client/src/__tests__/lib/platform/chat/conversation.test.ts
new file mode 100644
index 00000000..c4e602a5
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/conversation.test.ts
@@ -0,0 +1,159 @@
+jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));
+jest.mock('@/lib/platform/chat/table-details', () => ({
+ OPENLIT_CHAT_CONVERSATION_TABLE: 'openlit_chat_conversation',
+ OPENLIT_CHAT_MESSAGE_TABLE: 'openlit_chat_message',
+}));
+jest.mock('@/utils/sanitizer', () => ({
+ __esModule: true,
+ default: {
+ sanitizeValue: jest.fn((v: string) => v),
+ },
+}));
+
+import {
+ getConversations,
+ getConversationWithMessages,
+ createConversation,
+ deleteConversation,
+ addMessage,
+ updateMessage,
+ updateConversation,
+ getConversationMessages,
+} from '@/lib/platform/chat/conversation';
+import { dataCollector } from '@/lib/platform/common';
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ (dataCollector as jest.Mock).mockResolvedValue({ data: [], err: null });
+});
+
+describe('getConversations', () => {
+ it('returns conversations ordered by updated_at', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({
+ data: [
+ { id: 'c1', title: 'Test', totalCost: 0.01, updatedAt: '2025-01-01' },
+ ],
+ });
+ const { data } = await getConversations();
+ expect(data).toHaveLength(1);
+ expect(data![0].id).toBe('c1');
+ });
+
+ it('queries with LIMIT 50', async () => {
+ await getConversations();
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain('LIMIT 50');
+ });
+
+ it('passes databaseConfigId', async () => {
+ await getConversations('db-1');
+ expect(dataCollector).toHaveBeenCalledWith(expect.any(Object), 'query', 'db-1');
+ });
+});
+
+describe('createConversation', () => {
+ it('inserts into conversation table', async () => {
+ (dataCollector as jest.Mock)
+ .mockResolvedValueOnce({ err: null, data: {} }) // insert
+ .mockResolvedValueOnce({ data: [{ id: 'new-id' }] }); // select latest
+ const { data } = await createConversation('Test', 'openai', 'gpt-4');
+ expect(data).toBe('new-id');
+ });
+
+ it('returns error on insert failure', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: 'fail' });
+ const { err } = await createConversation('Test', 'openai', 'gpt-4');
+ expect(err).toBe('fail');
+ });
+});
+
+describe('deleteConversation', () => {
+ it('deletes messages first, then conversation', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ await deleteConversation('c1');
+ expect(dataCollector).toHaveBeenCalledTimes(2);
+ const firstCall = (dataCollector as jest.Mock).mock.calls[0][0].query;
+ expect(firstCall).toContain('openlit_chat_message');
+ const secondCall = (dataCollector as jest.Mock).mock.calls[1][0].query;
+ expect(secondCall).toContain('openlit_chat_conversation');
+ });
+});
+
+describe('addMessage', () => {
+ it('inserts message with all fields', async () => {
+ (dataCollector as jest.Mock)
+ .mockResolvedValueOnce({ err: null, data: {} }) // insert
+ .mockResolvedValueOnce({ data: [{ id: 'msg-1' }] }); // select latest
+ const { data } = await addMessage({
+ conversationId: 'c1',
+ role: 'user',
+ content: 'Hello',
+ promptTokens: 10,
+ completionTokens: 20,
+ cost: 0.001,
+ });
+ expect(data).toBe('msg-1');
+ const insertCall = (dataCollector as jest.Mock).mock.calls[0];
+ expect(insertCall[0].table).toBe('openlit_chat_message');
+ expect(insertCall[0].values[0].role).toBe('user');
+ expect(insertCall[0].values[0].prompt_tokens).toBe(10);
+ });
+});
+
+describe('updateMessage', () => {
+ it('updates message with query result', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ await updateMessage('msg-1', { queryResult: '[]', queryRowsRead: 5 });
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain('ALTER TABLE');
+ expect(query).toContain('query_result');
+ expect(query).toContain('query_rows_read = 5');
+ });
+
+ it('skips update when no fields provided', async () => {
+ await updateMessage('msg-1', {});
+ expect(dataCollector).not.toHaveBeenCalled();
+ });
+});
+
+describe('updateConversation', () => {
+ it('updates title', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ await updateConversation('c1', { title: 'New Title' });
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain("title = 'New Title'");
+ expect(query).toContain('updated_at = now()');
+ });
+
+ it('accumulates token counts', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ await updateConversation('c1', { addPromptTokens: 100, addCompletionTokens: 50, addCost: 0.01 });
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain('total_prompt_tokens = total_prompt_tokens + 100');
+ expect(query).toContain('total_completion_tokens = total_completion_tokens + 50');
+ expect(query).toContain('total_cost = total_cost + 0.01');
+ });
+
+ it('increments message count', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+ await updateConversation('c1', { incrementMessages: true });
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain('total_messages = total_messages + 1');
+ });
+});
+
+describe('getConversationMessages', () => {
+ it('returns messages for a conversation', async () => {
+ (dataCollector as jest.Mock).mockResolvedValue({
+ data: [{ id: 'm1', role: 'user', content: 'Hello' }],
+ });
+ const { data } = await getConversationMessages('c1');
+ expect(data).toHaveLength(1);
+ });
+
+ it('respects limit parameter', async () => {
+ await getConversationMessages('c1', 5);
+ const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];
+ expect(query).toContain('LIMIT 5');
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts b/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts
new file mode 100644
index 00000000..664fbcf8
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts
@@ -0,0 +1,134 @@
+jest.mock('@/lib/platform/manage-dashboard/widget', () => ({
+ createWidget: jest.fn(),
+}));
+jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));
+jest.mock('@/lib/platform/manage-dashboard/table-details', () => ({
+ OPENLIT_BOARD_WIDGET_TABLE_NAME: 'openlit_board_widget',
+}));
+jest.mock('@/utils/sanitizer', () => ({
+ __esModule: true,
+ default: { sanitizeValue: jest.fn((v: string) => v) },
+}));
+
+import { saveQueryAsWidget, convertToMustacheTemplate } from '@/lib/platform/chat/save-widget';
+import { createWidget } from '@/lib/platform/manage-dashboard/widget';
+import { dataCollector } from '@/lib/platform/common';
+
+beforeEach(() => {
+ jest.clearAllMocks();
+});
+
+describe('convertToMustacheTemplate', () => {
+ it('replaces parseDateTimeBestEffort start_time', () => {
+ const query = "parseDateTimeBestEffort('2025-01-01') AS start_time";
+ const result = convertToMustacheTemplate(query);
+ expect(result).toContain("{{filter.timeLimit.start}}");
+ expect(result).toContain("AS start_time");
+ });
+
+ it('replaces parseDateTimeBestEffort end_time', () => {
+ const query = "parseDateTimeBestEffort('2025-12-31') AS end_time";
+ const result = convertToMustacheTemplate(query);
+ expect(result).toContain("{{filter.timeLimit.end}}");
+ });
+
+ it('replaces now() - INTERVAL pattern', () => {
+ const query = "WHERE Timestamp >= now() - INTERVAL 24 HOUR";
+ const result = convertToMustacheTemplate(query);
+ expect(result).toContain("{{filter.timeLimit.start}}");
+ });
+
+ it('replaces toStartOfDay(now())', () => {
+ const query = "WHERE Timestamp >= toStartOfDay(now())";
+ const result = convertToMustacheTemplate(query);
+ expect(result).toContain("{{filter.timeLimit.start}}");
+ });
+
+ it('replaces hardcoded date strings', () => {
+ const query = "WHERE Timestamp >= '2025-01-01 00:00:00' AND Timestamp <= '2025-12-31 23:59:59'";
+ const result = convertToMustacheTemplate(query);
+ expect(result).toContain("{{filter.timeLimit.start}}");
+ expect(result).toContain("{{filter.timeLimit.end}}");
+ });
+
+ it('preserves query without time references', () => {
+ const query = "SELECT COUNT(*) FROM otel_traces LIMIT 10";
+ expect(convertToMustacheTemplate(query)).toBe(query);
+ });
+});
+
+describe('saveQueryAsWidget', () => {
+ it('creates a widget successfully', async () => {
+ (createWidget as jest.Mock).mockResolvedValue({ data: { id: 'w1', title: 'Test' } });
+
+ const result = await saveQueryAsWidget({
+ title: 'Test Widget',
+ type: 'BAR_CHART',
+ query: 'SELECT * FROM otel_traces LIMIT 10',
+ });
+
+ expect(result.data).toBeDefined();
+ expect(createWidget).toHaveBeenCalledTimes(1);
+ const widgetArg = (createWidget as jest.Mock).mock.calls[0][0];
+ expect(widgetArg.title).toBe('Test Widget');
+ expect(widgetArg.type).toBe('BAR_CHART');
+ });
+
+ it('returns error when createWidget fails', async () => {
+ (createWidget as jest.Mock).mockResolvedValue({ err: 'creation failed' });
+
+ const result = await saveQueryAsWidget({
+ title: 'Test',
+ type: 'TABLE',
+ query: 'SELECT 1',
+ });
+
+ expect(result.err).toBe('creation failed');
+ });
+
+ it('links widget to board when boardId provided', async () => {
+ (createWidget as jest.Mock).mockResolvedValue({ data: { id: 'w1' } });
+ (dataCollector as jest.Mock).mockResolvedValue({ err: null });
+
+ await saveQueryAsWidget({
+ title: 'Test',
+ type: 'TABLE',
+ query: 'SELECT 1',
+ boardId: 'board-123',
+ });
+
+ expect(dataCollector).toHaveBeenCalledWith(
+ expect.objectContaining({ table: 'openlit_board_widget' }),
+ 'insert'
+ );
+ const insertValues = (dataCollector as jest.Mock).mock.calls[0][0].values[0];
+ expect(insertValues.board_id).toBe('board-123');
+ });
+
+ it('returns warning when board linking fails', async () => {
+ (createWidget as jest.Mock).mockResolvedValue({ data: { id: 'w1' } });
+ (dataCollector as jest.Mock).mockResolvedValue({ err: 'link failed' });
+
+ const result = await saveQueryAsWidget({
+ title: 'Test',
+ type: 'TABLE',
+ query: 'SELECT 1',
+ boardId: 'board-123',
+ });
+
+ expect(result.data).toBeDefined();
+ expect(result.warning).toContain('could not be added');
+ });
+
+ it('does not link to board when boardId is absent', async () => {
+ (createWidget as jest.Mock).mockResolvedValue({ data: { id: 'w1' } });
+
+ await saveQueryAsWidget({
+ title: 'Test',
+ type: 'TABLE',
+ query: 'SELECT 1',
+ });
+
+ expect(dataCollector).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/schema-context.test.ts b/src/client/src/__tests__/lib/platform/chat/schema-context.test.ts
new file mode 100644
index 00000000..668d5d9e
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/schema-context.test.ts
@@ -0,0 +1,83 @@
+import { getChatSystemPrompt } from '@/lib/platform/chat/schema-context';
+
+describe('getChatSystemPrompt', () => {
+ const prompt = getChatSystemPrompt();
+
+ it('returns a non-empty string', () => {
+ expect(typeof prompt).toBe('string');
+ expect(prompt.length).toBeGreaterThan(100);
+ });
+
+ it('includes the role description', () => {
+ expect(prompt).toContain('AI assistant for OpenLIT');
+ });
+
+ it('includes otel_traces table schema', () => {
+ expect(prompt).toContain('otel_traces');
+ expect(prompt).toContain('Timestamp');
+ expect(prompt).toContain('SpanAttributes');
+ expect(prompt).toContain('ResourceAttributes');
+ });
+
+ it('includes metrics table schemas', () => {
+ expect(prompt).toContain('otel_metrics_gauge');
+ expect(prompt).toContain('otel_metrics_sum');
+ expect(prompt).toContain('otel_metrics_histogram');
+ });
+
+ it('includes common SpanAttributes keys', () => {
+ expect(prompt).toContain('gen_ai.system');
+ expect(prompt).toContain('gen_ai.request.model');
+ expect(prompt).toContain('gen_ai.usage.cost');
+ expect(prompt).toContain('gen_ai.usage.input_tokens');
+ });
+
+ it('includes Mustache template pattern requirement', () => {
+ expect(prompt).toContain('{{filter.timeLimit.start}}');
+ expect(prompt).toContain('{{filter.timeLimit.end}}');
+ });
+
+ it('includes tool descriptions', () => {
+ expect(prompt).toContain('create_rule');
+ expect(prompt).toContain('create_context');
+ expect(prompt).toContain('create_prompt');
+ expect(prompt).toContain('create_vault_secret');
+ expect(prompt).toContain('create_custom_model');
+ });
+
+ it('includes entity links section', () => {
+ expect(prompt).toContain('Entity Links');
+ expect(prompt).toContain('/rule-engine/{id}');
+ expect(prompt).toContain('/context/{id}');
+ expect(prompt).toContain('/prompt-hub/{id}');
+ });
+
+ it('includes dashboard generation section', () => {
+ expect(prompt).toContain('Dashboard Generation');
+ expect(prompt).toContain('```dashboard');
+ expect(prompt).toContain('STAT_CARD');
+ expect(prompt).toContain('BAR_CHART');
+ expect(prompt).toContain('LINE_CHART');
+ expect(prompt).toContain('PIE_CHART');
+ expect(prompt).toContain('AREA_CHART');
+ });
+
+ it('includes widget property documentation', () => {
+ expect(prompt).toContain('properties connect query columns');
+ expect(prompt).toContain('"value": "0.');
+ expect(prompt).toContain('"xAxis"');
+ expect(prompt).toContain('"labelPath"');
+ expect(prompt).toContain('"yAxes"');
+ });
+
+ it('includes example queries with CTE pattern', () => {
+ expect(prompt).toContain('parseDateTimeBestEffort');
+ expect(prompt).toContain('start_time');
+ expect(prompt).toContain('end_time');
+ });
+
+ it('includes instant creation guideline', () => {
+ expect(prompt).toContain('IMMEDIATELY');
+ expect(prompt).toContain('Do NOT ask for confirmation');
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/sql-validator.test.ts b/src/client/src/__tests__/lib/platform/chat/sql-validator.test.ts
new file mode 100644
index 00000000..f79739eb
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/sql-validator.test.ts
@@ -0,0 +1,125 @@
+import { validateSQL, extractSQLFromResponse } from '@/lib/platform/chat/sql-validator';
+
+describe('validateSQL', () => {
+ it('rejects empty query', () => {
+ const result = validateSQL('');
+ expect(result.valid).toBe(false);
+ expect(result.error).toBe('Empty query');
+ });
+
+ it('rejects null/undefined', () => {
+ expect(validateSQL(null as any).valid).toBe(false);
+ expect(validateSQL(undefined as any).valid).toBe(false);
+ });
+
+ it('accepts valid SELECT query', () => {
+ const result = validateSQL('SELECT COUNT(*) FROM otel_traces LIMIT 10');
+ expect(result.valid).toBe(true);
+ expect(result.query).toContain('SELECT COUNT(*)');
+ });
+
+ it('accepts WITH...SELECT (CTE)', () => {
+ const result = validateSQL("WITH parseDateTimeBestEffort('2025-01-01') AS start_time SELECT COUNT(*) FROM otel_traces LIMIT 10");
+ expect(result.valid).toBe(true);
+ });
+
+ it('rejects INSERT', () => {
+ const result = validateSQL("INSERT INTO otel_traces VALUES ('a')");
+ expect(result.valid).toBe(false);
+ expect(result.error).toContain('forbidden operation');
+ });
+
+ it('rejects DROP', () => {
+ const result = validateSQL('DROP TABLE otel_traces');
+ expect(result.valid).toBe(false);
+ expect(result.error).toContain('forbidden operation');
+ });
+
+ it('rejects DELETE', () => {
+ const result = validateSQL('DELETE FROM otel_traces WHERE 1=1');
+ expect(result.valid).toBe(false);
+ expect(result.error).toContain('forbidden operation');
+ });
+
+ it('rejects ALTER', () => {
+ const result = validateSQL('ALTER TABLE otel_traces ADD COLUMN x String');
+ expect(result.valid).toBe(false);
+ });
+
+ it('rejects CREATE', () => {
+ const result = validateSQL('CREATE TABLE bad_table (id Int)');
+ expect(result.valid).toBe(false);
+ });
+
+ it('rejects non-allowed tables', () => {
+ const result = validateSQL('SELECT * FROM openlit_vault LIMIT 10');
+ expect(result.valid).toBe(false);
+ expect(result.error).toContain('not allowed');
+ });
+
+ it('allows otel_traces', () => {
+ expect(validateSQL('SELECT * FROM otel_traces LIMIT 10').valid).toBe(true);
+ });
+
+ it('allows otel_metrics_gauge', () => {
+ expect(validateSQL('SELECT * FROM otel_metrics_gauge LIMIT 10').valid).toBe(true);
+ });
+
+ it('allows otel_metrics_sum', () => {
+ expect(validateSQL('SELECT * FROM otel_metrics_sum LIMIT 10').valid).toBe(true);
+ });
+
+ it('allows otel_metrics_histogram', () => {
+ expect(validateSQL('SELECT * FROM otel_metrics_histogram LIMIT 10').valid).toBe(true);
+ });
+
+ it('appends LIMIT 1000 if missing', () => {
+ const result = validateSQL('SELECT * FROM otel_traces');
+ expect(result.valid).toBe(true);
+ expect(result.query).toContain('LIMIT 1000');
+ });
+
+ it('preserves existing LIMIT', () => {
+ const result = validateSQL('SELECT * FROM otel_traces LIMIT 50');
+ expect(result.valid).toBe(true);
+ expect(result.query).toContain('LIMIT 50');
+ });
+
+ it('caps LIMIT above 10000 to 1000', () => {
+ const result = validateSQL('SELECT * FROM otel_traces LIMIT 99999');
+ expect(result.valid).toBe(true);
+ expect(result.query).toContain('LIMIT 1000');
+ });
+
+ it('strips markdown code block markers', () => {
+ const result = validateSQL('```sql\nSELECT 1 FROM otel_traces LIMIT 1\n```');
+ expect(result.valid).toBe(true);
+ expect(result.query).not.toContain('```');
+ });
+
+ it('strips trailing semicolons', () => {
+ const result = validateSQL('SELECT 1 FROM otel_traces LIMIT 1;');
+ expect(result.valid).toBe(true);
+ expect(result.query).not.toContain(';');
+ });
+});
+
+describe('extractSQLFromResponse', () => {
+ it('extracts SQL from markdown response', () => {
+ const response = 'Here is the query:\n```sql\nSELECT COUNT(*) FROM otel_traces\n```\nThis counts traces.';
+ const blocks = extractSQLFromResponse(response);
+ expect(blocks).toHaveLength(1);
+ expect(blocks[0]).toContain('SELECT COUNT(*)');
+ });
+
+ it('extracts multiple SQL blocks', () => {
+ const response = '```sql\nSELECT 1\n```\nAnd also:\n```sql\nSELECT 2\n```';
+ const blocks = extractSQLFromResponse(response);
+ expect(blocks).toHaveLength(2);
+ });
+
+ it('returns empty array when no SQL blocks', () => {
+ const blocks = extractSQLFromResponse('No SQL here');
+ expect(blocks).toHaveLength(0);
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/stream.test.ts b/src/client/src/__tests__/lib/platform/chat/stream.test.ts
new file mode 100644
index 00000000..79e9405b
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/stream.test.ts
@@ -0,0 +1,166 @@
+// Mock ai package before anything else
+jest.mock('ai', () => ({
+ streamText: jest.fn(),
+ generateText: jest.fn(),
+ stepCountIs: jest.fn(() => () => false),
+ tool: jest.fn((t: any) => t),
+ jsonSchema: jest.fn((s: any) => s),
+}));
+
+import { getModelInstance, formatStreamError, buildConversationMessages } from '@/lib/platform/chat/stream';
+
+// Mock external dependencies
+jest.mock('@ai-sdk/openai', () => ({
+ createOpenAI: jest.fn(() => (model: string) => ({ model, provider: 'openai' })),
+}));
+jest.mock('@ai-sdk/anthropic', () => ({
+ createAnthropic: jest.fn(() => (model: string) => ({ model, provider: 'anthropic' })),
+}));
+jest.mock('@ai-sdk/google', () => ({
+ google: (model: string) => ({ model, provider: 'google' }),
+}));
+jest.mock('@ai-sdk/mistral', () => ({
+ createMistral: jest.fn(() => (model: string) => ({ model, provider: 'mistral' })),
+}));
+jest.mock('@ai-sdk/cohere', () => ({
+ createCohere: jest.fn(() => (model: string) => ({ model, provider: 'cohere' })),
+}));
+jest.mock('@/lib/platform/chat/conversation', () => ({
+ addMessage: jest.fn(),
+ getConversationMessages: jest.fn(),
+ updateConversation: jest.fn(),
+}));
+jest.mock('@/lib/platform/chat/schema-context', () => ({
+ getChatSystemPrompt: jest.fn(() => 'system prompt'),
+}));
+jest.mock('@/lib/platform/chat/sql-validator', () => ({
+ validateSQL: jest.fn(),
+ extractSQLFromResponse: jest.fn(() => []),
+}));
+jest.mock('@/lib/platform/common', () => ({
+ dataCollector: jest.fn(),
+}));
+jest.mock('@/lib/platform/chat/tools', () => ({
+ getChatTools: jest.fn(() => ({})),
+}));
+
+import { getConversationMessages } from '@/lib/platform/chat/conversation';
+
+describe('getModelInstance', () => {
+ it('creates OpenAI model instance', () => {
+ const instance = getModelInstance('openai', 'sk-test', 'gpt-4o');
+ expect(instance).toEqual({ model: 'gpt-4o', provider: 'openai' });
+ });
+
+ it('creates Anthropic model instance', () => {
+ const instance = getModelInstance('anthropic', 'sk-test', 'claude-3');
+ expect(instance).toEqual({ model: 'claude-3', provider: 'anthropic' });
+ });
+
+ it('creates Google model instance', () => {
+ const instance = getModelInstance('google', 'key', 'gemini-pro');
+ expect(instance).toEqual({ model: 'gemini-pro', provider: 'google' });
+ });
+
+ it('throws for unsupported provider', () => {
+ expect(() => getModelInstance('nonexistent', 'key', 'model')).toThrow('not supported');
+ });
+
+ it('supports groq via OpenAI-compatible endpoint', () => {
+ const instance = getModelInstance('groq', 'key', 'llama-3');
+ expect(instance).toBeDefined();
+ });
+
+ it('supports all 14 providers', () => {
+ const providers = [
+ 'openai', 'anthropic', 'google', 'mistral', 'cohere',
+ 'groq', 'perplexity', 'azure', 'together', 'fireworks',
+ 'deepseek', 'xai', 'huggingface', 'replicate',
+ ];
+ for (const p of providers) {
+ expect(() => getModelInstance(p, 'key', 'model')).not.toThrow();
+ }
+ });
+});
+
+describe('formatStreamError', () => {
+ it('returns specific message for 401', () => {
+ const msg = formatStreamError({ statusCode: 401 });
+ expect(msg).toContain('Invalid API key');
+ });
+
+ it('returns specific message for invalid_api_key code', () => {
+ const msg = formatStreamError({ data: { error: { code: 'invalid_api_key' } } });
+ expect(msg).toContain('Invalid API key');
+ });
+
+ it('returns specific message for 429', () => {
+ expect(formatStreamError({ statusCode: 429 })).toContain('Rate limit');
+ });
+
+ it('returns specific message for 403', () => {
+ expect(formatStreamError({ statusCode: 403 })).toContain('Access denied');
+ });
+
+ it('returns specific message for 404', () => {
+ expect(formatStreamError({ statusCode: 404 })).toContain('Model not found');
+ });
+
+ it('returns specific message for 500+', () => {
+ expect(formatStreamError({ statusCode: 500 })).toContain('provider is experiencing issues');
+ expect(formatStreamError({ statusCode: 503 })).toContain('provider is experiencing issues');
+ });
+
+ it('strips long tokens from error messages', () => {
+ const msg = formatStreamError({ message: 'Key: sk-abcdefghijklmnopqrstuvwxyz1234567890 is invalid' });
+ expect(msg).toContain('***');
+ expect(msg).not.toContain('sk-abcdefghijklmnopqrstuvwxyz1234567890');
+ });
+
+ it('returns generic message for unknown errors', () => {
+ expect(formatStreamError({})).toContain('An error occurred');
+ });
+});
+
+describe('buildConversationMessages', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it('returns current message when no history', async () => {
+ (getConversationMessages as jest.Mock).mockResolvedValue({ data: [] });
+ const messages = await buildConversationMessages('c1', 'Hello');
+ expect(messages).toEqual([{ role: 'user', content: 'Hello' }]);
+ });
+
+ it('includes history messages', async () => {
+ (getConversationMessages as jest.Mock).mockResolvedValue({
+ data: [
+ { role: 'user', content: 'Hi' },
+ { role: 'assistant', content: 'Hello!' },
+ { role: 'user', content: 'New message' },
+ ],
+ });
+ const messages = await buildConversationMessages('c1', 'New message');
+ expect(messages).toHaveLength(3);
+ expect(messages[0]).toEqual({ role: 'user', content: 'Hi' });
+ expect(messages[2]).toEqual({ role: 'user', content: 'New message' });
+ });
+
+ it('appends current message if not in history', async () => {
+ (getConversationMessages as jest.Mock).mockResolvedValue({
+ data: [{ role: 'user', content: 'Old message' }],
+ });
+ const messages = await buildConversationMessages('c1', 'Brand new');
+ expect(messages).toHaveLength(2);
+ expect(messages[1].content).toBe('Brand new');
+ });
+
+ it('does not duplicate current message if already in history', async () => {
+ (getConversationMessages as jest.Mock).mockResolvedValue({
+ data: [{ role: 'user', content: 'Same message' }],
+ });
+ const messages = await buildConversationMessages('c1', 'Same message');
+ expect(messages).toHaveLength(1);
+ });
+});
diff --git a/src/client/src/app/(playground)/chat/page.tsx b/src/client/src/app/(playground)/chat/page.tsx
new file mode 100644
index 00000000..4965997e
--- /dev/null
+++ b/src/client/src/app/(playground)/chat/page.tsx
@@ -0,0 +1,15 @@
+"use client";
+
+import { useSearchParams } from "next/navigation";
+import ChatLayout from "@/components/(playground)/chat/chat-layout";
+
+export default function ChatPage() {
+ const searchParams = useSearchParams();
+ const conversationId = searchParams.get("id");
+
+ return (
+ <div className="flex flex-col w-full h-full overflow-hidden">
+ <ChatLayout initialConversationId={conversationId} />
+ </div>
+ );
+}
diff --git a/src/client/src/app/(playground)/chat/settings/page.tsx b/src/client/src/app/(playground)/chat/settings/page.tsx
new file mode 100644
index 00000000..bc0bde4a
--- /dev/null
+++ b/src/client/src/app/(playground)/chat/settings/page.tsx
@@ -0,0 +1,11 @@
+"use client";
+
+import ChatSettingsForm from "@/components/(playground)/chat/chat-settings-form";
+
+export default function ChatSettingsPage() {
+ return (
+ <div className="flex flex-col w-full h-full overflow-y-auto p-6">
+ <ChatSettingsForm />
+ </div>
+ );
+}
diff --git a/src/client/src/app/(playground)/layout.tsx b/src/client/src/app/(playground)/layout.tsx
index e62e0a55..26e13cd0 100644
--- a/src/client/src/app/(playground)/layout.tsx
+++ b/src/client/src/app/(playground)/layout.tsx
@@ -7,6 +7,7 @@ import CustomPostHogProvider from "@/components/(playground)/posthog";
import NavigationEvents from "@/components/common/navigation-events";
import AppInit from "@/components/common/app-init";
import { PortalProvider } from "@/components/(playground)/header-portal";
+import ChatFloatingButton from "@/components/(playground)/chat/chat-floating-button";
export default async function PlaygroundLayout({
children,
@@ -30,6 +31,7 @@ export default async function PlaygroundLayout({
</main>
</div>
</div>
+ <ChatFloatingButton />
</PortalProvider>
</TooltipProvider>
<Suspense fallback={null}>
diff --git a/src/client/src/app/(playground)/settings/manage-models/page.tsx b/src/client/src/app/(playground)/settings/manage-models/page.tsx
index 98daacd3..af7d32d4 100644
--- a/src/client/src/app/(playground)/settings/manage-models/page.tsx
+++ b/src/client/src/app/(playground)/settings/manage-models/page.tsx
@@ -5,7 +5,7 @@ import { usePostHog } from "posthog-js/react";
import { CLIENT_EVENTS } from "@/constants/events";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
-import { ArrowLeftIcon, SettingsIcon } from "lucide-react";
+import { ArrowLeftIcon, SettingsIcon, Download } from "lucide-react";
import { useRouter } from "next/navigation";
import getMessage from "@/constants/messages";
import ModelListSidebar from "@/components/(playground)/openground/model-list-sidebar";
@@ -15,8 +15,10 @@ import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
interface CustomModel extends ModelMetadata {
id: string;
+ customId?: string;
model_id: string;
provider?: string;
+ modelType?: string;
}
export default function SettingsManageModelsPage() {
@@ -93,6 +95,17 @@ export default function SettingsManageModelsPage() {
{getMessage().OPENGROUND_MANAGE_MODELS_DESCRIPTION}
</p>
</div>
+ <Button
+ variant="outline"
+ size="sm"
+ className="gap-2"
+ onClick={() => {
+ window.open("/api/openground/models/export", "_blank");
+ }}
+ >
+ <Download className="h-4 w-4" />
+ Export Pricing
+ </Button>
</div>
{/* Main Content - Split Layout */}
diff --git a/src/client/src/app/api/chat/config/route.ts b/src/client/src/app/api/chat/config/route.ts
new file mode 100644
index 00000000..ed8ea4d6
--- /dev/null
+++ b/src/client/src/app/api/chat/config/route.ts
@@ -0,0 +1,49 @@
+import { getChatConfig, upsertChatConfig } from "@/lib/platform/chat/config";
+import { getCurrentUser } from "@/lib/session";
+import { NextRequest } from "next/server";
+import asaw from "@/utils/asaw";
+
+export async function GET() {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const { data, err } = await getChatConfig();
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json({ data });
+}
+
+export async function POST(request: NextRequest) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const formData = await request.json();
+
+ if (!formData.provider || !formData.model || !formData.vaultId) {
+ return Response.json("Missing required fields: provider, model, vaultId", {
+ status: 400,
+ });
+ }
+
+ const [err, data] = await asaw(
+ upsertChatConfig({
+ provider: formData.provider,
+ model: formData.model,
+ vaultId: formData.vaultId,
+ meta: formData.meta || "{}",
+ })
+ );
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json(data);
+}
diff --git a/src/client/src/app/api/chat/conversation/[id]/route.ts b/src/client/src/app/api/chat/conversation/[id]/route.ts
new file mode 100644
index 00000000..1ddb193d
--- /dev/null
+++ b/src/client/src/app/api/chat/conversation/[id]/route.ts
@@ -0,0 +1,44 @@
+import {
+ getConversationWithMessages,
+ deleteConversation,
+} from "@/lib/platform/chat/conversation";
+import { getCurrentUser } from "@/lib/session";
+import { NextRequest } from "next/server";
+
+export async function GET(
+ _: NextRequest,
+ { params }: { params: Promise<{ id: string }> }
+) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const { id } = await params;
+ const { data, err } = await getConversationWithMessages(id);
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json({ data });
+}
+
+export async function DELETE(
+ _: NextRequest,
+ { params }: { params: Promise<{ id: string }> }
+) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const { id } = await params;
+ const { err } = await deleteConversation(id);
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json({ data: "Conversation deleted" });
+}
diff --git a/src/client/src/app/api/chat/conversation/route.ts b/src/client/src/app/api/chat/conversation/route.ts
new file mode 100644
index 00000000..a64cf672
--- /dev/null
+++ b/src/client/src/app/api/chat/conversation/route.ts
@@ -0,0 +1,42 @@
+import {
+ getConversations,
+ createConversation,
+} from "@/lib/platform/chat/conversation";
+import { getCurrentUser } from "@/lib/session";
+import { NextRequest } from "next/server";
+
+export async function GET() {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const { data, err } = await getConversations();
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json({ data });
+}
+
+export async function POST(request: NextRequest) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const body = await request.json();
+
+ const { data, err } = await createConversation(
+ body.title || "",
+ body.provider || "",
+ body.model || ""
+ );
+
+ if (err) {
+ return Response.json(err, { status: 400 });
+ }
+
+ return Response.json({ data });
+}
diff --git a/src/client/src/app/api/chat/message/execute/route.ts b/src/client/src/app/api/chat/message/execute/route.ts
new file mode 100644
index 00000000..8c727057
--- /dev/null
+++ b/src/client/src/app/api/chat/message/execute/route.ts
@@ -0,0 +1,61 @@
+import { getCurrentUser } from "@/lib/session";
+import { dataCollector } from "@/lib/platform/common";
+import { validateSQL } from "@/lib/platform/chat/sql-validator";
+import { updateMessage } from "@/lib/platform/chat/conversation";
+import { NextRequest } from "next/server";
+
+export async function POST(request: NextRequest) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const body = await request.json();
+ const { messageId, query } = body;
+
+ if (!query) {
+ return Response.json("Missing query", { status: 400 });
+ }
+
+ // Validate SQL
+ const validation = validateSQL(query);
+ if (!validation.valid) {
+ return Response.json(
+ { err: validation.error },
+ { status: 400 }
+ );
+ }
+
+ const startTime = Date.now();
+
+ // Execute with readonly mode
+ const { data, err } = await dataCollector({
+ query: validation.query!,
+ enable_readonly: true,
+ });
+
+ const executionTimeMs = Date.now() - startTime;
+
+ if (err) {
+ return Response.json({ err }, { status: 400 });
+ }
+
+ const results = data as any[];
+ const stats = {
+ rowsRead: results?.length || 0,
+ executionTimeMs,
+ bytesRead: 0,
+ };
+
+ // Update message with query result and stats if messageId provided
+ if (messageId) {
+ await updateMessage(messageId, {
+ queryResult: JSON.stringify(results || []),
+ queryRowsRead: stats.rowsRead,
+ queryExecutionTimeMs: stats.executionTimeMs,
+ queryBytesRead: stats.bytesRead,
+ });
+ }
+
+ return Response.json({ data: results, stats });
+}
diff --git a/src/client/src/app/api/chat/message/route.ts b/src/client/src/app/api/chat/message/route.ts
new file mode 100644
index 00000000..22dcea7e
--- /dev/null
+++ b/src/client/src/app/api/chat/message/route.ts
@@ -0,0 +1,53 @@
+import { getCurrentUser } from "@/lib/session";
+import { getDBConfigByUser } from "@/lib/db-config";
+import { getChatConfigWithApiKey } from "@/lib/platform/chat/config";
+import { streamChatMessage, formatStreamError } from "@/lib/platform/chat/stream";
+import { NextRequest } from "next/server";
+import asaw from "@/utils/asaw";
+
+export async function POST(request: NextRequest) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const body = await request.json();
+ const { conversationId, content } = body;
+
+ if (!conversationId || !content) {
+ return Response.json("Missing conversationId or content", { status: 400 });
+ }
+
+ const { data: config, err: configErr } = await getChatConfigWithApiKey();
+ if (configErr || !config) {
+ return Response.json(
+ configErr || "Chat not configured. Please set up your AI provider.",
+ { status: 400 }
+ );
+ }
+
+ const [, dbConfig] = await asaw(getDBConfigByUser(true));
+ const dbConfigId = (dbConfig as any)?.id || "";
+
+ try {
+ const { responseText } = await streamChatMessage({
+ conversationId,
+ content,
+ provider: config.provider,
+ apiKey: config.apiKey,
+ model: config.model,
+ userId: user.id,
+ dbConfigId,
+ });
+
+ return new Response(
+ responseText || "**Error:** No response received. Please check your Chat Settings.",
+ { headers: { "Content-Type": "text/plain; charset=utf-8" } }
+ );
+ } catch (e: any) {
+ const errorMsg = formatStreamError(e);
+ return new Response(`**Error:** ${errorMsg}`, {
+ headers: { "Content-Type": "text/plain; charset=utf-8" },
+ });
+ }
+}
diff --git a/src/client/src/app/api/chat/message/save-widget/route.ts b/src/client/src/app/api/chat/message/save-widget/route.ts
new file mode 100644
index 00000000..6a816aa5
--- /dev/null
+++ b/src/client/src/app/api/chat/message/save-widget/route.ts
@@ -0,0 +1,34 @@
+import { getCurrentUser } from "@/lib/session";
+import { saveQueryAsWidget } from "@/lib/platform/chat/save-widget";
+import { NextRequest } from "next/server";
+
+export async function POST(request: NextRequest) {
+ const user = await getCurrentUser();
+ if (!user) {
+ return Response.json("Unauthorized", { status: 401 });
+ }
+
+ const body = await request.json();
+ const { title, description, type, query, properties, boardId } = body;
+
+ if (!title || !type || !query) {
+ return Response.json("Missing required fields: title, type, query", {
+ status: 400,
+ });
+ }
+
+ const result = await saveQueryAsWidget({
+ title,
+ description,
+ type,
+ query,
+ properties,
+ boardId,
+ });
+
+ if (result.err) {
+ return Response.json({ err: result.err }, { status: 400 });
+ }
+
+ return Response.json(result);
+}
diff --git a/src/client/src/app/api/openground/models/export/route.ts b/src/client/src/app/api/openground/models/export/route.ts
new file mode 100644
index 00000000..0553a88f
--- /dev/null
+++ b/src/client/src/app/api/openground/models/export/route.ts
@@ -0,0 +1,95 @@
+import { NextRequest, NextResponse } from "next/server";
+import { getServerSession } from "next-auth";
+import { authOptions } from "@/app/auth";
+import { dataCollector } from "@/lib/platform/common";
+import { getDBConfigByUser } from "@/lib/db-config";
+import getMessage from "@/constants/messages";
+import Sanitizer from "@/utils/sanitizer";
+import { OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME } from "@/lib/platform/openground/table-details";
+import asaw from "@/utils/asaw";
+
+interface SessionWithId {
+ user?: { id?: string };
+}
+
+/**
+ * GET /api/openground/models/export
+ * Export custom models in pricing.json format compatible with OpenLIT SDK pricing
+ */
+export async function GET(request: NextRequest) {
+ const session = (await getServerSession(authOptions)) as SessionWithId;
+
+ if (!session?.user?.id) {
+ return NextResponse.json(
+ { error: getMessage().UNAUTHORIZED_USER },
+ { status: 401 }
+ );
+ }
+
+ const [, dbConfig] = await asaw(getDBConfigByUser(true));
+ if (!dbConfig?.id) {
+ return NextResponse.json(
+ { error: getMessage().DATABASE_CONFIG_NOT_FOUND },
+ { status: 500 }
+ );
+ }
+
+ const query = `
+ SELECT
+ model_id,
+ model_type,
+ input_price_per_m_token as inputPrice,
+ output_price_per_m_token as outputPrice
+ FROM ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}
+ WHERE created_by_user_id = '${Sanitizer.sanitizeValue(session.user.id)}'
+ AND database_config_id = '${Sanitizer.sanitizeValue(dbConfig.id)}'
+ ORDER BY model_type, model_id
+ `;
+
+ const { data, err } = await dataCollector(
+ { query },
+ "query",
+ dbConfig.id
+ );
+
+ if (err) {
+ return NextResponse.json({ error: getMessage().OPERATION_FAILED }, { status: 500 });
+ }
+
+ const models = (data as any[]) || [];
+
+ // Build pricing.json format grouped by model_type
+ const pricing: Record<string, any> = {};
+
+ for (const model of models) {
+ const type = model.model_type || "chat";
+ if (!pricing[type]) {
+ pricing[type] = {};
+ }
+
+ if (type === "chat") {
+ pricing[type][model.model_id] = {
+ promptPrice: model.inputPrice / 1000, // per-M to per-K
+ completionPrice: model.outputPrice / 1000,
+ };
+ } else if (type === "embeddings") {
+ pricing[type][model.model_id] = model.inputPrice / 1000;
+ } else if (type === "audio") {
+ pricing[type][model.model_id] = model.inputPrice / 1000;
+ } else if (type === "images") {
+ pricing[type][model.model_id] = {
+ standard: {
+ "1024x1024": model.inputPrice / 1000,
+ },
+ };
+ }
+ }
+
+ // Return as downloadable JSON
+ return new NextResponse(JSON.stringify(pricing, null, 4), {
+ headers: {
+ "Content-Type": "application/json",
+ "Content-Disposition": "attachment; filename=custom-pricing.json",
+ },
+ });
+}
diff --git a/src/client/src/app/api/openground/models/route.ts b/src/client/src/app/api/openground/models/route.ts
index c30a59b9..e1705e43 100644
--- a/src/client/src/app/api/openground/models/route.ts
+++ b/src/client/src/app/api/openground/models/route.ts
@@ -48,9 +48,11 @@ export async function GET(request: NextRequest) {
const query = `
SELECT
toString(id) as customId,
+ model_id,
model_id as id,
provider,
display_name as displayName,
+ model_type as modelType,
context_window as contextWindow,
input_price_per_m_token as inputPricePerMToken,
output_price_per_m_token as outputPricePerMToken,
@@ -78,33 +80,16 @@ export async function GET(request: NextRequest) {
);
}
- // Filter out invalid UUIDs and group by provider
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
+ // Group models by provider
const grouped: Record<string, any[]> = {};
- const invalidIds: string[] = [];
(data as any[] || []).forEach((model: any) => {
- // Check if UUID is valid
- if (!model.customId || !uuidRegex.test(model.customId)) {
- invalidIds.push(model.customId || 'null');
- return; // Skip invalid models
- }
-
if (!grouped[model.provider]) {
grouped[model.provider] = [];
}
grouped[model.provider].push(model);
});
- // Log invalid IDs for cleanup
- if (invalidIds.length > 0) {
- // Use separate parameters to prevent log injection
- console.warn('Found', invalidIds.length, 'models with invalid UUIDs:', invalidIds);
- console.warn('These models will not be shown in the UI. Clean them up by calling: DELETE FROM', OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME, 'WHERE id NOT IN (SELECT id FROM', OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME, 'WHERE match(toString(id), \'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$\'))');
- }
-
- console.log("GET /api/openground/models - Returning grouped models:", JSON.stringify(grouped, null, 2));
-
PostHogServer.fireEvent({
event: SERVER_EVENTS.OPENGROUND_MODELS_LIST_SUCCESS,
startTimestamp,
@@ -147,21 +132,11 @@ export async function GET(request: NextRequest) {
);
}
- // Filter out invalid UUIDs
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
- const validModels = (data as any[] || []).filter((model: any) => {
- const isValid = model.customId && uuidRegex.test(model.customId);
- if (!isValid) {
- console.warn(`Filtering out model with invalid UUID:`, model.customId);
- }
- return isValid;
- });
-
PostHogServer.fireEvent({
event: SERVER_EVENTS.OPENGROUND_MODELS_LIST_SUCCESS,
startTimestamp,
});
- return NextResponse.json(validModels);
+ return NextResponse.json(data || []);
}
// POST: Create or update a custom model
@@ -187,11 +162,9 @@ export async function POST(request: NextRequest) {
const body = await request.json();
const { provider, model, customId } = body;
- console.log("POST /api/openground/models - Body:", JSON.stringify(body, null, 2));
- console.log("CustomId type:", typeof customId, "value:", customId);
-
- if (!provider || !model?.id || !model?.displayName) {
- console.error("Validation failed - provider:", provider, "model.id:", model?.id, "model.displayName:", model?.displayName);
+ // Support both model.id and model.model_id for compatibility
+ const modelId = model?.id || model?.model_id;
+ if (!provider || !modelId || !model?.displayName) {
return NextResponse.json(
{ error: "Provider, model ID, and display name are required" },
{ status: 400 }
@@ -200,18 +173,6 @@ export async function POST(request: NextRequest) {
// Check if updating existing model
if (customId) {
- console.log("Updating custom model with ID:", customId);
-
- // Validate UUID format
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
- if (!uuidRegex.test(customId)) {
- console.error("Invalid UUID format:", customId);
- return NextResponse.json(
- { error: `Invalid UUID format: ${customId}` },
- { status: 400 }
- );
- }
-
// Use ALTER TABLE UPDATE for ClickHouse
const capabilitiesArray = (model.capabilities || [])
.map((c: string) => `'${Sanitizer.sanitizeValue(c)}'`)
@@ -221,16 +182,18 @@ export async function POST(request: NextRequest) {
ALTER TABLE ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}
UPDATE
display_name = '${Sanitizer.sanitizeValue(model.displayName)}',
+ model_type = '${Sanitizer.sanitizeValue(model.modelType || "chat")}',
context_window = ${model.contextWindow || 4096},
input_price_per_m_token = ${model.inputPricePerMToken || 0},
output_price_per_m_token = ${model.outputPricePerMToken || 0},
capabilities = [${capabilitiesArray}],
updated_at = now()
- WHERE id = toUUID('${Sanitizer.sanitizeValue(customId)}')
+ WHERE model_id = '${Sanitizer.sanitizeValue(modelId)}'
+ AND provider = '${Sanitizer.sanitizeValue(provider)}'
+ AND created_by_user_id = '${Sanitizer.sanitizeValue(session.user.id)}'
+ AND database_config_id = '${Sanitizer.sanitizeValue(dbConfig.id)}'
`;
- console.log("Update query:", updateQuery);
-
const { err } = await dataCollector(
{ query: updateQuery },
"exec",
@@ -242,7 +205,6 @@ export async function POST(request: NextRequest) {
event: SERVER_EVENTS.OPENGROUND_MODELS_CREATE_FAILURE,
startTimestamp,
});
- console.error("Error updating model:", err);
return NextResponse.json(
{ error: err || getMessage().OPERATION_FAILED },
{ status: 500 }
@@ -263,8 +225,9 @@ export async function POST(request: NextRequest) {
values: [
{
provider: provider,
- model_id: model.id,
+ model_id: modelId,
display_name: model.displayName,
+ model_type: model.modelType || "chat",
context_window: model.contextWindow || 4096,
input_price_per_m_token: model.inputPricePerMToken || 0,
output_price_per_m_token: model.outputPricePerMToken || 0,
@@ -283,7 +246,6 @@ export async function POST(request: NextRequest) {
event: SERVER_EVENTS.OPENGROUND_MODELS_CREATE_FAILURE,
startTimestamp,
});
- console.error("Error inserting custom model:", err);
return NextResponse.json(
{ error: err || getMessage().OPERATION_FAILED },
{ status: 500 }
@@ -305,7 +267,7 @@ export async function POST(request: NextRequest) {
WHERE created_by_user_id = '${Sanitizer.sanitizeValue(session.user.id)}'
AND database_config_id = '${Sanitizer.sanitizeValue(dbConfig.id)}'
AND provider = '${Sanitizer.sanitizeValue(provider)}'
- AND model_id = '${Sanitizer.sanitizeValue(model.id)}'
+ AND model_id = '${Sanitizer.sanitizeValue(modelId)}'
ORDER BY created_at DESC
LIMIT 1
`;
@@ -316,8 +278,6 @@ export async function POST(request: NextRequest) {
dbConfig.id
);
- console.log("Newly created model:", JSON.stringify(newModel, null, 2));
-
PostHogServer.fireEvent({
event: SERVER_EVENTS.OPENGROUND_MODELS_CREATE_SUCCESS,
startTimestamp,
@@ -351,35 +311,34 @@ export async function DELETE(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const id = searchParams.get("id");
+ const modelId = searchParams.get("model_id");
+ const provider = searchParams.get("provider");
- console.log("DELETE - id parameter:", id, "type:", typeof id);
-
- if (!id) {
+ if (!id && !modelId) {
return NextResponse.json(
- { error: "Model ID parameter is required" },
+ { error: "Model ID or model_id parameter is required" },
{ status: 400 }
);
}
- // Validate UUID format
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
- if (!uuidRegex.test(id)) {
- console.error("Invalid UUID format:", id);
- return NextResponse.json(
- { error: `Invalid UUID format: ${id}` },
- { status: 400 }
- );
+ // Build WHERE clause — prefer model_id+provider match, fall back to UUID id
+ const whereConditions = [
+ `created_by_user_id = '${Sanitizer.sanitizeValue(session.user.id)}'`,
+ `database_config_id = '${Sanitizer.sanitizeValue(dbConfig.id)}'`,
+ ];
+
+ if (modelId && provider) {
+ whereConditions.push(`model_id = '${Sanitizer.sanitizeValue(modelId)}'`);
+ whereConditions.push(`provider = '${Sanitizer.sanitizeValue(provider)}'`);
+ } else if (id) {
+ whereConditions.push(`(toString(id) = '${Sanitizer.sanitizeValue(id)}' OR model_id = '${Sanitizer.sanitizeValue(id)}')`);
}
const deleteQuery = `
DELETE FROM ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}
- WHERE id = toUUID('${Sanitizer.sanitizeValue(id)}')
- AND created_by_user_id = '${Sanitizer.sanitizeValue(session.user.id)}'
- AND database_config_id = '${Sanitizer.sanitizeValue(dbConfig.id)}'
+ WHERE ${whereConditions.join(" AND ")}
`;
- console.log("Delete query:", deleteQuery);
-
const { err } = await dataCollector(
{ query: deleteQuery },
"exec",
@@ -391,7 +350,6 @@ export async function DELETE(request: NextRequest) {
event: SERVER_EVENTS.OPENGROUND_MODELS_DELETE_FAILURE,
startTimestamp,
});
- console.error("Delete error:", err);
return NextResponse.json(
{ error: err || getMessage().OPERATION_FAILED },
{ status: 500 }
diff --git a/src/client/src/clickhouse/migrations/add-model-type-migration.ts b/src/client/src/clickhouse/migrations/add-model-type-migration.ts
new file mode 100644
index 00000000..aec4fe7d
--- /dev/null
+++ b/src/client/src/clickhouse/migrations/add-model-type-migration.ts
@@ -0,0 +1,20 @@
+import migrationHelper from "./migration-helper";
+import { OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME } from "@/lib/platform/openground/table-details";
+
+const MIGRATION_ID = "add-model-type-column";
+
+export default async function AddModelTypeMigration(
+ databaseConfigId?: string
+) {
+ const queries = [
+ `ALTER TABLE ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME} ADD COLUMN IF NOT EXISTS model_type String DEFAULT 'chat'`,
+ ];
+
+ const { migrationExist, queriesRun } = await migrationHelper({
+ clickhouseMigrationId: MIGRATION_ID,
+ databaseConfigId,
+ queries,
+ });
+
+ return { migrationExist, queriesRun };
+}
diff --git a/src/client/src/clickhouse/migrations/create-chat-migration.ts b/src/client/src/clickhouse/migrations/create-chat-migration.ts
new file mode 100644
index 00000000..77a2c2e0
--- /dev/null
+++ b/src/client/src/clickhouse/migrations/create-chat-migration.ts
@@ -0,0 +1,86 @@
+import migrationHelper from "./migration-helper";
+
+const MIGRATION_ID = "create-chat-tables-v2";
+
+const CHAT_CONFIG_TABLE = "openlit_chat_config";
+const CHAT_CONVERSATION_TABLE = "openlit_chat_conversation";
+const CHAT_MESSAGE_TABLE = "openlit_chat_message";
+
+export default async function CreateChatMigration(
+ databaseConfigId?: string
+) {
+ const queries = [
+ `
+ CREATE TABLE IF NOT EXISTS ${CHAT_CONFIG_TABLE} (
+ id UUID DEFAULT generateUUIDv4(),
+ provider String,
+ model String,
+ vault_id String,
+ meta String DEFAULT '{}',
+ created_at DateTime DEFAULT now(),
+ updated_at DateTime DEFAULT now(),
+
+ INDEX id_index (id) TYPE bloom_filter GRANULARITY 1,
+ INDEX created_at_index (created_at) TYPE minmax GRANULARITY 1,
+ INDEX updated_at_index (updated_at) TYPE minmax GRANULARITY 1,
+
+ PRIMARY KEY id
+ ) ENGINE = ReplacingMergeTree(updated_at)
+ ORDER BY (id)
+ `,
+ `
+ CREATE TABLE IF NOT EXISTS ${CHAT_CONVERSATION_TABLE} (
+ id UUID DEFAULT generateUUIDv4(),
+ title String DEFAULT '',
+ total_prompt_tokens UInt64 DEFAULT 0,
+ total_completion_tokens UInt64 DEFAULT 0,
+ total_cost Float64 DEFAULT 0,
+ total_messages UInt32 DEFAULT 0,
+ provider String DEFAULT '',
+ model String DEFAULT '',
+ created_at DateTime DEFAULT now(),
+ updated_at DateTime DEFAULT now(),
+
+ INDEX title_index (title) TYPE tokenbf_v1(32768, 3, 0) GRANULARITY 1,
+ INDEX created_at_index (created_at) TYPE minmax GRANULARITY 1,
+ INDEX updated_at_index (updated_at) TYPE minmax GRANULARITY 1,
+
+ PRIMARY KEY id
+ ) ENGINE = MergeTree()
+ ORDER BY (id, created_at)
+ `,
+ `
+ CREATE TABLE IF NOT EXISTS ${CHAT_MESSAGE_TABLE} (
+ id UUID DEFAULT generateUUIDv4(),
+ conversation_id UUID,
+ role String,
+ content String,
+ sql_query String DEFAULT '',
+ query_result String DEFAULT '',
+ widget_type String DEFAULT '',
+ prompt_tokens UInt64 DEFAULT 0,
+ completion_tokens UInt64 DEFAULT 0,
+ cost Float64 DEFAULT 0,
+ query_rows_read UInt64 DEFAULT 0,
+ query_execution_time_ms UInt64 DEFAULT 0,
+ query_bytes_read UInt64 DEFAULT 0,
+ created_at DateTime DEFAULT now(),
+
+ INDEX conversation_id_index (conversation_id) TYPE bloom_filter GRANULARITY 1,
+ INDEX role_index (role) TYPE bloom_filter GRANULARITY 1,
+ INDEX created_at_index (created_at) TYPE minmax GRANULARITY 1,
+
+ PRIMARY KEY id
+ ) ENGINE = MergeTree()
+ ORDER BY (id, conversation_id, created_at)
+ `,
+ ];
+
+ const { migrationExist, queriesRun } = await migrationHelper({
+ clickhouseMigrationId: MIGRATION_ID,
+ databaseConfigId,
+ queries,
+ });
+
+ return { migrationExist, queriesRun };
+}
diff --git a/src/client/src/clickhouse/migrations/index.ts b/src/client/src/clickhouse/migrations/index.ts
index 352d9a9c..517afe7b 100644
--- a/src/client/src/clickhouse/migrations/index.ts
+++ b/src/client/src/clickhouse/migrations/index.ts
@@ -7,6 +7,8 @@ import CreateCustomDashboardsMigration from "./create-custom-dashboards-migratio
import CreateOpengroundMigration from "./create-openground-migration";
import CreateOpengroundCustomModelsMigration from "./create-openground-custom-models-migration";
import CreateRuleEngineMigration from "./create-rule-engine-migration";
+import CreateChatMigration from "./create-chat-migration";
+import AddModelTypeMigration from "./add-model-type-migration";
export default async function migrations(databaseConfigId?: string) {
return Promise.all([
@@ -19,5 +21,7 @@ export default async function migrations(databaseConfigId?: string) {
CreateOpengroundMigration(databaseConfigId),
CreateOpengroundCustomModelsMigration(databaseConfigId),
CreateRuleEngineMigration(databaseConfigId),
+ CreateChatMigration(databaseConfigId),
+ AddModelTypeMigration(databaseConfigId),
]);
}
diff --git a/src/client/src/components/(playground)/chat/chat-empty-state.tsx b/src/client/src/components/(playground)/chat/chat-empty-state.tsx
new file mode 100644
index 00000000..d7bfc5a6
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/chat-empty-state.tsx
@@ -0,0 +1,91 @@
+"use client";
+
+import {
+ MessageSquare,
+ LayoutDashboard,
+ CircleDollarSign,
+ SlidersHorizontal,
+ LineChart,
+ Component,
+ BookKey,
+} from "lucide-react";
+import { Button } from "@/components/ui/button";
+import getMessage from "@/constants/messages";
+import Otter from "@/components/svg/otter";
+
+interface ChatEmptyStateProps {
+ onSendQuestion: (question: string) => void;
+ hasConfig: boolean;
+}
+
+const EXAMPLE_ICONS = [
+ LayoutDashboard,
+ CircleDollarSign,
+ SlidersHorizontal,
+ LineChart,
+ Component,
+ BookKey,
+];
+
+export default function ChatEmptyState({
+ onSendQuestion,
+ hasConfig,
+}: ChatEmptyStateProps) {
+ const m = getMessage();
+ const examples = [
+ m.CHAT_EXAMPLE_Q1,
+ m.CHAT_EXAMPLE_Q2,
+ m.CHAT_EXAMPLE_Q3,
+ m.CHAT_EXAMPLE_Q4,
+ m.CHAT_EXAMPLE_Q5,
+ m.CHAT_EXAMPLE_Q6,
+ ];
+
+ return (
+ <div className="flex flex-col items-center justify-center flex-1 px-6 py-12">
+ <div className="flex items-center justify-center w-16 h-16 rounded-full bg-stone-200 dark:bg-stone-200 mb-5">
+ {/* <MessageSquare className="w-7 h-7 text-orange-600 dark:text-orange-400" /> */}
+ <Otter className="w-12 h-12 text-stone-800 dark:text-stone-800" />
+ </div>
+ <h2 className="text-xl font-semibold text-stone-900 dark:text-stone-100 mb-1.5">
+ {m.CHAT_EMPTY_TITLE}
+ </h2>
+ <p className="text-sm text-stone-500 dark:text-stone-400 text-center max-w-lg mb-8">
+ {m.CHAT_EMPTY_DESCRIPTION}
+ </p>
+
+ {!hasConfig && (
+ <div className="bg-amber-50 dark:bg-amber-950/30 border border-amber-200 dark:border-amber-800 rounded-lg px-4 py-3 mb-8 max-w-md text-center">
+ <p className="text-sm text-amber-800 dark:text-amber-200">
+ {m.CHAT_CONFIGURE_PROVIDER}{" "}
+ <a
+ href="/chat/settings"
+ className="underline font-medium hover:text-amber-900 dark:hover:text-amber-100"
+ >
+ {m.CHAT_SETTINGS_LINK}
+ </a>{" "}
+ {m.CHAT_TO_GET_STARTED}
+ </p>
+ </div>
+ )}
+
+ <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-2.5 max-w-3xl w-full">
+ {examples.map((question, i) => {
+ const Icon = EXAMPLE_ICONS[i];
+ return (
+ <Button
+ key={question}
+ variant="outline"
+ className="h-auto py-3 px-3.5 text-left justify-start gap-2.5 text-sm font-normal text-stone-600 dark:text-stone-300 border-stone-200 dark:border-stone-700 hover:bg-stone-50 dark:hover:bg-stone-800 hover:border-stone-300 dark:hover:border-stone-600 whitespace-normal transition-colors"
+ onClick={() => onSendQuestion(question)}
+ disabled={!hasConfig}
+ >
+ <Icon className="h-4 w-4 shrink-0 text-stone-400 dark:text-stone-500" />
+ <span className="line-clamp-2">{question}</span>
+ </Button>
+ );
+ })}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/chat-floating-button.tsx b/src/client/src/components/(playground)/chat/chat-floating-button.tsx
new file mode 100644
index 00000000..11df4e5a
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/chat-floating-button.tsx
@@ -0,0 +1,61 @@
+"use client";
+
+import { useMemo } from "react";
+import { usePathname } from "next/navigation";
+import Link from "next/link";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import Otter from "@/components/svg/otter";
+
+const OTTER_TOOLTIPS = [
+ "Hi, I'm Otter 🦦, ask me anything!",
+ "Otter here! Ready to dive into your data?",
+ "Psst... Otter can run SQL so you don't have to.",
+ "Need a dashboard? Otter's got you covered.",
+ "Otter's always paddling, ready to help!",
+ "Ask Otter. It's faster than googling.",
+ "Otter knows your traces like the back of its paw.",
+ "Click me! I won't bite, I'm an otter.",
+ "Otter's a 10x engineer. Try it.",
+ "Your observability BFF, Otter.",
+];
+
+export default function ChatFloatingButton() {
+ const pathname = usePathname();
+
+ // Pick a random tooltip per page load
+ const tooltip = useMemo(
+ () => OTTER_TOOLTIPS[Math.floor(Math.random() * OTTER_TOOLTIPS.length)],
+ []
+ );
+
+ // Don't show on the chat page itself
+ if (pathname?.startsWith("/chat")) return null;
+
+ return (
+ <div className="fixed bottom-6 right-6 z-50">
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Link href="/chat">
+ <button
+ aria-label="Open Otter chat"
+ className="relative flex items-center justify-center h-14 w-14 rounded-full bg-white dark:bg-stone-300 border border-stone-200 dark:border-stone-700 shadow-lg hover:shadow-xl hover:scale-105 active:scale-95 transition-all duration-200 group"
+ >
+ <Otter className="transition-transform group-hover:rotate-6 text-stone-700" />
+ {/* Online pulse dot */}
+ <span className="absolute top-1 right-1 h-2.5 w-2.5 rounded-full bg-primary ring-2 ring-white dark:ring-stone-900">
+ <span className="absolute inset-0 rounded-full bg-primary animate-ping opacity-75" />
+ </span>
+ </button>
+ </Link>
+ </TooltipTrigger>
+ <TooltipContent side="left" className="max-w-xs text-xs">
+ <p>{tooltip}</p>
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/chat-layout.tsx b/src/client/src/components/(playground)/chat/chat-layout.tsx
new file mode 100644
index 00000000..638f2c67
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/chat-layout.tsx
@@ -0,0 +1,209 @@
+"use client";
+
+import { useEffect, useCallback } from "react";
+import { useRouter } from "next/navigation";
+import { Card } from "@/components/ui/card";
+import { ResizeablePanel } from "@/components/ui/resizeable-panel";
+import ConversationList from "./conversation-list";
+import ChatPanel from "./chat-panel";
+import { useRootStore } from "@/store";
+import {
+ getChatConversations,
+ getChatActiveId,
+ getChatHasConfig,
+ getChatConfigInfo,
+ getChatIsLoadingConversations,
+ getChatIsLoadingConfig,
+ getChatActions,
+} from "@/selectors/chat";
+import getMessage from "@/constants/messages";
+import { toast } from "sonner";
+
+interface ChatLayoutProps {
+ initialConversationId: string | null;
+}
+
+export default function ChatLayout({ initialConversationId }: ChatLayoutProps) {
+ const m = getMessage();
+ const router = useRouter();
+
+ const conversations = useRootStore(getChatConversations);
+ const activeId = useRootStore(getChatActiveId);
+ const hasConfig = useRootStore(getChatHasConfig);
+ const configInfo = useRootStore(getChatConfigInfo);
+ const loadingConversations = useRootStore(getChatIsLoadingConversations);
+ const loadingConfig = useRootStore(getChatIsLoadingConfig);
+ const {
+ setConversations,
+ setActiveConversationId,
+ setHasConfig,
+ setConfigInfo,
+ setIsLoadingConversations,
+ setIsLoadingConfig,
+ addConversation,
+ removeConversation,
+ } = useRootStore(getChatActions);
+
+ // Sync activeId with URL param
+ useEffect(() => {
+ setActiveConversationId(initialConversationId);
+ }, [initialConversationId, setActiveConversationId]);
+
+ const fetchConversations = useCallback(async () => {
+ try {
+ const res = await fetch("/api/chat/conversation");
+ const result = await res.json();
+ if (result.data) {
+ setConversations(
+ result.data.map((c: any) => ({
+ id: c.id,
+ title: c.title || m.CHAT_NEW_CONVERSATION,
+ totalCost: Number(c.totalCost) || 0,
+ totalMessages: Number(c.totalMessages) || 0,
+ totalPromptTokens: Number(c.totalPromptTokens) || 0,
+ totalCompletionTokens: Number(c.totalCompletionTokens) || 0,
+ updatedAt: c.updatedAt,
+ }))
+ );
+ }
+ } catch {
+ // Silently fail
+ } finally {
+ setIsLoadingConversations(false);
+ }
+ }, [m, setConversations, setIsLoadingConversations]);
+
+ const fetchConfig = useCallback(async () => {
+ try {
+ const [configRes, providersRes] = await Promise.all([
+ fetch("/api/chat/config").then((r) => r.json()),
+ fetch("/api/openground/providers").then((r) => r.json()),
+ ]);
+
+ const config = configRes.data;
+ setHasConfig(!!config?.provider);
+
+ if (config?.provider) {
+ const providersList = Array.isArray(providersRes) ? providersRes : providersRes?.data || [];
+ const providerObj = providersList.find((p: any) => p.providerId === config.provider);
+ const modelObj = providerObj?.supportedModels?.find((md: any) => md.id === config.model);
+
+ setConfigInfo({
+ providerName: providerObj?.displayName || config.provider,
+ modelName: modelObj?.displayName,
+ modelId: config.model,
+ inputPricePerMToken: modelObj?.inputPricePerMToken,
+ outputPricePerMToken: modelObj?.outputPricePerMToken,
+ contextWindow: modelObj?.contextWindow,
+ });
+ }
+ } catch {
+ setHasConfig(false);
+ } finally {
+ setIsLoadingConfig(false);
+ }
+ }, [setHasConfig, setConfigInfo, setIsLoadingConfig]);
+
+ useEffect(() => {
+ fetchConversations();
+ fetchConfig();
+ }, [fetchConversations, fetchConfig]);
+
+ const navigateTo = useCallback(
+ (id: string | null) => {
+ if (id) {
+ router.push(`/chat?id=${id}`);
+ } else {
+ router.push("/chat");
+ }
+ },
+ [router]
+ );
+
+ const handleNewConversation = useCallback(async (): Promise<string | null> => {
+ try {
+ const res = await fetch("/api/chat/conversation", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ title: "",
+ provider: configInfo?.providerName || "",
+ model: configInfo?.modelId || configInfo?.modelName || "",
+ }),
+ });
+ const result = await res.json();
+
+ if (result.data) {
+ const newId = result.data;
+ addConversation({
+ id: newId,
+ title: m.CHAT_NEW_CONVERSATION,
+ totalCost: 0,
+ totalMessages: 0,
+ updatedAt: new Date().toISOString(),
+ });
+ navigateTo(newId);
+ return newId;
+ }
+ } catch {
+ toast.error(m.CHAT_FAILED_TO_CREATE_CONVERSATION);
+ }
+ return null;
+ }, [addConversation, navigateTo, m, configInfo]);
+
+ const handleDeleteConversation = useCallback(
+ async (id: string) => {
+ try {
+ await fetch(`/api/chat/conversation/${id}`, { method: "DELETE" });
+ removeConversation(id);
+ if (activeId === id) {
+ navigateTo(null);
+ }
+ } catch {
+ toast.error(m.CHAT_FAILED_TO_DELETE_CONVERSATION);
+ }
+ },
+ [activeId, removeConversation, navigateTo, m]
+ );
+
+ const handleSelectConversation = useCallback(
+ (id: string) => {
+ navigateTo(id);
+ },
+ [navigateTo]
+ );
+
+ const handleNewChat = useCallback(() => {
+ navigateTo(null);
+ }, [navigateTo]);
+
+ return (
+ <Card className="flex h-full w-full overflow-hidden border-stone-200 dark:border-stone-800">
+ <ResizeablePanel
+ defaultWidth={280}
+ minWidth={220}
+ maxWidth={400}
+ handlePosition="right"
+ className="border-r border-stone-200 dark:border-stone-800"
+ >
+ <ConversationList
+ conversations={conversations}
+ activeId={activeId}
+ onSelect={handleSelectConversation}
+ onDelete={handleDeleteConversation}
+ onNew={handleNewChat}
+ isLoading={loadingConversations}
+ />
+ </ResizeablePanel>
+
+ <div className="flex-1 min-w-0 bg-white dark:bg-stone-950">
+ <ChatPanel
+ conversationId={activeId}
+ hasConfig={hasConfig && !loadingConfig}
+ configInfo={configInfo}
+ onNewConversation={handleNewConversation}
+ />
+ </div>
+ </Card>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/chat-panel.tsx b/src/client/src/components/(playground)/chat/chat-panel.tsx
new file mode 100644
index 00000000..a77875b5
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/chat-panel.tsx
@@ -0,0 +1,289 @@
+"use client";
+
+import { useCallback, useEffect, useRef } from "react";
+import getMessage from "@/constants/messages";
+import { useRootStore } from "@/store";
+import {
+ getChatMessages,
+ getChatIsStreaming,
+ getChatActions,
+} from "@/selectors/chat";
+import { ChatConfigInfo } from "./message-input";
+import MessageList from "./message-list";
+import MessageInput from "./message-input";
+import ChatEmptyState from "./chat-empty-state";
+
+interface ChatPanelProps {
+ conversationId: string | null;
+ hasConfig: boolean;
+ configInfo?: ChatConfigInfo | null;
+ onNewConversation: () => Promise<string | null>;
+}
+
+export default function ChatPanel({
+ conversationId,
+ hasConfig,
+ configInfo,
+ onNewConversation,
+}: ChatPanelProps) {
+ const messages = useRootStore(getChatMessages);
+ const isStreaming = useRootStore(getChatIsStreaming);
+ const {
+ setMessages,
+ addMessage,
+ updateLastMessage,
+ setIsStreaming,
+ updateConversation,
+ } = useRootStore(getChatActions);
+
+ const inputRef = useRef("");
+ const abortControllerRef = useRef<AbortController | null>(null);
+ const skipNextLoadRef = useRef(false);
+
+ // Input state — ref avoids re-renders, state keeps textarea controlled
+ const [inputValue, setInputValue] = useInputState(inputRef);
+
+ // Load messages when conversation changes
+ useEffect(() => {
+ if (skipNextLoadRef.current) {
+ skipNextLoadRef.current = false;
+ return;
+ }
+
+ if (!conversationId) {
+ setMessages([]);
+ return;
+ }
+
+ fetch(`/api/chat/conversation/${conversationId}`)
+ .then((res) => res.json())
+ .then((res) => {
+ if (res.data?.messages?.length > 0) {
+ setMessages(
+ res.data.messages.map((m: any) => ({
+ id: m.id,
+ role: m.role,
+ content: m.content,
+ promptTokens: Number(m.promptTokens) || 0,
+ completionTokens: Number(m.completionTokens) || 0,
+ cost: Number(m.cost) || 0,
+ queryRowsRead: Number(m.queryRowsRead) || 0,
+ queryExecutionTimeMs: Number(m.queryExecutionTimeMs) || 0,
+ createdAt: m.createdAt,
+ }))
+ );
+ }
+ // Update sidebar with this conversation's latest data
+ if (res.data?.conversation) {
+ const conv = res.data.conversation;
+ updateConversation(conversationId, {
+ title: conv.title,
+ totalCost: Number(conv.totalCost) || 0,
+ totalMessages: Number(conv.totalMessages) || 0,
+ updatedAt: conv.updatedAt,
+ });
+ }
+ })
+ .catch(() => {});
+ }, [conversationId, setMessages, updateConversation]);
+
+ /**
+ * Fetch a single conversation and update the store (sidebar + messages).
+ * Called after streaming completes to pick up stats, title, and query results.
+ */
+ const refreshConversation = useCallback(
+ (convId: string) => {
+ fetch(`/api/chat/conversation/${convId}`)
+ .then((res) => res.json())
+ .then((res) => {
+ if (res.data?.conversation) {
+ const conv = res.data.conversation;
+ const updates: any = {
+ totalCost: Number(conv.totalCost) || 0,
+ totalMessages: Number(conv.totalMessages) || 0,
+ updatedAt: conv.updatedAt,
+ };
+ // Only update title if it's non-empty (title generation may still be pending)
+ if (conv.title) {
+ updates.title = conv.title;
+ }
+ updateConversation(convId, updates);
+ }
+ if (res.data?.messages?.length > 0) {
+ setMessages(
+ res.data.messages.map((m: any) => ({
+ id: m.id,
+ role: m.role,
+ content: m.content,
+ promptTokens: Number(m.promptTokens) || 0,
+ completionTokens: Number(m.completionTokens) || 0,
+ cost: Number(m.cost) || 0,
+ queryRowsRead: Number(m.queryRowsRead) || 0,
+ queryExecutionTimeMs: Number(m.queryExecutionTimeMs) || 0,
+ createdAt: m.createdAt,
+ }))
+ );
+ }
+ })
+ .catch(() => {});
+ },
+ [setMessages, updateConversation]
+ );
+
+ const sendMessage = useCallback(
+ async (text?: string) => {
+ const content = text || inputRef.current.trim();
+ if (!content || isStreaming) return;
+
+ let currentConvId = conversationId;
+
+ if (!currentConvId) {
+ skipNextLoadRef.current = true;
+ currentConvId = await onNewConversation();
+ if (!currentConvId) {
+ skipNextLoadRef.current = false;
+ return;
+ }
+ }
+
+ // Add user message optimistically
+ addMessage({ role: "user", content, createdAt: new Date().toISOString() });
+ setInputValue("");
+ setIsStreaming(true);
+
+ // Add empty assistant message for streaming
+ addMessage({ role: "assistant", content: "", createdAt: new Date().toISOString() });
+
+ try {
+ const controller = new AbortController();
+ abortControllerRef.current = controller;
+
+ const res = await fetch("/api/chat/message", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ conversationId: currentConvId, content }),
+ signal: controller.signal,
+ });
+
+ if (!res.ok) {
+ const errText = await res.text();
+ throw new Error(errText || getMessage().CHAT_FAILED_TO_GET_RESPONSE);
+ }
+
+ const reader = res.body?.getReader();
+ if (!reader) throw new Error(getMessage().CHAT_NO_RESPONSE_STREAM);
+
+ const decoder = new TextDecoder();
+ let fullText = "";
+
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ const chunk = decoder.decode(value, { stream: true });
+ fullText += chunk;
+ updateLastMessage(fullText);
+ }
+
+ // After stream completes, refresh this conversation from server.
+ // Two refreshes: first for message stats/query results, second for title
+ // (title generation is a separate LLM call that takes longer).
+ if (currentConvId && !fullText.startsWith("**Error:**")) {
+ // 1st refresh: message stats, cost, query results
+ setTimeout(() => refreshConversation(currentConvId!), 2000);
+ // 2nd refresh: title (generateText takes 3-5s)
+ setTimeout(() => {
+ fetch(`/api/chat/conversation/${currentConvId}`)
+ .then((r) => r.json())
+ .then((res) => {
+ if (res.data?.conversation?.title) {
+ updateConversation(currentConvId!, {
+ title: res.data.conversation.title,
+ });
+ }
+ })
+ .catch(() => {});
+ }, 6000);
+ }
+ } catch (e: any) {
+ if (e.name === "AbortError") {
+ // Remove empty assistant message on cancel
+ setMessages(messages.filter((m, i) => !(i === messages.length - 1 && m.role === "assistant" && !m.content)));
+ } else {
+ updateLastMessage(`**${getMessage().CHAT_ERROR_PREFIX}** ${e.message || getMessage().CHAT_SOMETHING_WENT_WRONG}`);
+ }
+ } finally {
+ setIsStreaming(false);
+ abortControllerRef.current = null;
+ }
+ },
+ [conversationId, isStreaming, messages, onNewConversation, addMessage, updateLastMessage, setMessages, setIsStreaming, setInputValue, refreshConversation]
+ );
+
+ const handleExecuteQuery = useCallback(
+ async (query: string, messageId?: string) => {
+ try {
+ const res = await fetch("/api/chat/message/execute", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ messageId, query }),
+ });
+ const result = await res.json();
+ if (!res.ok || result.err) {
+ return { err: typeof result.err === "string" ? result.err : JSON.stringify(result.err) };
+ }
+ return { data: result.data, stats: result.stats };
+ } catch (e: any) {
+ return { err: e.message || getMessage().CHAT_QUERY_EXECUTION_FAILED };
+ }
+ },
+ []
+ );
+
+ const showEmptyState = !conversationId && messages.length === 0 && !isStreaming;
+
+ return (
+ <div className="flex flex-col h-full">
+ {showEmptyState ? (
+ <ChatEmptyState
+ onSendQuestion={(q) => sendMessage(q)}
+ hasConfig={hasConfig}
+ />
+ ) : (
+ <MessageList
+ messages={messages}
+ isStreaming={isStreaming}
+ onExecuteQuery={handleExecuteQuery}
+ />
+ )}
+ <MessageInput
+ value={inputValue}
+ onChange={setInputValue}
+ onSubmit={() => sendMessage()}
+ isLoading={isStreaming}
+ disabled={!hasConfig}
+ configInfo={configInfo}
+ />
+ </div>
+ );
+}
+
+/**
+ * Custom hook to manage input value via ref (avoids re-renders)
+ * but still provides a state value for the controlled textarea.
+ */
+function useInputState(ref: React.MutableRefObject<string>): [string, (val: string) => void] {
+ const [value, setValue] = __useState(ref.current);
+
+ const setInputValue = useCallback(
+ (val: string) => {
+ ref.current = val;
+ setValue(val);
+ },
+ [ref]
+ );
+
+ return [value, setInputValue];
+}
+
+// Import useState with alias to avoid conflict
+import { useState as __useState } from "react";
diff --git a/src/client/src/components/(playground)/chat/chat-settings-form.tsx b/src/client/src/components/(playground)/chat/chat-settings-form.tsx
new file mode 100644
index 00000000..10e578a7
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/chat-settings-form.tsx
@@ -0,0 +1,295 @@
+"use client";
+
+import { useState, useEffect, useMemo } from "react";
+import { Button } from "@/components/ui/button";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { toast } from "sonner";
+import { Loader2, CheckCircle, Info } from "lucide-react";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import getMessage from "@/constants/messages";
+import SecretForm from "@/components/(playground)/vault/form";
+
+interface ModelMetadata {
+ id: string;
+ displayName: string;
+ contextWindow?: number;
+ inputPricePerMToken?: number;
+ outputPricePerMToken?: number;
+}
+
+interface ProviderMetadata {
+ providerId: string;
+ displayName: string;
+ supportedModels: ModelMetadata[];
+}
+
+export default function ChatSettingsForm() {
+ const m = getMessage();
+ const [provider, setProvider] = useState("");
+ const [model, setModel] = useState("");
+ const [vaultId, setVaultId] = useState("");
+ const [providers, setProviders] = useState<ProviderMetadata[]>([]);
+ const [vaultSecrets, setVaultSecrets] = useState<any[]>([]);
+ const [saving, setSaving] = useState(false);
+ const [loading, setLoading] = useState(true);
+ const [hasExistingConfig, setHasExistingConfig] = useState(false);
+
+ // Load existing config, providers from manage-models, and vault secrets
+ useEffect(() => {
+ Promise.all([
+ fetch("/api/chat/config").then((r) => r.json()),
+ fetch("/api/openground/providers").then((r) => r.json()),
+ fetch("/api/vault/get", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({}),
+ }).then((r) => r.json()),
+ ])
+ .then(([configRes, providersRes, vaultRes]) => {
+ if (configRes.data?.provider) {
+ setProvider(configRes.data.provider);
+ setModel(configRes.data.model || "");
+ setVaultId(configRes.data.vaultId || "");
+ setHasExistingConfig(true);
+ }
+ if (Array.isArray(providersRes)) {
+ setProviders(providersRes);
+ } else if (providersRes?.data && Array.isArray(providersRes.data)) {
+ setProviders(providersRes.data);
+ }
+ if (Array.isArray(vaultRes)) {
+ setVaultSecrets(vaultRes);
+ } else if (vaultRes?.data) {
+ setVaultSecrets(vaultRes.data);
+ }
+ })
+ .catch(() => {
+ toast.error(m.CHAT_SETTINGS_LOAD_FAILED);
+ })
+ .finally(() => setLoading(false));
+ }, []);
+
+ const availableModels = useMemo(() => {
+ if (!provider) return [];
+ const p = providers.find((pr) => pr.providerId === provider);
+ return p?.supportedModels || [];
+ }, [provider, providers]);
+
+ const handleProviderChange = (newProvider: string) => {
+ setProvider(newProvider);
+ const p = providers.find((pr) => pr.providerId === newProvider);
+ const models = p?.supportedModels || [];
+ const currentModelExists = models.some((md) => md.id === model);
+ if (!currentModelExists) {
+ setModel("");
+ }
+ };
+
+ const handleSave = async () => {
+ if (!provider || !model || !vaultId) {
+ toast.error(m.CHAT_SETTINGS_FILL_ALL);
+ return;
+ }
+
+ setSaving(true);
+ try {
+ const res = await fetch("/api/chat/config", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ provider, model, vaultId }),
+ });
+
+ if (!res.ok) {
+ const err = await res.json();
+ throw new Error(typeof err === "string" ? err : JSON.stringify(err));
+ }
+
+ setHasExistingConfig(true);
+ toast.success(m.CHAT_SETTINGS_SAVED);
+ } catch (e: any) {
+ toast.error(e.message || m.CHAT_SETTINGS_SAVE_FAILED);
+ } finally {
+ setSaving(false);
+ }
+ };
+
+ if (loading) {
+ return (
+ <div className="flex items-center justify-center py-12">
+ <Loader2 className="h-6 w-6 animate-spin text-stone-400" />
+ </div>
+ );
+ }
+
+ const selectedProviderName = providers.find((p) => p.providerId === provider)?.displayName;
+ const selectedModelObj = availableModels.find((md) => md.id === model);
+ const selectedModelName = selectedModelObj?.displayName;
+ const selectedVaultName = vaultSecrets.find((s: any) => s.id === vaultId)?.key;
+
+ return (
+ <div className="max-w-lg space-y-6">
+ {/* Current config indicator */}
+ {hasExistingConfig && (
+ <div className="flex items-start gap-3 p-4 rounded-lg bg-green-50 dark:bg-green-950/20 border border-green-200 dark:border-green-800">
+ <CheckCircle className="h-5 w-5 text-green-600 dark:text-green-400 shrink-0 mt-0.5" />
+ <div className="flex-1">
+ <div className="flex items-center gap-2">
+ <p className="text-sm font-medium text-green-800 dark:text-green-200">
+ {m.CONFIGURED}
+ </p>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Info className="h-3.5 w-3.5 text-green-600 dark:text-green-400 cursor-help" />
+ </TooltipTrigger>
+ <TooltipContent side="right" className="max-w-xs text-xs space-y-1.5 p-3">
+ <p className="font-medium">Chat Configuration</p>
+ <p><span className="text-stone-400">Provider:</span> {selectedProviderName || provider}</p>
+ <p><span className="text-stone-400">Model:</span> {selectedModelName || model}</p>
+ <p><span className="text-stone-400">API Key:</span> {selectedVaultName || "configured"}</p>
+ {selectedModelObj && (
+ <>
+ <hr className="border-stone-200 dark:border-stone-700" />
+ <p className="font-medium">Pricing (per message)</p>
+ <p><span className="text-stone-400">Input:</span> ${selectedModelObj.inputPricePerMToken}/M tokens</p>
+ <p><span className="text-stone-400">Output:</span> ${selectedModelObj.outputPricePerMToken}/M tokens</p>
+ <p><span className="text-stone-400">Context:</span> {selectedModelObj.contextWindow?.toLocaleString()} tokens</p>
+ <hr className="border-stone-200 dark:border-stone-700" />
+ <p className="font-medium">Cost calculation</p>
+ <p className="text-stone-400">cost = (input_tokens / 1M) × input_price + (output_tokens / 1M) × output_price</p>
+ </>
+ )}
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ <p className="text-xs text-green-600 dark:text-green-400 mt-0.5">
+ {selectedProviderName} / {selectedModelName || model} / {selectedVaultName || vaultId}
+ </p>
+ </div>
+ </div>
+ )}
+
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_SETTINGS_PROVIDER_LABEL}
+ </label>
+ <Select value={provider} onValueChange={handleProviderChange}>
+ <SelectTrigger className="bg-white dark:bg-stone-900">
+ <SelectValue placeholder={m.CHAT_SETTINGS_PROVIDER_PLACEHOLDER} />
+ </SelectTrigger>
+ <SelectContent>
+ {providers.map((p) => (
+ <SelectItem key={p.providerId} value={p.providerId}>
+ {p.displayName}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_SETTINGS_MODEL_LABEL}
+ </label>
+ <Select value={model} onValueChange={setModel} disabled={!provider}>
+ <SelectTrigger className="bg-white dark:bg-stone-900">
+ <SelectValue
+ placeholder={
+ provider
+ ? m.CHAT_SETTINGS_MODEL_PLACEHOLDER
+ : m.CHAT_SETTINGS_SELECT_PROVIDER_FIRST
+ }
+ />
+ </SelectTrigger>
+ <SelectContent>
+ {availableModels.map((md) => (
+ <SelectItem key={md.id} value={md.id}>
+ {md.displayName}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <p className="text-xs text-stone-400 dark:text-stone-500">
+ {m.CHAT_SETTINGS_MODEL_HINT}{" "}
+ <a
+ href="/settings/manage-models"
+ className="underline hover:text-stone-600 dark:hover:text-stone-300"
+ >
+ {m.CHAT_SETTINGS_MANAGE_MODELS}
+ </a>
+ {m.CHAT_SETTINGS_MODEL_HINT_SUFFIX}
+ </p>
+ </div>
+
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_SETTINGS_API_KEY_LABEL}
+ </label>
+ <Select value={vaultId} onValueChange={setVaultId}>
+ <SelectTrigger className="bg-white dark:bg-stone-900">
+ <SelectValue placeholder={m.CHAT_SETTINGS_API_KEY_PLACEHOLDER} />
+ </SelectTrigger>
+ <SelectContent>
+ {vaultSecrets.map((secret: any) => (
+ <SelectItem key={secret.id} value={secret.id}>
+ {secret.key}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <div className="flex flex-wrap items-center gap-1 text-xs text-stone-400 dark:text-stone-500">
+ {m.CHAT_SETTINGS_API_KEY_HINT_PREFIX}{" "}
+ <a
+ href="/vault"
+ className="underline hover:text-stone-600 dark:hover:text-stone-300"
+ >
+ {m.FEATURE_VAULT}
+ </a>{" "}
+ {m.CHAT_SETTINGS_API_KEY_HINT_SUFFIX}
+ {" or "}
+ <SecretForm
+ successCallback={() => {
+ // Reload vault secrets after creation
+ fetch("/api/vault/get", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({}),
+ })
+ .then((r) => r.json())
+ .then((res) => {
+ const secrets = Array.isArray(res) ? res : res?.data || [];
+ setVaultSecrets(secrets);
+ // Auto-select the newly created secret (last one)
+ if (secrets.length > 0) {
+ setVaultId(secrets[secrets.length - 1].id);
+ }
+ })
+ .catch(() => {});
+ }}
+ >
+ <Button
+ variant="ghost"
+ className="py-0 h-auto px-0 text-xs text-primary hover:bg-transparent underline"
+ >
+ {m.EVALUATION_CREATE_NEW}
+ </Button>
+ </SecretForm>
+ </div>
+ </div>
+
+ <Button onClick={handleSave} disabled={saving}>
+ {saving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
+ {hasExistingConfig ? m.OPENGROUND_UPDATE_CONFIGURATION : m.CHAT_SETTINGS_SAVE}
+ </Button>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/conversation-item.tsx b/src/client/src/components/(playground)/chat/conversation-item.tsx
new file mode 100644
index 00000000..e5ea290d
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/conversation-item.tsx
@@ -0,0 +1,101 @@
+"use client";
+
+import { useState } from "react";
+import { Trash2, Coins } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { cn } from "@/lib/utils";
+import getMessage from "@/constants/messages";
+import { formatDistanceToNow } from "date-fns";
+
+interface ConversationItemProps {
+ id: string;
+ title: string;
+ totalCost: number;
+ totalMessages: number;
+ updatedAt: string;
+ isActive: boolean;
+ onClick: () => void;
+ onDelete: () => void;
+}
+
+export default function ConversationItem({
+ title,
+ totalCost,
+ updatedAt,
+ isActive,
+ onClick,
+ onDelete,
+}: ConversationItemProps) {
+ const [showDelete, setShowDelete] = useState(false);
+ const [confirming, setConfirming] = useState(false);
+ const messages = getMessage();
+
+ const timeAgo = (() => {
+ try {
+ return formatDistanceToNow(new Date(updatedAt), { addSuffix: true });
+ } catch {
+ return "";
+ }
+ })();
+
+ const handleDelete = (e: React.MouseEvent) => {
+ e.stopPropagation();
+ if (confirming) {
+ onDelete();
+ setConfirming(false);
+ } else {
+ setConfirming(true);
+ setTimeout(() => setConfirming(false), 3000);
+ }
+ };
+
+ return (
+ <div
+ onClick={onClick}
+ onMouseEnter={() => setShowDelete(true)}
+ onMouseLeave={() => {
+ setShowDelete(false);
+ setConfirming(false);
+ }}
+ className={cn(
+ "group flex items-center gap-2 px-3 py-2.5 rounded-lg cursor-pointer transition-colors",
+ isActive
+ ? "bg-stone-200/70 dark:bg-stone-800"
+ : "hover:bg-stone-100 dark:hover:bg-stone-800/50"
+ )}
+ >
+ <div className="flex-1 min-w-0">
+ <p className="text-sm font-medium text-stone-800 dark:text-stone-200 truncate">
+ {title || messages.CHAT_NEW_CONVERSATION}
+ </p>
+ <div className="flex items-center gap-2 mt-0.5">
+ <span className="text-xs text-stone-400 dark:text-stone-500">
+ {timeAgo}
+ </span>
+ {totalCost > 0 && (
+ <span className="flex items-center gap-0.5 text-xs text-stone-400 dark:text-stone-500">
+ <Coins className="h-2.5 w-2.5" />
+ ${totalCost.toFixed(4)}
+ </span>
+ )}
+ </div>
+ </div>
+
+ {showDelete && (
+ <Button
+ variant="ghost"
+ size="icon"
+ className={cn(
+ "h-7 w-7 shrink-0",
+ confirming
+ ? "text-red-500 dark:text-red-400"
+ : "text-stone-400 hover:text-red-500 dark:hover:text-red-400"
+ )}
+ onClick={handleDelete}
+ >
+ <Trash2 className="h-3.5 w-3.5" />
+ </Button>
+ )}
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/conversation-list.tsx b/src/client/src/components/(playground)/chat/conversation-list.tsx
new file mode 100644
index 00000000..8c566e69
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/conversation-list.tsx
@@ -0,0 +1,152 @@
+"use client";
+
+import { useState, useMemo } from "react";
+import { Plus, Search, Settings } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import getMessage from "@/constants/messages";
+import ConversationItem from "./conversation-item";
+import { useRouter } from "next/navigation";
+
+interface Conversation {
+ id: string;
+ title: string;
+ totalCost: number;
+ totalMessages: number;
+ updatedAt: string;
+}
+
+interface ConversationListProps {
+ conversations: Conversation[];
+ activeId: string | null;
+ onSelect: (id: string) => void;
+ onDelete: (id: string) => void;
+ onNew: () => void;
+ isLoading: boolean;
+}
+
+function groupByDate(conversations: Conversation[]) {
+ const now = new Date();
+ const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
+ const yesterday = new Date(today.getTime() - 86400000);
+ const weekAgo = new Date(today.getTime() - 7 * 86400000);
+
+ const groups: { label: string; items: Conversation[] }[] = [
+ { label: "Today", items: [] },
+ { label: "Yesterday", items: [] },
+ { label: "Previous 7 Days", items: [] },
+ { label: "Older", items: [] },
+ ];
+
+ for (const conv of conversations) {
+ const date = new Date(conv.updatedAt);
+ if (date >= today) {
+ groups[0].items.push(conv);
+ } else if (date >= yesterday) {
+ groups[1].items.push(conv);
+ } else if (date >= weekAgo) {
+ groups[2].items.push(conv);
+ } else {
+ groups[3].items.push(conv);
+ }
+ }
+
+ return groups.filter((g) => g.items.length > 0);
+}
+
+export default function ConversationList({
+ conversations,
+ activeId,
+ onSelect,
+ onDelete,
+ onNew,
+ isLoading,
+}: ConversationListProps) {
+ const [search, setSearch] = useState("");
+ const m = getMessage();
+ const router = useRouter();
+
+ const filtered = useMemo(() => {
+ if (!search.trim()) return conversations;
+ const q = search.toLowerCase();
+ return conversations.filter((c) => c.title.toLowerCase().includes(q));
+ }, [conversations, search]);
+
+ const groups = useMemo(() => groupByDate(filtered), [filtered]);
+
+ return (
+ <div className="flex flex-col h-full bg-stone-50 dark:bg-stone-900">
+ {/* Header */}
+ <div className="p-3 space-y-3 border-b border-stone-200 dark:border-stone-800">
+ <div className="flex items-center gap-2">
+ <Button
+ onClick={onNew}
+ className="flex-1 justify-center gap-2"
+ size="sm"
+ >
+ <Plus className="h-4 w-4" />
+ {m.CHAT_NEW_CHAT}
+ </Button>
+ <Button
+ variant="outline"
+ size="icon"
+ className="h-8 w-8 shrink-0 border-stone-200 dark:border-stone-700"
+ onClick={() => router.push("/chat/settings")}
+ >
+ <Settings className="h-4 w-4" />
+ </Button>
+ </div>
+ <div className="relative">
+ <Search className="absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-stone-400 dark:text-stone-500" />
+ <Input
+ value={search}
+ onChange={(e) => setSearch(e.target.value)}
+ placeholder={m.CHAT_SEARCH_CONVERSATIONS}
+ className="pl-8 h-8 text-sm bg-white dark:bg-stone-800 border-stone-200 dark:border-stone-700"
+ />
+ </div>
+ </div>
+
+ {/* Conversation list */}
+ <div className="flex-1 overflow-y-auto px-2 py-2">
+ {isLoading ? (
+ <div className="space-y-2 px-1">
+ {[1, 2, 3].map((i) => (
+ <div
+ key={i}
+ className="h-14 rounded-lg bg-stone-100 dark:bg-stone-800 animate-pulse"
+ />
+ ))}
+ </div>
+ ) : groups.length === 0 ? (
+ <p className="text-sm text-stone-500 dark:text-stone-400 text-center mt-8 px-4">
+ {search
+ ? m.CHAT_NO_MATCHING_CONVERSATIONS
+ : m.CHAT_NO_CONVERSATIONS_YET}
+ </p>
+ ) : (
+ groups.map((group) => (
+ <div key={group.label} className="mb-3">
+ <p className="text-[11px] font-medium text-stone-500 dark:text-stone-400 uppercase tracking-wider px-3 mb-1">
+ {group.label}
+ </p>
+ {group.items.map((conv) => (
+ <ConversationItem
+ key={conv.id}
+ id={conv.id}
+ title={conv.title}
+ totalCost={conv.totalCost}
+ totalMessages={conv.totalMessages}
+ updatedAt={conv.updatedAt}
+ isActive={conv.id === activeId}
+ onClick={() => onSelect(conv.id)}
+ onDelete={() => onDelete(conv.id)}
+ />
+ ))}
+ </div>
+ ))
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/dashboard-import-card.tsx b/src/client/src/components/(playground)/chat/dashboard-import-card.tsx
new file mode 100644
index 00000000..4601a35f
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/dashboard-import-card.tsx
@@ -0,0 +1,159 @@
+"use client";
+
+import { useState } from "react";
+import { Button } from "@/components/ui/button";
+import { LayoutDashboard, Loader2, Check, ExternalLink, Download } from "lucide-react";
+import { toast } from "sonner";
+import getMessage from "@/constants/messages";
+import Link from "next/link";
+
+interface DashboardImportCardProps {
+ dashboardJson: string;
+}
+
+export default function DashboardImportCard({
+ dashboardJson,
+}: DashboardImportCardProps) {
+ const [importing, setImporting] = useState(false);
+ const [imported, setImported] = useState(false);
+ const [dashboardId, setDashboardId] = useState<string | null>(null);
+
+ let dashboard: any = null;
+ try {
+ dashboard = JSON.parse(dashboardJson);
+ } catch {
+ return null;
+ }
+
+ if (!dashboard?.title || !dashboard?.widgets) return null;
+
+ const widgetCount = Object.keys(dashboard.widgets).length;
+
+ const handleImport = async () => {
+ setImporting(true);
+ try {
+ const res = await fetch("/api/manage-dashboard/board/layout/import", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: dashboardJson,
+ });
+
+ const result = await res.json();
+
+ if (!res.ok || result.error) {
+ throw new Error(result.error || "Failed to import dashboard");
+ }
+
+ setImported(true);
+ const newId = result.data?.id;
+ if (newId) setDashboardId(newId);
+ toast.success(`Dashboard "${dashboard.title}" imported successfully`);
+ } catch (e: any) {
+ toast.error(e.message || "Failed to import dashboard");
+ } finally {
+ setImporting(false);
+ }
+ };
+
+ return (
+ <div className="my-3 rounded-lg border border-stone-200 dark:border-stone-700 overflow-hidden bg-white dark:bg-stone-900">
+ {/* Header */}
+ <div className="flex items-center gap-3 px-4 py-3 bg-stone-50 dark:bg-stone-800/50 border-b border-stone-200 dark:border-stone-700">
+ <div className="flex-shrink-0 p-2 rounded-lg bg-orange-100 dark:bg-orange-950/30">
+ <LayoutDashboard className="h-5 w-5 text-orange-600 dark:text-orange-400" />
+ </div>
+ <div className="flex-1 min-w-0">
+ <p className="text-sm font-semibold text-stone-900 dark:text-stone-100">
+ {dashboard.title}
+ </p>
+ <p className="text-xs text-stone-500 dark:text-stone-400">
+ {widgetCount} widgets &middot; Ready to import
+ </p>
+ </div>
+ </div>
+
+ {/* Description */}
+ {dashboard.description && (
+ <div className="px-4 py-2 border-b border-stone-100 dark:border-stone-800">
+ <p className="text-xs text-stone-500 dark:text-stone-400">
+ {dashboard.description}
+ </p>
+ </div>
+ )}
+
+ {/* Widget preview */}
+ <div className="px-4 py-3">
+ <div className="flex flex-wrap gap-1.5">
+ {Object.values(dashboard.widgets)
+ .slice(0, 8)
+ .map((widget: any) => (
+ <span
+ key={widget.id}
+ className="inline-flex items-center gap-1 px-2 py-1 rounded text-[11px] bg-stone-100 dark:bg-stone-800 text-stone-600 dark:text-stone-300"
+ >
+ <span className="font-medium">{widget.type?.replace("_", " ")}</span>
+ <span className="text-stone-400 dark:text-stone-500">&middot;</span>
+ <span className="truncate max-w-[120px]">{widget.title}</span>
+ </span>
+ ))}
+ {widgetCount > 8 && (
+ <span className="inline-flex items-center px-2 py-1 rounded text-[11px] bg-stone-100 dark:bg-stone-800 text-stone-400">
+ +{widgetCount - 8} more
+ </span>
+ )}
+ </div>
+ </div>
+
+ {/* Action */}
+ <div className="flex items-center gap-2 px-4 py-3 border-t border-stone-200 dark:border-stone-700 bg-stone-50 dark:bg-stone-800/30">
+ {imported ? (
+ <div className="flex items-center gap-3">
+ <div className="flex items-center gap-2 text-green-600 dark:text-green-400">
+ <Check className="h-4 w-4" />
+ <span className="text-sm font-medium">Dashboard imported</span>
+ </div>
+ {dashboardId && (
+ <Link href={`/home?dashboardId=${dashboardId}`}>
+ <Button variant="outline" size="sm" className="gap-1.5 text-xs">
+ Open Dashboard
+ <ExternalLink className="h-3 w-3" />
+ </Button>
+ </Link>
+ )}
+ </div>
+ ) : (
+ <Button
+ onClick={handleImport}
+ disabled={importing}
+ size="sm"
+ className="gap-2"
+ >
+ {importing ? (
+ <Loader2 className="h-4 w-4 animate-spin" />
+ ) : (
+ <LayoutDashboard className="h-4 w-4" />
+ )}
+ {importing ? "Importing..." : "Import Dashboard"}
+ </Button>
+ )}
+ <Button
+ variant="outline"
+ size="sm"
+ className="gap-1.5 ml-auto"
+ onClick={() => {
+ const blob = new Blob([dashboardJson], { type: "application/json" });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ a.href = url;
+ a.download = `${(dashboard.title || "dashboard").replace(/\s+/g, "-").toLowerCase()}.json`;
+ a.click();
+ URL.revokeObjectURL(url);
+ }}
+ >
+ <Download className="h-3.5 w-3.5" />
+ Download JSON
+ </Button>
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/entity-card.tsx b/src/client/src/components/(playground)/chat/entity-card.tsx
new file mode 100644
index 00000000..2f8314a3
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/entity-card.tsx
@@ -0,0 +1,100 @@
+"use client";
+
+import Link from "next/link";
+import {
+ SlidersHorizontal,
+ BookOpen,
+ BookKey,
+ Component,
+ SettingsIcon,
+ MonitorCog,
+ ExternalLink,
+} from "lucide-react";
+
+const ENTITY_ICONS: Record<string, React.ElementType> = {
+ rule: SlidersHorizontal,
+ context: BookOpen,
+ prompt: Component,
+ model: SettingsIcon,
+ evaluation: MonitorCog,
+ vault: BookKey,
+};
+
+const ENTITY_COLORS: Record<string, { bg: string; border: string; icon: string; text: string }> = {
+ rule: {
+ bg: "bg-purple-50 dark:bg-purple-950/20",
+ border: "border-purple-200 dark:border-purple-800",
+ icon: "text-purple-600 dark:text-purple-400",
+ text: "text-purple-700 dark:text-purple-300",
+ },
+ context: {
+ bg: "bg-blue-50 dark:bg-blue-950/20",
+ border: "border-blue-200 dark:border-blue-800",
+ icon: "text-blue-600 dark:text-blue-400",
+ text: "text-blue-700 dark:text-blue-300",
+ },
+ prompt: {
+ bg: "bg-green-50 dark:bg-green-950/20",
+ border: "border-green-200 dark:border-green-800",
+ icon: "text-green-600 dark:text-green-400",
+ text: "text-green-700 dark:text-green-300",
+ },
+ model: {
+ bg: "bg-orange-50 dark:bg-orange-950/20",
+ border: "border-orange-200 dark:border-orange-800",
+ icon: "text-orange-600 dark:text-orange-400",
+ text: "text-orange-700 dark:text-orange-300",
+ },
+ evaluation: {
+ bg: "bg-yellow-50 dark:bg-yellow-950/20",
+ border: "border-yellow-200 dark:border-yellow-800",
+ icon: "text-yellow-600 dark:text-yellow-400",
+ text: "text-yellow-700 dark:text-yellow-300",
+ },
+ vault: {
+ bg: "bg-rose-50 dark:bg-rose-950/20",
+ border: "border-rose-200 dark:border-rose-800",
+ icon: "text-rose-600 dark:text-rose-400",
+ text: "text-rose-700 dark:text-rose-300",
+ },
+};
+
+const DEFAULT_COLORS = {
+ bg: "bg-stone-50 dark:bg-stone-800",
+ border: "border-stone-200 dark:border-stone-700",
+ icon: "text-stone-500 dark:text-stone-400",
+ text: "text-stone-700 dark:text-stone-300",
+};
+
+interface EntityCardProps {
+ type: string;
+ name: string;
+ url: string;
+}
+
+export default function EntityCard({ type, name, url }: EntityCardProps) {
+ const Icon = ENTITY_ICONS[type] || ExternalLink;
+ const colors = ENTITY_COLORS[type] || DEFAULT_COLORS;
+ const label = type.charAt(0).toUpperCase() + type.slice(1);
+
+ return (
+ <Link href={url} className="block no-underline">
+ <div
+ className={`flex items-center gap-3 px-3 py-2.5 rounded-lg border ${colors.bg} ${colors.border} hover:shadow-sm transition-shadow cursor-pointer group mt-2`}
+ >
+ <div className={`flex-shrink-0 p-1.5 rounded-md ${colors.bg}`}>
+ <Icon className={`h-4 w-4 ${colors.icon}`} />
+ </div>
+ <div className="flex-1 min-w-0">
+ <p className={`text-xs font-medium ${colors.text} uppercase tracking-wide`}>
+ {label}
+ </p>
+ <p className="text-sm font-medium text-stone-900 dark:text-stone-100 truncate">
+ {name || label}
+ </p>
+ </div>
+ <ExternalLink className="h-3.5 w-3.5 text-stone-400 dark:text-stone-500 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0" />
+ </div>
+ </Link>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/message-bubble.tsx b/src/client/src/components/(playground)/chat/message-bubble.tsx
new file mode 100644
index 00000000..d6ebde84
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/message-bubble.tsx
@@ -0,0 +1,375 @@
+"use client";
+
+import { useMemo } from "react";
+import ReactMarkdown from "react-markdown";
+import SqlBlock from "./sql-block";
+import EntityCard from "./entity-card";
+import DashboardImportCard from "./dashboard-import-card";
+import getMessage from "@/constants/messages";
+import { Coins, Clock, Database, User, Bot } from "lucide-react";
+
+interface EntityLink {
+ type: string;
+ id?: string;
+ name: string;
+ url: string;
+}
+
+/**
+ * Parse ```entities and ```dashboard blocks from LLM response.
+ * Returns clean text and parsed structured data.
+ */
+function parseStructuredBlocks(text: string): {
+ cleanText: string;
+ entities: EntityLink[];
+ dashboards: string[];
+ queryResults: any[][];
+} {
+ const entities: EntityLink[] = [];
+ const dashboards: string[] = [];
+ const queryResults: any[][] = [];
+
+ // Parse ```entities blocks
+ const entitiesRegex = /```entities\s*\n([\s\S]*?)```/g;
+ let match;
+ while ((match = entitiesRegex.exec(text)) !== null) {
+ try {
+ const parsed = JSON.parse(match[1].trim());
+ if (Array.isArray(parsed)) {
+ for (const item of parsed) {
+ if (item.type && item.url) {
+ entities.push({
+ type: item.type,
+ id: item.id || "",
+ name: item.name || item.type,
+ url: item.url,
+ });
+ }
+ }
+ }
+ } catch {
+ // Invalid JSON — skip
+ }
+ }
+
+ // Parse ```dashboard blocks
+ const dashboardRegex = /```dashboard\s*\n([\s\S]*?)```/g;
+ while ((match = dashboardRegex.exec(text)) !== null) {
+ const json = match[1].trim();
+ if (json) dashboards.push(json);
+ }
+
+ // Parse ```query-result blocks (in order — each maps to the preceding SQL block)
+ const queryResultRegex = /```query-result\s*\n([\s\S]*?)```/g;
+ while ((match = queryResultRegex.exec(text)) !== null) {
+ try {
+ const parsed = JSON.parse(match[1].trim());
+ queryResults.push(Array.isArray(parsed) ? parsed : []);
+ } catch {
+ queryResults.push([]);
+ }
+ }
+
+ const cleanText = text
+ .replace(/```entities\s*\n[\s\S]*?```/g, "")
+ .replace(/```dashboard\s*\n[\s\S]*?```/g, "")
+ .replace(/```query-result\s*\n[\s\S]*?```/g, "")
+ .trim();
+
+ return { cleanText, entities, dashboards, queryResults };
+}
+
+interface MessageBubbleProps {
+ role: "user" | "assistant";
+ content: string;
+ promptTokens?: number;
+ completionTokens?: number;
+ cost?: number;
+ queryRowsRead?: number;
+ queryExecutionTimeMs?: number;
+ createdAt?: string;
+ isStreaming?: boolean;
+ onExecuteQuery?: (
+ query: string,
+ messageId?: string
+ ) => Promise<{ data?: any[]; stats?: any; err?: string }>;
+ messageId?: string;
+}
+
+const StreamingCursor = () => (
+ <span className="inline-block w-[2px] h-[18px] bg-stone-800 dark:bg-stone-200 animate-blink align-text-bottom ml-0.5" />
+);
+
+const StreamingDots = () => (
+ <div className="flex items-center gap-1.5 mt-1">
+ <div className="h-2 w-2 rounded-full bg-stone-400 dark:bg-stone-500 animate-pulse" />
+ <div className="h-2 w-2 rounded-full bg-stone-400 dark:bg-stone-500 animate-pulse [animation-delay:150ms]" />
+ <div className="h-2 w-2 rounded-full bg-stone-400 dark:bg-stone-500 animate-pulse [animation-delay:300ms]" />
+ </div>
+);
+
+export default function MessageBubble({
+ role,
+ content,
+ promptTokens,
+ completionTokens,
+ cost,
+ queryRowsRead,
+ queryExecutionTimeMs,
+ isStreaming,
+ onExecuteQuery,
+ messageId,
+}: MessageBubbleProps) {
+ const m = getMessage();
+ const hasStats =
+ (promptTokens && promptTokens > 0) ||
+ (cost && cost > 0) ||
+ (queryRowsRead && queryRowsRead > 0);
+
+ // During streaming, pass raw content to ReactMarkdown (it handles incomplete code blocks).
+ // After streaming completes, parse and strip structured blocks for clean rendering.
+ const { cleanText, entities, dashboards, queryResults } = useMemo(() => {
+ if (isStreaming) {
+ return { cleanText: content || "", entities: [], dashboards: [], queryResults: [] };
+ }
+ return parseStructuredBlocks(content || "");
+ }, [content, isStreaming]);
+
+ // Track SQL block index to pair with query results
+ let sqlBlockIndex = 0;
+
+ if (role === "user") {
+ return (
+ <div className="flex justify-end py-4">
+ <div className="flex gap-3 max-w-[80%]">
+ <div className="flex-1 min-w-0">
+ <div className="rounded-2xl rounded-br-sm bg-stone-100 dark:bg-stone-800 px-4 py-3">
+ <p className="text-sm leading-relaxed text-stone-900 dark:text-stone-300 whitespace-pre-wrap">
+ {content}
+ </p>
+ </div>
+ </div>
+ <div className="flex-shrink-0 w-8 h-8 rounded-full bg-stone-200 dark:bg-stone-700 flex items-center justify-center">
+ <User className="h-4 w-4 text-stone-600 dark:text-stone-300" />
+ </div>
+ </div>
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex gap-3 py-4">
+ <div className="flex-shrink-0 w-8 h-8 rounded-full bg-blue-100 dark:bg-blue-950/40 flex items-center justify-center">
+ <Bot className="h-4 w-4 text-blue-600 dark:text-blue-400" />
+ </div>
+ <div className="flex-1 min-w-0">
+ {cleanText ? (
+ <div className="chat-markdown">
+ <ReactMarkdown
+ components={{
+ p({ children }) {
+ return (
+ <p className="text-sm leading-relaxed text-stone-800 dark:text-stone-100 my-2">
+ {children}
+ </p>
+ );
+ },
+ h1({ children }) {
+ return (
+ <h1 className="text-xl font-bold text-stone-900 dark:text-stone-300 mt-4 mb-2">
+ {children}
+ </h1>
+ );
+ },
+ h2({ children }) {
+ return (
+ <h2 className="text-lg font-semibold text-stone-900 dark:text-stone-300 mt-3 mb-2">
+ {children}
+ </h2>
+ );
+ },
+ h3({ children }) {
+ return (
+ <h3 className="text-base font-semibold text-stone-900 dark:text-stone-300 mt-3 mb-1">
+ {children}
+ </h3>
+ );
+ },
+ strong({ children }) {
+ return (
+ <strong className="font-semibold text-stone-900 dark:text-stone-300">
+ {children}
+ </strong>
+ );
+ },
+ em({ children }) {
+ return (
+ <em className="italic text-stone-700 dark:text-stone-200">
+ {children}
+ </em>
+ );
+ },
+ ul({ children }) {
+ return (
+ <ul className="list-disc pl-5 my-2 space-y-1 text-sm text-stone-800 dark:text-stone-100">
+ {children}
+ </ul>
+ );
+ },
+ ol({ children }) {
+ return (
+ <ol className="list-decimal pl-5 my-2 space-y-1 text-sm text-stone-800 dark:text-stone-100">
+ {children}
+ </ol>
+ );
+ },
+ li({ children }) {
+ return (
+ <li className="text-sm leading-relaxed text-stone-800 dark:text-stone-100">
+ {children}
+ </li>
+ );
+ },
+ a({ href, children }) {
+ return (
+ <a
+ href={href}
+ className="text-blue-600 dark:text-blue-400 underline hover:text-blue-700 dark:hover:text-blue-300"
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ {children}
+ </a>
+ );
+ },
+ blockquote({ children }) {
+ return (
+ <blockquote className="border-l-2 border-stone-300 dark:border-stone-600 pl-4 my-2 italic text-stone-600 dark:text-stone-300">
+ {children}
+ </blockquote>
+ );
+ },
+ hr() {
+ return <hr className="my-4 border-stone-200 dark:border-stone-700" />;
+ },
+ code({ className, children, ...props }) {
+ const match = /language-(\w+)/.exec(className || "");
+ const lang = match?.[1];
+
+ if (lang === "sql") {
+ const currentIndex = sqlBlockIndex++;
+ const preloaded = queryResults[currentIndex] || null;
+ return (
+ <SqlBlock
+ code={String(children).replace(/\n$/, "")}
+ messageId={messageId}
+ preloadedResult={preloaded}
+ onExecute={onExecuteQuery}
+ />
+ );
+ }
+
+ if (lang === "query-result") {
+ // Query results are handled via preloadedResult on SqlBlock — hide raw block
+ return null;
+ }
+
+ if (lang === "dashboard") {
+ return (
+ <DashboardImportCard
+ dashboardJson={String(children).replace(/\n$/, "")}
+ />
+ );
+ }
+
+ if (lang === "entities") {
+ // Entities blocks are parsed separately — hide from markdown
+ return null;
+ }
+
+ // Block code (has className like language-*)
+ if (className) {
+ return (
+ <pre className="rounded-lg bg-stone-100 dark:bg-stone-800 p-4 overflow-x-auto my-3">
+ <code className="text-[14px] font-mono text-stone-800 dark:text-stone-100">
+ {children}
+ </code>
+ </pre>
+ );
+ }
+
+ // Inline code
+ return (
+ <code
+ className="px-1.5 py-0.5 rounded bg-stone-100 dark:bg-stone-700 text-[14px] font-mono text-stone-900 dark:text-stone-300"
+ {...props}
+ >
+ {children}
+ </code>
+ );
+ },
+ pre({ children }) {
+ return <>{children}</>;
+ },
+ }}
+ >
+ {cleanText}
+ </ReactMarkdown>
+ </div>
+ ) : null}
+
+ {/* Dashboard import cards */}
+ {dashboards.length > 0 && !isStreaming ? (
+ <div className="mt-3">
+ {dashboards.map((json, i) => (
+ <DashboardImportCard key={i} dashboardJson={json} />
+ ))}
+ </div>
+ ) : null}
+
+ {/* Entity link cards from LLM-generated ```entities blocks */}
+ {entities.length > 0 && !isStreaming ? (
+ <div className="flex flex-col gap-1.5 mt-3 max-w-sm">
+ {entities.map((entity, i) => (
+ <EntityCard
+ key={`${entity.type}-${entity.id || i}`}
+ type={entity.type}
+ name={entity.name}
+ url={entity.url}
+ />
+ ))}
+ </div>
+ ) : null}
+
+ {/* Streaming indicator — blinking cursor when text is flowing, dots when waiting */}
+ {isStreaming ? (
+ cleanText ? <StreamingCursor /> : <StreamingDots />
+ ) : null}
+
+ {/* Stats footer */}
+ {hasStats && !isStreaming ? (
+ <div className="flex items-center gap-4 mt-2.5 text-xs text-stone-500 dark:text-stone-400">
+ {promptTokens !== undefined && promptTokens > 0 && (
+ <span className="flex items-center gap-1">
+ <Database className="h-3 w-3" />
+ {promptTokens + (completionTokens || 0)} {m.CHAT_TOKENS}
+ </span>
+ )}
+ {cost !== undefined && cost > 0 && (
+ <span className="flex items-center gap-1">
+ <Coins className="h-3 w-3" />${cost.toFixed(6)}
+ </span>
+ )}
+ {queryExecutionTimeMs !== undefined && queryExecutionTimeMs > 0 && (
+ <span className="flex items-center gap-1">
+ <Clock className="h-3 w-3" />
+ {queryExecutionTimeMs}ms
+ {queryRowsRead ? ` / ${queryRowsRead} ${m.CHAT_ROWS}` : ""}
+ </span>
+ )}
+ </div>
+ ) : null}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/message-input.tsx b/src/client/src/components/(playground)/chat/message-input.tsx
new file mode 100644
index 00000000..99632748
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/message-input.tsx
@@ -0,0 +1,132 @@
+"use client";
+
+import { useRef, useCallback, useEffect, KeyboardEvent } from "react";
+import { Button } from "@/components/ui/button";
+import { Send, Loader2, Info } from "lucide-react";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import getMessage from "@/constants/messages";
+
+import { ChatConfigInfo } from "@/types/store/chat";
+
+export type { ChatConfigInfo };
+
+interface MessageInputProps {
+ value: string;
+ onChange: (value: string) => void;
+ onSubmit: () => void;
+ isLoading: boolean;
+ disabled?: boolean;
+ configInfo?: ChatConfigInfo | null;
+}
+
+const MAX_TEXTAREA_HEIGHT = 150;
+
+export default function MessageInput({
+ value,
+ onChange,
+ onSubmit,
+ isLoading,
+ disabled = false,
+ configInfo,
+}: MessageInputProps) {
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
+ const m = getMessage();
+
+ const resizeTextarea = useCallback(() => {
+ const textarea = textareaRef.current;
+ if (!textarea) return;
+ textarea.style.height = "auto";
+ const newHeight = Math.min(textarea.scrollHeight, MAX_TEXTAREA_HEIGHT);
+ textarea.style.height = `${newHeight}px`;
+ textarea.style.overflowY =
+ textarea.scrollHeight > MAX_TEXTAREA_HEIGHT ? "auto" : "hidden";
+ }, []);
+
+ useEffect(() => {
+ resizeTextarea();
+ }, [value, resizeTextarea]);
+
+ const handleKeyDown = useCallback(
+ (e: KeyboardEvent<HTMLTextAreaElement>) => {
+ if (e.key === "Enter" && !e.shiftKey) {
+ e.preventDefault();
+ if (value.trim() && !isLoading && !disabled) {
+ onSubmit();
+ }
+ }
+ },
+ [value, isLoading, disabled, onSubmit]
+ );
+
+ return (
+ <div className="border-t border-stone-200 dark:border-stone-800 bg-stone-50 dark:bg-stone-900 p-4 shrink-0">
+ <div className="flex items-end gap-3 max-w-4xl mx-auto">
+ <div className="flex-1 relative">
+ <textarea
+ ref={textareaRef}
+ value={value}
+ onChange={(e) => onChange(e.target.value)}
+ onKeyDown={handleKeyDown}
+ placeholder={m.CHAT_ASK_QUESTION}
+ disabled={disabled || isLoading}
+ rows={1}
+ className="w-full resize-none rounded-lg border border-stone-200 dark:border-stone-700 bg-white dark:bg-stone-800 px-4 py-3 text-sm text-stone-900 dark:text-stone-100 placeholder:text-stone-400 dark:placeholder:text-stone-500 focus:outline-none focus:ring-2 focus:ring-stone-400 dark:focus:ring-stone-600 focus:border-transparent disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
+ style={{
+ maxHeight: `${MAX_TEXTAREA_HEIGHT}px`,
+ overflowY: "hidden",
+ }}
+ />
+ </div>
+ <Button
+ onClick={onSubmit}
+ disabled={!value.trim() || isLoading || disabled}
+ size="icon"
+ className="h-[46px] w-[46px] shrink-0 rounded-lg"
+ >
+ {isLoading ? (
+ <Loader2 className="h-4 w-4 animate-spin" />
+ ) : (
+ <Send className="h-4 w-4" />
+ )}
+ </Button>
+ </div>
+ <div className="flex items-center justify-center gap-2 mt-2 max-w-4xl mx-auto">
+ <p className="text-[11px] text-stone-400 dark:text-stone-500">
+ {m.CHAT_ENTER_TO_SEND}
+ </p>
+ {configInfo?.providerName && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="inline-flex items-center gap-1 text-[11px] text-stone-400 dark:text-stone-500 cursor-help">
+ <Info className="h-3 w-3" />
+ {configInfo.providerName} / {configInfo.modelName || configInfo.modelId}
+ </span>
+ </TooltipTrigger>
+ <TooltipContent side="top" className="max-w-xs text-xs space-y-1.5 p-3">
+ <p className="font-medium">Active Configuration</p>
+ <p><span className="text-stone-400">Provider:</span> {configInfo.providerName}</p>
+ <p><span className="text-stone-400">Model:</span> {configInfo.modelName || configInfo.modelId}</p>
+ {configInfo.inputPricePerMToken !== undefined && (
+ <>
+ <hr className="border-stone-200 dark:border-stone-700" />
+ <p className="font-medium">Pricing</p>
+ <p><span className="text-stone-400">Input:</span> ${configInfo.inputPricePerMToken}/M tokens</p>
+ <p><span className="text-stone-400">Output:</span> ${configInfo.outputPricePerMToken}/M tokens</p>
+ {configInfo.contextWindow && (
+ <p><span className="text-stone-400">Context:</span> {configInfo.contextWindow.toLocaleString()} tokens</p>
+ )}
+ <hr className="border-stone-200 dark:border-stone-700" />
+ <p className="text-stone-400">cost = (input_tokens / 1M) × ${configInfo.inputPricePerMToken} + (output_tokens / 1M) × ${configInfo.outputPricePerMToken}</p>
+ </>
+ )}
+ </TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/message-list.tsx b/src/client/src/components/(playground)/chat/message-list.tsx
new file mode 100644
index 00000000..65f13d60
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/message-list.tsx
@@ -0,0 +1,85 @@
+"use client";
+
+import { useRef, useEffect, useCallback } from "react";
+import MessageBubble from "./message-bubble";
+
+interface Message {
+ id?: string;
+ role: "user" | "assistant";
+ content: string;
+ promptTokens?: number;
+ completionTokens?: number;
+ cost?: number;
+ queryRowsRead?: number;
+ queryExecutionTimeMs?: number;
+ createdAt?: string;
+}
+
+interface MessageListProps {
+ messages: Message[];
+ isStreaming: boolean;
+ onExecuteQuery: (
+ query: string,
+ messageId?: string
+ ) => Promise<{ data?: any[]; stats?: any; err?: string }>;
+}
+
+export default function MessageList({
+ messages,
+ isStreaming,
+ onExecuteQuery,
+}: MessageListProps) {
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const isNearBottomRef = useRef(true);
+
+ const checkNearBottom = useCallback(() => {
+ const el = scrollRef.current;
+ if (!el) return;
+ const threshold = 100;
+ isNearBottomRef.current =
+ el.scrollHeight - el.scrollTop - el.clientHeight < threshold;
+ }, []);
+
+ useEffect(() => {
+ if (isNearBottomRef.current) {
+ const el = scrollRef.current;
+ if (el) {
+ el.scrollTop = el.scrollHeight;
+ }
+ }
+ }, [messages, isStreaming]);
+
+ return (
+ <div
+ ref={scrollRef}
+ onScroll={checkNearBottom}
+ className="flex-1 overflow-y-auto"
+ >
+ <div className="max-w-4xl mx-auto px-4 py-6 space-y-1">
+ {messages.map((msg, i) => {
+ const isLastAssistant =
+ isStreaming &&
+ msg.role === "assistant" &&
+ i === messages.length - 1;
+
+ return (
+ <MessageBubble
+ key={msg.id || `msg-${i}`}
+ role={msg.role}
+ content={msg.content}
+ promptTokens={msg.promptTokens}
+ completionTokens={msg.completionTokens}
+ cost={msg.cost}
+ queryRowsRead={msg.queryRowsRead}
+ queryExecutionTimeMs={msg.queryExecutionTimeMs}
+ createdAt={msg.createdAt}
+ isStreaming={isLastAssistant}
+ onExecuteQuery={onExecuteQuery}
+ messageId={msg.id}
+ />
+ );
+ })}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/result-display.tsx b/src/client/src/components/(playground)/chat/result-display.tsx
new file mode 100644
index 00000000..04e830ae
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/result-display.tsx
@@ -0,0 +1,361 @@
+"use client";
+
+import { useState, useMemo } from "react";
+import { Button } from "@/components/ui/button";
+import { Save, Table, BarChart3, LineChart as LineChartIcon, PieChart as PieChartIcon } from "lucide-react";
+import getMessage from "@/constants/messages";
+import SaveWidgetDialog from "./save-widget-dialog";
+import {
+ ResponsiveContainer,
+ LineChart,
+ Line,
+ BarChart,
+ Bar,
+ PieChart,
+ Pie,
+ Cell,
+ XAxis,
+ YAxis,
+ CartesianGrid,
+ Tooltip,
+ Legend,
+} from "recharts";
+
+type VizType = "TABLE" | "BAR_CHART" | "LINE_CHART" | "PIE_CHART" | "AREA_CHART" | "STAT_CARD";
+
+interface ResultDisplayProps {
+ data: any[];
+ stats?: {
+ rowsRead?: number;
+ executionTimeMs?: number;
+ bytesRead?: number;
+ };
+ query: string;
+}
+
+const CHART_COLORS = [
+ "#F36C06", "#3b82f6", "#10b981", "#8b5cf6",
+ "#f59e0b", "#ef4444", "#06b6d4", "#ec4899",
+];
+
+function formatNumber(val: unknown): string {
+ if (val === null || val === undefined) return "";
+ const num = Number(val);
+ if (isNaN(num)) return String(val);
+ if (Number.isInteger(num)) return num.toLocaleString();
+ // Preserve up to 10 decimal places, strip trailing zeros
+ return parseFloat(num.toFixed(10)).toString();
+}
+
+function detectVizType(data: any[]): VizType {
+ if (!data || data.length === 0) return "TABLE";
+
+ const columns = Object.keys(data[0]);
+ const numericCols = columns.filter((col) => {
+ const val = data[0][col];
+ return typeof val === "number" || (!isNaN(Number(val)) && val !== "" && val !== null);
+ });
+ const stringCols = columns.filter((col) => !numericCols.includes(col));
+
+ if (data.length === 1 && numericCols.length >= 1 && numericCols.length <= 2) {
+ return "STAT_CARD";
+ }
+
+ const hasTimestamp = columns.some((col) => {
+ const lower = col.toLowerCase();
+ return (
+ lower.includes("time") || lower.includes("hour") ||
+ lower.includes("day") || lower.includes("date") ||
+ lower.includes("minute")
+ );
+ });
+
+ if (hasTimestamp && numericCols.length >= 1 && data.length > 1) {
+ return "LINE_CHART";
+ }
+
+ if (columns.length === 2 && stringCols.length === 1 && numericCols.length === 1 && data.length > 1) {
+ return "BAR_CHART";
+ }
+
+ return "TABLE";
+}
+
+function getColumnTypes(data: any[]) {
+ if (!data || data.length === 0) return { numericCols: [] as string[], labelCols: [] as string[] };
+ const columns = Object.keys(data[0]);
+ const numericCols = columns.filter((col) => {
+ const val = data[0][col];
+ return typeof val === "number" || (!isNaN(Number(val)) && val !== "" && val !== null);
+ });
+ const labelCols = columns.filter((col) => !numericCols.includes(col));
+ return { numericCols, labelCols };
+}
+
+const VIZ_OPTIONS: { type: VizType; icon: React.ElementType; label: string }[] = [
+ { type: "TABLE", icon: Table, label: "Table" },
+ { type: "BAR_CHART", icon: BarChart3, label: "Bar" },
+ { type: "LINE_CHART", icon: LineChartIcon, label: "Line" },
+ { type: "PIE_CHART", icon: PieChartIcon, label: "Pie" },
+];
+
+export default function ResultDisplay({ data, stats, query }: ResultDisplayProps) {
+ const m = getMessage();
+ const detectedType = useMemo(() => detectVizType(data), [data]);
+ const [vizType, setVizType] = useState<VizType>(detectedType);
+ const [showSaveDialog, setShowSaveDialog] = useState(false);
+
+ if (!data || data.length === 0) {
+ return (
+ <div className="px-4 py-6 text-center text-sm text-stone-400 dark:text-stone-500">
+ {m.CHAT_NO_DATA_RETURNED}
+ </div>
+ );
+ }
+
+ const columns = Object.keys(data[0]);
+
+ return (
+ <div className="bg-white dark:bg-stone-900">
+ <div className="flex items-center justify-between px-3 py-2 border-b border-stone-200 dark:border-stone-700">
+ <div className="flex items-center gap-1">
+ {VIZ_OPTIONS.map(({ type, icon: Icon, label }) => (
+ <Button
+ key={type}
+ variant={vizType === type ? "default" : "ghost"}
+ size="sm"
+ className="h-7 px-2 text-xs"
+ onClick={() => setVizType(type)}
+ >
+ <Icon className="h-3 w-3 mr-1" />
+ {label}
+ </Button>
+ ))}
+ </div>
+ <div className="flex items-center gap-3">
+ {stats && (
+ <span className="text-xs text-stone-400 dark:text-stone-500">
+ {data.length} {m.CHAT_ROWS}
+ {stats.executionTimeMs ? ` in ${stats.executionTimeMs}ms` : ""}
+ </span>
+ )}
+ <Button
+ variant="outline"
+ size="sm"
+ className="h-7 px-2 text-xs border-stone-200 dark:border-stone-700"
+ onClick={() => setShowSaveDialog(true)}
+ >
+ <Save className="h-3 w-3 mr-1" />
+ {m.CHAT_SAVE_AS_WIDGET}
+ </Button>
+ </div>
+ </div>
+
+ <div className="p-4 max-h-[400px] overflow-auto">
+ {vizType === "STAT_CARD" && <StatCardView data={data} />}
+ {vizType === "TABLE" && <TableView data={data} columns={columns} />}
+ {vizType === "BAR_CHART" && <RechartsBarChart data={data} />}
+ {vizType === "LINE_CHART" && <RechartsLineChart data={data} />}
+ {vizType === "PIE_CHART" && <RechartsPieChart data={data} />}
+ </div>
+
+ {showSaveDialog && (
+ <SaveWidgetDialog
+ open={showSaveDialog}
+ onClose={() => setShowSaveDialog(false)}
+ query={query}
+ suggestedType={vizType}
+ data={data}
+ />
+ )}
+ </div>
+ );
+}
+
+function StatCardView({ data }: { data: any[] }) {
+ const row = data[0];
+ return (
+ <div className="flex items-center gap-6 justify-center py-4">
+ {Object.entries(row).map(([key, value]) => (
+ <div key={key} className="text-center">
+ <p className="text-3xl font-bold text-stone-900 dark:text-stone-100">
+ {formatNumber(value)}
+ </p>
+ <p className="text-sm text-stone-500 dark:text-stone-400 mt-1">
+ {key.replace(/_/g, " ")}
+ </p>
+ </div>
+ ))}
+ </div>
+ );
+}
+
+function TableView({ data, columns }: { data: any[]; columns: string[] }) {
+ return (
+ <table className="w-full text-sm">
+ <thead>
+ <tr className="border-b border-stone-200 dark:border-stone-700">
+ {columns.map((col) => (
+ <th key={col} className="px-3 py-2 text-left text-xs font-medium text-stone-500 dark:text-stone-400 uppercase tracking-wider">
+ {col.replace(/_/g, " ")}
+ </th>
+ ))}
+ </tr>
+ </thead>
+ <tbody>
+ {data.map((row, i) => (
+ <tr key={i} className="border-b border-stone-100 dark:border-stone-800 hover:bg-stone-50 dark:hover:bg-stone-800/50">
+ {columns.map((col) => (
+ <td key={col} className="px-3 py-2 text-stone-700 dark:text-stone-300 font-mono text-xs">
+ {typeof row[col] === "object"
+ ? JSON.stringify(row[col])
+ : formatNumber(row[col])}
+ </td>
+ ))}
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ );
+}
+
+function RechartsBarChart({ data }: { data: any[] }) {
+ const { numericCols, labelCols } = getColumnTypes(data);
+ const xKey = labelCols[0] || Object.keys(data[0])[0];
+ const yKeys = numericCols.length > 0 ? numericCols : [Object.keys(data[0])[1]];
+
+ // Convert string numbers to actual numbers for Recharts
+ const chartData = data.map((row) => {
+ const newRow: Record<string, any> = { ...row };
+ yKeys.forEach((k) => {
+ newRow[k] = Number(row[k]) || 0;
+ });
+ return newRow;
+ });
+
+ return (
+ <div className="h-[300px] w-full">
+ <ResponsiveContainer width="100%" height="100%">
+ <BarChart data={chartData} margin={{ top: 5, right: 20, bottom: 5, left: 0 }}>
+ <CartesianGrid strokeDasharray="3 3" className="stroke-stone-200 dark:stroke-stone-700" />
+ <XAxis
+ dataKey={xKey}
+ tick={{ fontSize: 11 }}
+ className="text-stone-500 dark:text-stone-400"
+ />
+ <YAxis
+ tick={{ fontSize: 11 }}
+ className="text-stone-500 dark:text-stone-400"
+ tickFormatter={(v) => formatNumber(v)}
+ />
+ <Tooltip
+ contentStyle={{
+ backgroundColor: "var(--tooltip-bg, #fff)",
+ border: "1px solid var(--tooltip-border, #e7e5e4)",
+ borderRadius: "8px",
+ fontSize: "12px",
+ }}
+ formatter={(value: any) => formatNumber(value)}
+ />
+ {yKeys.length > 1 && <Legend />}
+ {yKeys.map((key, i) => (
+ <Bar key={key} dataKey={key} fill={CHART_COLORS[i % CHART_COLORS.length]} radius={[4, 4, 0, 0]} />
+ ))}
+ </BarChart>
+ </ResponsiveContainer>
+ </div>
+ );
+}
+
+function RechartsLineChart({ data }: { data: any[] }) {
+ const { numericCols, labelCols } = getColumnTypes(data);
+ const xKey = labelCols[0] || Object.keys(data[0])[0];
+ const yKeys = numericCols.length > 0 ? numericCols : [Object.keys(data[0])[1]];
+
+ const chartData = data.map((row) => {
+ const newRow: Record<string, any> = { ...row };
+ yKeys.forEach((k) => {
+ newRow[k] = Number(row[k]) || 0;
+ });
+ return newRow;
+ });
+
+ return (
+ <div className="h-[300px] w-full">
+ <ResponsiveContainer width="100%" height="100%">
+ <LineChart data={chartData} margin={{ top: 5, right: 20, bottom: 5, left: 0 }}>
+ <CartesianGrid strokeDasharray="3 3" className="stroke-stone-200 dark:stroke-stone-700" />
+ <XAxis
+ dataKey={xKey}
+ tick={{ fontSize: 11 }}
+ className="text-stone-500 dark:text-stone-400"
+ />
+ <YAxis
+ tick={{ fontSize: 11 }}
+ className="text-stone-500 dark:text-stone-400"
+ tickFormatter={(v) => formatNumber(v)}
+ />
+ <Tooltip
+ contentStyle={{
+ backgroundColor: "var(--tooltip-bg, #fff)",
+ border: "1px solid var(--tooltip-border, #e7e5e4)",
+ borderRadius: "8px",
+ fontSize: "12px",
+ }}
+ formatter={(value: any) => formatNumber(value)}
+ />
+ {yKeys.length > 1 && <Legend />}
+ {yKeys.map((key, i) => (
+ <Line
+ key={key}
+ type="monotone"
+ dataKey={key}
+ stroke={CHART_COLORS[i % CHART_COLORS.length]}
+ strokeWidth={2}
+ dot={{ r: 3 }}
+ activeDot={{ r: 5 }}
+ />
+ ))}
+ </LineChart>
+ </ResponsiveContainer>
+ </div>
+ );
+}
+
+function RechartsPieChart({ data }: { data: any[] }) {
+ const { numericCols, labelCols } = getColumnTypes(data);
+ const nameKey = labelCols[0] || Object.keys(data[0])[0];
+ const valueKey = numericCols[0] || Object.keys(data[0])[1];
+
+ const chartData = data.map((row) => ({
+ name: String(row[nameKey] ?? ""),
+ value: Number(row[valueKey]) || 0,
+ }));
+
+ return (
+ <div className="h-[300px] w-full">
+ <ResponsiveContainer width="100%" height="100%">
+ <PieChart>
+ <Pie
+ data={chartData}
+ dataKey="value"
+ nameKey="name"
+ cx="50%"
+ cy="50%"
+ outerRadius={100}
+ label={({ name, percent }) =>
+ `${name} (${(percent * 100).toFixed(1)}%)`
+ }
+ labelLine={{ strokeWidth: 1 }}
+ >
+ {chartData.map((_, i) => (
+ <Cell key={i} fill={CHART_COLORS[i % CHART_COLORS.length]} />
+ ))}
+ </Pie>
+ <Tooltip formatter={(value: any) => formatNumber(value)} />
+ <Legend />
+ </PieChart>
+ </ResponsiveContainer>
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/save-widget-dialog.tsx b/src/client/src/components/(playground)/chat/save-widget-dialog.tsx
new file mode 100644
index 00000000..f4f66cc0
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/save-widget-dialog.tsx
@@ -0,0 +1,195 @@
+"use client";
+
+import { useState, useEffect } from "react";
+import {
+ Dialog,
+ DialogContent,
+ DialogHeader,
+ DialogTitle,
+ DialogFooter,
+} from "@/components/ui/dialog";
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { toast } from "sonner";
+import { Loader2 } from "lucide-react";
+import getMessage from "@/constants/messages";
+
+interface SaveWidgetDialogProps {
+ open: boolean;
+ onClose: () => void;
+ query: string;
+ suggestedType: string;
+ data: any[];
+}
+
+const WIDGET_TYPES = [
+ { value: "STAT_CARD", label: "Stat Card" },
+ { value: "BAR_CHART", label: "Bar Chart" },
+ { value: "LINE_CHART", label: "Line Chart" },
+ { value: "PIE_CHART", label: "Pie Chart" },
+ { value: "AREA_CHART", label: "Area Chart" },
+ { value: "TABLE", label: "Table" },
+];
+
+export default function SaveWidgetDialog({
+ open,
+ onClose,
+ query,
+ suggestedType,
+ data,
+}: SaveWidgetDialogProps) {
+ const m = getMessage();
+ const [title, setTitle] = useState("");
+ const [widgetType, setWidgetType] = useState(suggestedType);
+ const [boards, setBoards] = useState<any[]>([]);
+ const [selectedBoard, setSelectedBoard] = useState<string>("");
+ const [saving, setSaving] = useState(false);
+
+ useEffect(() => {
+ if (open) {
+ fetch("/api/manage-dashboard/board?home=false")
+ .then((res) => res.json())
+ .then((res) => {
+ if (res.data) setBoards(res.data);
+ })
+ .catch(() => {});
+ }
+ }, [open]);
+
+ const handleSave = async () => {
+ if (!title.trim()) {
+ toast.error(m.CHAT_WIDGET_ENTER_TITLE);
+ return;
+ }
+
+ setSaving(true);
+ try {
+ const columns = data.length > 0 ? Object.keys(data[0]) : [];
+ const numericCol = columns.find((col) => {
+ const val = data[0]?.[col];
+ return typeof val === "number" || (!isNaN(Number(val)) && val !== "");
+ });
+ const labelCol = columns.find((col) => col !== numericCol);
+
+ const properties: Record<string, any> = {
+ color: "#F36C06",
+ };
+
+ if (widgetType === "STAT_CARD" && numericCol) {
+ properties.value = `0.${numericCol}`;
+ } else if (
+ ["BAR_CHART", "LINE_CHART", "AREA_CHART"].includes(widgetType)
+ ) {
+ if (labelCol) properties.xAxis = labelCol;
+ if (numericCol) properties.yAxis = numericCol;
+ } else if (widgetType === "PIE_CHART") {
+ if (labelCol) properties.labelPath = labelCol;
+ if (numericCol) properties.valuePath = numericCol;
+ }
+
+ const res = await fetch("/api/chat/message/save-widget", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ title: title.trim(),
+ description: "",
+ type: widgetType,
+ query,
+ properties,
+ boardId: selectedBoard && selectedBoard !== "none" ? selectedBoard : undefined,
+ }),
+ });
+
+ const result = await res.json();
+
+ if (!res.ok || result.err) {
+ throw new Error(result.err || m.CHAT_WIDGET_SAVE_FAILED);
+ }
+
+ toast.success(m.CHAT_WIDGET_SAVED);
+ onClose();
+ } catch (e: any) {
+ toast.error(e.message || m.CHAT_WIDGET_SAVE_FAILED);
+ } finally {
+ setSaving(false);
+ }
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={(o) => !o && onClose()}>
+ <DialogContent className="sm:max-w-md">
+ <DialogHeader>
+ <DialogTitle>{m.CHAT_SAVE_WIDGET_TITLE}</DialogTitle>
+ </DialogHeader>
+
+ <div className="space-y-4 py-4">
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_WIDGET_TITLE_LABEL}
+ </label>
+ <Input
+ value={title}
+ onChange={(e) => setTitle(e.target.value)}
+ placeholder={m.CHAT_WIDGET_TITLE_PLACEHOLDER}
+ className="bg-white dark:bg-stone-900"
+ />
+ </div>
+
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_WIDGET_TYPE_LABEL}
+ </label>
+ <Select value={widgetType} onValueChange={setWidgetType}>
+ <SelectTrigger>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {WIDGET_TYPES.map((wt) => (
+ <SelectItem key={wt.value} value={wt.value}>
+ {wt.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+
+ <div className="space-y-2">
+ <label className="text-sm font-medium text-stone-700 dark:text-stone-300">
+ {m.CHAT_WIDGET_DASHBOARD_LABEL}
+ </label>
+ <Select value={selectedBoard} onValueChange={setSelectedBoard}>
+ <SelectTrigger>
+ <SelectValue placeholder={m.CHAT_WIDGET_DASHBOARD_PLACEHOLDER} />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="none">-</SelectItem>
+ {boards.map((board: any) => (
+ <SelectItem key={board.id} value={board.id}>
+ {board.title}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ </div>
+
+ <DialogFooter>
+ <Button variant="outline" onClick={onClose}>
+ {m.CANCEL}
+ </Button>
+ <Button onClick={handleSave} disabled={saving}>
+ {saving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
+ {m.CHAT_SAVE_AS_WIDGET}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/client/src/components/(playground)/chat/sql-block.tsx b/src/client/src/components/(playground)/chat/sql-block.tsx
new file mode 100644
index 00000000..beca227a
--- /dev/null
+++ b/src/client/src/components/(playground)/chat/sql-block.tsx
@@ -0,0 +1,163 @@
+"use client";
+
+import { useState, useCallback } from "react";
+import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
+import {
+ oneDark,
+ oneLight,
+} from "react-syntax-highlighter/dist/cjs/styles/prism";
+import { Button } from "@/components/ui/button";
+import {
+ Accordion,
+ AccordionContent,
+ AccordionItem,
+ AccordionTrigger,
+} from "@/components/ui/accordion";
+import { Copy, Play, Check, Loader2, Code } from "lucide-react";
+import { useTheme } from "next-themes";
+import getMessage from "@/constants/messages";
+import ResultDisplay from "./result-display";
+
+interface SqlBlockProps {
+ code: string;
+ messageId?: string;
+ /** Pre-loaded query results from the backend */
+ preloadedResult?: any[] | null;
+ onExecute?: (
+ query: string,
+ messageId?: string
+ ) => Promise<{ data?: any[]; stats?: any; err?: string }>;
+}
+
+export default function SqlBlock({
+ code,
+ messageId,
+ preloadedResult,
+ onExecute,
+}: SqlBlockProps) {
+ const [copied, setCopied] = useState(false);
+ const [executing, setExecuting] = useState(false);
+ const [result, setResult] = useState<any[] | null>(preloadedResult || null);
+ const [stats, setStats] = useState<any>(null);
+ const [error, setError] = useState<string | null>(null);
+ const { resolvedTheme } = useTheme();
+
+ const handleCopy = useCallback(async () => {
+ await navigator.clipboard.writeText(code);
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ }, [code]);
+
+ const handleExecute = useCallback(async () => {
+ if (!onExecute) return;
+ setExecuting(true);
+ setError(null);
+ setResult(null);
+
+ try {
+ const res = await onExecute(code, messageId);
+ if (res.err) {
+ setError(typeof res.err === "string" ? res.err : JSON.stringify(res.err));
+ } else {
+ setResult(res.data || []);
+ setStats(res.stats || null);
+ }
+ } catch (e: any) {
+ setError(e.message || getMessage().CHAT_QUERY_EXECUTION_FAILED);
+ } finally {
+ setExecuting(false);
+ }
+ }, [code, messageId, onExecute]);
+
+ return (
+ <div className="my-3 rounded-lg border border-stone-200 dark:border-stone-700 overflow-hidden bg-white dark:bg-stone-900">
+ {/* SQL Query in Accordion — collapsed by default */}
+ <Accordion type="single" collapsible>
+ <AccordionItem value="sql" className="border-b-0">
+ <div className="flex items-center justify-between px-3 py-1.5 bg-stone-50 dark:bg-stone-800/50">
+ <AccordionTrigger className="py-1.5 hover:no-underline gap-2 text-xs font-medium text-stone-500 dark:text-stone-400">
+ <div className="flex items-center gap-1.5">
+ <Code className="h-3.5 w-3.5" />
+ {getMessage().CHAT_SQL_LABEL}
+ </div>
+ </AccordionTrigger>
+ <div className="flex items-center gap-1.5">
+ <Button
+ variant="ghost"
+ size="sm"
+ className="h-6 px-2 text-xs text-stone-500 dark:text-stone-400"
+ onClick={handleCopy}
+ >
+ {copied ? (
+ <Check className="h-3 w-3 mr-1" />
+ ) : (
+ <Copy className="h-3 w-3 mr-1" />
+ )}
+ {copied ? getMessage().CHAT_COPIED : getMessage().CHAT_COPY}
+ </Button>
+ {onExecute && (
+ <Button
+ variant="default"
+ size="sm"
+ className="h-6 px-2 text-xs"
+ onClick={handleExecute}
+ disabled={executing}
+ >
+ {executing ? (
+ <Loader2 className="h-3 w-3 mr-1 animate-spin" />
+ ) : (
+ <Play className="h-3 w-3 mr-1" />
+ )}
+ {executing ? getMessage().CHAT_RUNNING : getMessage().CHAT_EXECUTE}
+ </Button>
+ )}
+ </div>
+ </div>
+ <AccordionContent className="pb-0" parentClassName="border-t border-stone-200 dark:border-stone-700">
+ <SyntaxHighlighter
+ language="sql"
+ style={resolvedTheme === "dark" ? oneDark : oneLight}
+ customStyle={{
+ margin: 0,
+ borderRadius: 0,
+ fontSize: "13px",
+ padding: "12px 16px",
+ background: "transparent",
+ }}
+ >
+ {code}
+ </SyntaxHighlighter>
+ </AccordionContent>
+ </AccordionItem>
+ </Accordion>
+
+ {/* Error */}
+ {error && (
+ <div className="px-4 py-3 bg-red-50 dark:bg-red-950/30 border-t border-red-200 dark:border-red-800">
+ <p className="text-sm text-red-700 dark:text-red-300 font-mono">
+ {error}
+ </p>
+ </div>
+ )}
+
+ {/* Loading state */}
+ {executing && !result && (
+ <div className="flex items-center justify-center gap-2 px-4 py-6 border-t border-stone-200 dark:border-stone-700">
+ <Loader2 className="h-4 w-4 animate-spin text-stone-400" />
+ <span className="text-sm text-stone-400 dark:text-stone-500">{getMessage().CHAT_RUNNING}</span>
+ </div>
+ )}
+
+ {/* Results */}
+ {result && (
+ <div className="border-t border-stone-200 dark:border-stone-700">
+ <ResultDisplay
+ data={result}
+ stats={stats}
+ query={code}
+ />
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/openground/model-editor-panel.tsx b/src/client/src/components/(playground)/openground/model-editor-panel.tsx
index d1891f92..9856a8c5 100644
--- a/src/client/src/components/(playground)/openground/model-editor-panel.tsx
+++ b/src/client/src/components/(playground)/openground/model-editor-panel.tsx
@@ -5,6 +5,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Trash2Icon } from "lucide-react";
import getMessage from "@/constants/messages";
@@ -13,10 +14,19 @@ import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
import { toast } from "sonner";
interface CustomModel extends ModelMetadata {
- id: string; // UUID from database
+ id: string;
+ customId?: string; // UUID from database
model_id: string; // Model identifier like "gpt-4o"
+ modelType?: string;
}
+const MODEL_TYPES = [
+ { value: "chat", label: "Chat" },
+ { value: "embeddings", label: "Embeddings" },
+ { value: "images", label: "Images" },
+ { value: "audio", label: "Audio" },
+];
+
interface ModelEditorPanelProps {
model: ModelMetadata | null;
provider: string | null;
@@ -44,6 +54,7 @@ export default function ModelEditorPanel({
id: "", // UUID (only for existing custom models)
model_id: "", // Model identifier
displayName: "",
+ modelType: "chat",
contextWindow: 4096,
inputPricePerMToken: 0,
outputPricePerMToken: 0,
@@ -52,23 +63,27 @@ export default function ModelEditorPanel({
useEffect(() => {
if (model && isCustomModel) {
- // Only allow editing custom models
- const customModel = model as CustomModel;
+ const customModel = model as any;
+ // The API returns `customId` as the UUID and `id` as the model_id.
+ // Map them correctly: UUID goes to `id`, model identifier goes to `model_id`.
+ const uuid = customModel.customId || customModel.id || "";
+ const modelIdentifier = customModel.model_id || customModel.id || "";
setFormData({
- id: customModel.id || "", // UUID for existing custom models
- model_id: customModel.model_id,
+ id: uuid,
+ model_id: modelIdentifier,
displayName: model.displayName,
+ modelType: customModel.modelType || "chat",
contextWindow: model.contextWindow,
inputPricePerMToken: model.inputPricePerMToken,
outputPricePerMToken: model.outputPricePerMToken,
capabilities: model.capabilities || [],
});
} else if (isAddingNew) {
- // Reset form for new model
setFormData({
id: "",
model_id: "",
displayName: "",
+ modelType: "chat",
contextWindow: 4096,
inputPricePerMToken: 0,
outputPricePerMToken: 0,
@@ -87,15 +102,16 @@ export default function ModelEditorPanel({
const payload = {
provider: provider,
model: {
+ id: formData.model_id,
model_id: formData.model_id,
displayName: formData.displayName,
+ modelType: formData.modelType || "chat",
contextWindow: formData.contextWindow || 4096,
inputPricePerMToken: formData.inputPricePerMToken || 0,
outputPricePerMToken: formData.outputPricePerMToken || 0,
capabilities: formData.capabilities || [],
},
- // Send UUID id for updates of existing custom models, undefined for new models
- id: formData.id || undefined,
+ customId: formData.id || undefined,
};
fireSaveRequest({
@@ -117,12 +133,15 @@ export default function ModelEditorPanel({
};
const handleDelete = () => {
- const id = (model as CustomModel)?.id;
- if (!id) return;
+ if (!formData.model_id || !provider) return;
+
+ const params = new URLSearchParams();
+ params.set("model_id", formData.model_id);
+ params.set("provider", provider);
fireDeleteRequest({
requestType: "DELETE",
- url: `/api/openground/models?id=${id}`,
+ url: `/api/openground/models?${params.toString()}`,
successCb: () => {
toast.success(getMessage().OPENGROUND_MODEL_DELETED_SUCCESS, {
id: "model-deleted",
@@ -175,6 +194,29 @@ export default function ModelEditorPanel({
/>
</div>
+ {/* Model Type */}
+ <div className="space-y-2">
+ <Label>Model Type</Label>
+ <Select
+ value={formData.modelType || "chat"}
+ onValueChange={(val) => setFormData({ ...formData, modelType: val })}
+ >
+ <SelectTrigger>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {MODEL_TYPES.map((mt) => (
+ <SelectItem key={mt.value} value={mt.value}>
+ {mt.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <p className="text-xs text-stone-500 dark:text-stone-400">
+ Determines pricing format when exported (chat, embeddings, images, audio)
+ </p>
+ </div>
+
{/* Context Window */}
<div className="space-y-2">
<Label htmlFor="context-window">{getMessage().OPENGROUND_CONTEXT_WINDOW}</Label>
diff --git a/src/client/src/components/(playground)/openground/model-list-sidebar.tsx b/src/client/src/components/(playground)/openground/model-list-sidebar.tsx
index 884fa0a7..d1d1341f 100644
--- a/src/client/src/components/(playground)/openground/model-list-sidebar.tsx
+++ b/src/client/src/components/(playground)/openground/model-list-sidebar.tsx
@@ -4,15 +4,17 @@ import { useState, useMemo } from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
-import { SearchIcon, PlusIcon, ChevronDownIcon, ChevronRightIcon } from "lucide-react";
+import { SearchIcon, PlusIcon, ChevronDownIcon, ChevronRightIcon, PencilIcon } from "lucide-react";
import getMessage from "@/constants/messages";
import { ProviderMetadata, ModelMetadata } from "@/types/openground";
import { cn } from "@/lib/utils";
interface CustomModel extends ModelMetadata {
- id: string; // UUID from database
+ id: string;
+ customId?: string; // UUID from database
model_id: string; // Model identifier like "gpt-4o"
provider?: string;
+ modelType?: string;
}
interface ModelListSidebarProps {
@@ -72,13 +74,14 @@ export default function ModelListSidebar({
if (selectedProvider !== provider) return false;
if (!selectedModel) return false;
- // Only custom models can be selected (static models are display-only)
if (!isCustom || !selectedIsCustom) return false;
- // Check id (UUID) to match custom models
- const customModel = model as CustomModel;
- const selectedCustomModel = selectedModel as CustomModel;
- return customModel.id === selectedCustomModel.id;
+ // Match by customId (UUID) or model_id
+ const customModel = model as any;
+ const selectedCustomModel = selectedModel as any;
+ const customModelKey = customModel.customId || customModel.id;
+ const selectedKey = selectedCustomModel.customId || selectedCustomModel.id;
+ return customModelKey === selectedKey;
};
return (
@@ -111,7 +114,10 @@ export default function ModelListSidebar({
{filteredProviders.map((provider) => {
const isExpanded = expandedProviders.has(provider.providerId);
const providerCustomModels = customModels[provider.providerId] || [];
- const totalModels = provider.supportedModels.length + providerCustomModels.length;
+ // Deduplicate: exclude custom model IDs from static count
+ const customModelIds = new Set(providerCustomModels.map((cm) => cm.model_id || cm.id));
+ const staticModels = provider.supportedModels.filter((m) => !customModelIds.has(m.id));
+ const totalModels = staticModels.length + providerCustomModels.length;
return (
<div key={provider.providerId} className="mb-2">
@@ -150,7 +156,7 @@ export default function ModelListSidebar({
{isExpanded && (
<div className="ml-4 mt-1 space-y-1">
{/* Static Models - Display only, not editable */}
- {provider.supportedModels.map((model) => (
+ {staticModels.map((model) => (
<div
key={model.id}
className="w-full text-left p-2 rounded-md border-l-2 border-transparent opacity-75"
@@ -178,15 +184,18 @@ export default function ModelListSidebar({
key={model.id}
onClick={() => onSelectModel(model, provider.providerId, true)}
className={cn(
- "w-full text-left p-2 rounded-md transition-colors border-l-2",
+ "w-full text-left p-2 rounded-md transition-colors border-l-2 group/model",
isModelSelected(model, provider.providerId, true)
? "bg-primary/10 dark:bg-primary/20 border-primary text-stone-900 dark:text-stone-100"
: "border-transparent hover:bg-stone-100 dark:hover:bg-stone-800 text-stone-700 dark:text-stone-300"
)}
>
- <div className="flex items-center gap-2">
- <div className="text-sm font-medium">{model.displayName}</div>
- <Badge className="text-xs h-4">Custom</Badge>
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <div className="text-sm font-medium">{model.displayName}</div>
+ <Badge className="text-xs h-4">Custom</Badge>
+ </div>
+ <PencilIcon className="h-3 w-3 text-stone-400 opacity-0 group-hover/model:opacity-100 transition-opacity" />
</div>
<div className="flex items-center gap-2 mt-1">
<Badge variant="outline" className="text-xs">
diff --git a/src/client/src/components/svg/otter.tsx b/src/client/src/components/svg/otter.tsx
new file mode 100644
index 00000000..1d376a9d
--- /dev/null
+++ b/src/client/src/components/svg/otter.tsx
@@ -0,0 +1,9 @@
+export default function Otter({ className = "" }: { className?: string }) {
+ return (
+ <svg xmlns="http://www.w3.org/2000/svg" width={40} height={40} viewBox="427 171 445 445" className={`${className} external-icon-svg`}>
+ <g>
+ <path d="M 477.31 545.63 C474.34,547.09 469.62,548.39 466.31,548.66 C461.36,549.08 460.02,548.8 457.28,546.82 C455.5,545.55 453.36,542.8 452.53,540.73 C448.77,531.44 451.8,519.33 464.55,492.8 C483.57,453.18 493.35,425.78 497.5,400.43 C499.49,388.28 499.4,383.37 497.12,380.15 C486.08,364.61 481.11,346.98 482.42,328 C482.78,322.77 483.48,316.72 483.98,314.55 C484.83,310.8 484.68,310.37 480.85,306.22 C463.91,287.84 473.71,261.31 497.75,260.48 L 504.03 260.26 L 513.27 251.33 C524.64,240.33 534.33,233.41 548,226.52 C570.64,215.12 590.83,210.12 617.61,209.29 C638.19,208.65 650.65,209.92 668.5,214.49 C695.54,221.41 718.34,233.76 737,251.59 L 745.5 259.71 L 752.76 260.3 C767.58,261.49 776.07,269.52 776.83,283.05 C777.34,291.95 775.12,298.57 769.59,304.68 L 765.33 309.39 L 766.81 315.94 C767.71,319.88 768.28,327.3 768.25,334.5 C768.18,349.62 765.93,358.82 758.89,372.6 L 753.74 382.7 L 755.44 392.1 C756.37,397.27 758.9,406.83 761.07,413.35 C763.23,419.87 765,425.6 765,426.08 C765,426.56 763.54,427.7 761.75,428.61 C759.96,429.53 756.42,432.01 753.87,434.13 L 749.24 437.99 L 748.16 435.24 C745.45,428.36 741.99,413.6 740.89,404.25 C740.23,398.61 739.41,394 739.08,394 C738.74,394 736.76,395.16 734.68,396.57 C729.29,400.24 724.53,400.85 723.04,398.08 C722,396.12 722.9,394.95 733.25,384.72 C743.03,375.05 745.18,372.32 748.77,365 C755.04,352.21 756.51,345.68 756.43,331 C756.36,318.28 755.23,312.05 750.92,300.51 C749,295.38 748.81,295.21 743.16,293.62 C739.97,292.73 735.14,292 732.41,292 L 727.45 292 L 727.84 296.17 C728.84,306.63 723.76,318.34 714.62,326.6 C706.88,333.61 700.26,336.16 688.26,336.77 C677.19,337.33 671.55,335.82 662.22,329.79 C658.77,327.55 655.81,325.9 655.64,326.11 C655.48,326.33 654.76,327.96 654.05,329.75 C652.42,333.85 641.94,343.71 635.72,347 C631.55,349.21 631,349.88 631,352.8 C631,362.63 642.61,371.15 653.91,369.6 C659.79,368.79 665.49,365.32 671.12,359.1 C675.96,353.77 678.69,352.83 681.43,355.57 C683.79,357.93 683.35,360.39 679.72,365.14 C675.18,371.09 668.97,375.59 662.44,377.65 C658.27,378.96 656.43,380.18 654.92,382.64 C651.48,388.2 642.43,395.7 636.95,397.52 C629.63,399.95 617.11,399.27 610.62,396.09 C605.48,393.57 595.52,384.22 594.48,380.94 C594.14,379.87 593.11,379 592.18,379 C588.57,379 577.75,372.84 572.99,368.09 C568.9,363.99 568,362.46 568,359.58 C568,356.01 569.97,354 573.45,354 C574.32,354 577.52,356.65 580.57,359.9 C589.97,369.92 599.58,372.32 609.68,367.16 C614.37,364.77 616.47,362.52 618.46,357.75 C621.11,351.41 620.57,349.8 614.88,347 C607.96,343.6 598.64,334.96 595.99,329.48 L 593.81 324.98 L 589.99 328.01 C587.89,329.67 583.55,332.26 580.34,333.77 C575.04,336.25 573.44,336.5 563,336.5 C552.3,336.5 551.09,336.3 545.65,333.59 C537.12,329.33 529.65,322.07 525.82,314.28 C523.02,308.6 522.58,306.67 522.55,299.74 L 522.5 291.75 L 515.5 292.34 C503.68,293.32 501.72,294.34 499.16,300.86 C493.96,314.13 492.4,334.91 495.63,347.94 C501.95,373.42 525.05,396.04 558.75,409.75 C563.06,411.51 564,412.33 564,414.32 C564,417.91 561.69,419.35 557.26,418.53 C550.88,417.35 534.94,410.21 525.87,404.47 C521.12,401.46 517.02,399 516.76,399 C516.49,399 515.96,402.94 515.57,407.75 C515.19,412.56 513.76,423.02 512.39,431 C503.88,480.71 505.18,498.7 518.35,513.66 C520.55,516.16 521.89,518.48 521.39,518.94 C520.9,519.39 518.7,520.32 516.5,521.02 C509,523.37 501.51,527.79 492,535.44 C486.77,539.65 480.16,544.23 477.31,545.63 ZM 742.7 572.93 C725.71,578.34 722.67,578.22 707,571.52 C691.39,564.86 670.04,559.17 650.57,556.48 C641.85,555.28 638.02,555.12 636.33,555.9 C631.96,557.89 620.88,560 614.83,560 C605.43,560 592,555.96 592,553.14 C592,551.17 598.4,549.69 610.12,548.95 C620.15,548.32 624.66,547.51 632.06,545.03 C650.89,538.72 663.42,530.03 662.81,523.69 L 662.5 520.5 L 653.38 520.8 C646.05,521.04 644.14,520.81 643.69,519.62 C642.85,517.44 644.72,516.47 652.39,515.12 C663.84,513.09 669,509.54 669,503.69 C669,499.25 667.09,498.7 654.76,499.6 C648.57,500.06 643.1,500.44 642.61,500.46 C642.12,500.48 641.22,499.7 640.61,498.72 C639.06,496.23 640.51,495.54 650.49,494.06 C659.72,492.69 662.7,490.77 661.49,486.97 C659.71,481.35 640.2,478.21 626.74,481.38 C616.45,483.81 610.79,487.22 600.07,497.48 C590.3,506.83 585.22,510 580,510 C576.92,510 574.5,507.75 575.31,505.64 C575.6,504.88 577.92,503.25 580.46,502.02 C583.06,500.76 586.91,497.58 589.29,494.72 C598.47,483.67 604.34,478.1 609.95,475.11 C616.65,471.53 615.69,474.37 619.05,448.5 C620.2,439.7 621.52,432.11 621.99,431.63 C622.46,431.15 625.69,430.46 629.17,430.1 C636.14,429.37 638,428.6 638,426.44 C638,425.47 639.11,425 641.43,425 C643.89,425 645.41,424.3 646.82,422.5 C648.88,419.89 651.86,419.26 653.65,421.05 C654.23,421.63 658.71,422.95 663.6,423.97 C668.5,424.99 676.1,427.03 680.5,428.49 C692.25,432.4 715.27,444.09 724.81,450.99 C729.39,454.29 733.56,457 734.09,457 C734.62,457 734.41,455.76 733.62,454.25 C729.48,446.28 717.94,435.38 707.5,429.57 C699.07,424.88 683.54,419.27 675.12,417.88 C667.08,416.55 664.72,414.85 667.32,412.25 C668.8,410.77 669.76,410.74 676.71,411.91 C694.86,414.95 715.88,424.29 726.27,433.91 C733.01,440.15 739.1,447.95 741.71,453.69 C742.66,455.79 743.53,457.65 743.64,457.84 C743.75,458.02 747.09,454.75 751.07,450.57 C766.85,433.95 787.92,425.59 815.97,424.8 L 826.45 424.5 L 826.98 427.5 C827.44,430.12 827.98,430.54 831.21,430.81 C834.87,431.11 836.97,433.62 835.53,435.95 C834.77,437.18 839.46,439.62 844.36,440.54 C846.45,440.93 848.4,441.89 848.7,442.67 C849.29,444.19 849.22,444.53 843.16,471.33 L 839.13 489.15 L 842.46 493.51 C847.94,500.7 849.51,506.11 849.41,517.5 C849.32,528.99 847.24,535.76 840.96,545 C837,550.84 834.17,553.29 826.97,557.1 C823.07,559.17 820.96,559.5 810.35,559.69 C792.15,560.01 769.09,564.52 742.7,572.93 ZM 494 286.05 C494,288.38 494.29,288.42 497.84,286.58 C501.55,284.66 514.83,282 520.71,282 C525.3,282 525.62,281.81 528.34,277.5 C532.05,271.62 538.16,266.38 545,263.2 C549.58,261.07 552.35,260.57 561.5,260.23 C573.93,259.77 579.83,261.32 588.74,267.38 C592.73,270.09 600.85,280.11 602.14,283.91 C602.38,284.6 604.2,284.43 607.5,283.41 C610.25,282.56 616.78,281.61 622,281.3 C631.11,280.77 633.12,281.04 645.5,284.5 C647.1,284.95 647.9,284.22 649.49,280.88 C652.47,274.58 659.17,267.72 665.75,264.21 C684.74,254.09 710.73,260.22 721.58,277.37 C724.34,281.73 724.35,281.74 731.42,282.28 C735.31,282.57 741.3,283.39 744.72,284.1 C748.14,284.81 751.26,285.07 751.65,284.68 C752.04,284.3 751.04,282.61 749.44,280.94 C747.01,278.41 746.78,277.75 748.02,276.97 C750.17,275.6 753.57,275.79 757.65,277.5 C762.23,279.41 762.28,279.4 760.97,276.95 C758.91,273.1 754,271.01 747,270.99 L 740.5 270.97 L 730.79 261.57 C714.87,246.18 696.1,234.95 675,228.21 C629.67,213.73 578.32,219.5 541.83,243.17 C533.17,248.79 520.62,259.56 514.59,266.56 C509.84,272.05 509.77,272.09 505.41,271.43 C499.12,270.49 494.47,271.65 490.48,275.15 C485.96,279.12 486.05,280.27 490.71,277.89 C494.61,275.9 498.63,275.48 500.77,276.83 C501.67,277.41 500.89,278.56 498.02,280.87 C495.8,282.65 494,284.97 494,286.05 ZM 676.35 326.62 C678.72,327.38 683.73,327.86 688,327.72 C705.32,327.14 718.01,314.57 717.99,298 C717.97,280.97 704.96,269 686.5,269 C673.6,269 664,274.03 658.29,283.77 C654.84,289.66 653.91,301.45 656.43,307.51 C660.23,316.63 667.71,323.81 676.35,326.62 ZM 551.36 325.92 C559.37,329 569.02,328.7 576.5,325.15 C588.95,319.22 596.04,307.48 594.59,295.17 C593.32,284.38 588.09,276.87 578.45,272 C574.15,269.83 572.19,269.5 563.5,269.51 C551.79,269.51 547.05,271.21 540.3,277.8 C533.55,284.39 532.5,287.12 532.5,298 C532.51,305.88 532.88,308.21 534.71,311.63 C537.66,317.15 545.05,323.49 551.36,325.92 ZM 710 561.32 C710.5,561.54 710.96,561.75 711.37,561.94 C712.61,562.53 713.45,562.92 714.08,562.71 C715.59,562.21 715.98,558.31 718.03,545.5 C720.46,530.36 730.83,469.51 731.52,466.34 C731.9,464.61 730.67,463.2 725.25,459.19 C710.98,448.62 692.11,439.24 674.5,433.96 C661.64,430.1 652.24,428.6 648,429.71 L 644.5 430.64 L 649.25 432.32 C651.86,433.24 656.03,434.71 658.5,435.58 C660.97,436.45 663,437.59 663,438.11 C663,440.25 659.4,440.13 651.18,437.71 C644.68,435.8 641.18,435.29 637.25,435.7 C634.36,436.01 632,436.55 632,436.92 C632,437.29 634.14,438.15 636.75,438.85 C639.36,439.55 650.5,443.52 661.5,447.68 C672.5,451.83 689.83,458.37 700,462.19 C710.17,466.02 718.8,469.45 719.17,469.83 C719.53,470.2 719.2,474.1 718.42,478.5 C717.13,485.83 715.78,494.49 708.42,542.88 C707.05,551.89 706.06,559.39 706.22,559.54 C706.37,559.68 708.08,560.49 710,561.32 ZM 798.42 545.12 C803.06,547.32 805.95,548 810.63,548 C822,548 829.55,542.68 835.02,530.8 C837.89,524.59 838.37,522.46 838.35,516.02 C838.33,507.89 836.36,502.52 831.83,498.3 C823.7,490.71 801.47,488.29 797.38,494.55 C794.84,498.41 797.34,501.27 805.17,503.46 C813.23,505.71 817,507.65 817,509.56 C817,512.16 814.24,512.29 805.97,510.05 C795.35,507.19 789.97,507.55 788.2,511.25 C785.63,516.64 789.98,521.43 801.07,525.45 C805.84,527.18 807.5,528.26 807.5,529.64 C807.5,531.91 802.94,532.06 795.9,530.01 C789.87,528.25 788,529.03 788,533.33 C788,538.06 791.24,541.72 798.42,545.12 ZM 734.41 562.16 L 733.88 564.82 L 737.87 563.65 C740.07,563 742.12,562.21 742.44,561.89 C742.99,561.35 753.3,515.81 759.99,484.43 L 763.2 469.35 L 779.35 462.98 C798.12,455.58 817.34,449.09 828,446.54 C834.93,444.88 839.24,443 836.1,443 C835.39,443 833.53,442.09 831.97,440.98 L 829.12 438.95 L 815.93 440.97 C796.67,443.93 772.13,452.57 758.88,461.08 C754.51,463.88 754.09,464.53 752.6,470.79 C749.22,485.04 743,515.47 739.06,537 C736.8,549.38 734.71,560.7 734.41,562.16 ZM 693.58 312.1 C688.61,314.36 683.5,314.45 679.5,312.34 C669.88,307.26 666.61,298.61 671.04,289.92 C676.07,280.07 690.22,278.24 698.78,286.34 C706.97,294.1 704.32,307.22 693.58,312.1 ZM 572.41 311.19 C567.6,314 561.2,314.39 556.46,312.18 C548.32,308.37 544.57,300.7 546.96,292.74 C552.2,275.26 580.03,278.85 579.98,297 C579.96,302.54 576.68,308.7 572.41,311.19 ZM 603.42 308.15 L 602.88 310.81 L 607.11 309.35 C613.9,307.01 629.28,306.54 638,308.4 L 638.01 308.4 C644.5,309.78 646.34,310.18 646.62,309.68 C646.74,309.48 646.61,309.14 646.43,308.67 L 646.43 308.67 C646.15,307.94 645.6,304.46 645.21,300.93 C644.58,295.23 644.19,294.38 641.71,293.19 C634.07,289.53 615.66,289.7 607.64,293.5 C604.77,294.86 604.48,295.45 604.23,300.25 C604.08,303.14 603.72,306.69 603.42,308.15 ZM 615.26 387.03 C623.63,390.83 634.77,389.15 641.26,383.1 L 645.26 379.36 L 639.24 377.17 C635.62,375.85 631.6,373.41 629.19,371.07 L 625.18 367.17 L 620.62 371.54 C617.88,374.16 614.36,376.38 611.79,377.12 C609.43,377.79 607.11,378.68 606.63,379.09 C605.35,380.2 610.36,384.81 615.26,387.03 ZM 757.05 454.39 C755.16,456.37 753.89,458 754.25,458 C754.6,458 757.77,456.19 761.29,453.98 C773.24,446.49 791.62,440.35 813.27,436.61 C823.82,434.79 824.84,434.28 821.97,432.27 C817.36,429.03 792.1,433.37 777.79,439.85 C770.39,443.19 762.36,448.82 757.05,454.39 ZM 730.43 351.43 C729.56,352.29 728.78,352.97 728.68,352.93 C719.66,349.24 716.79,348.69 705.74,348.54 C692.98,348.38 690.17,347.4 693.33,344.24 C696.25,341.32 712.91,341.22 722.51,344.07 C730.74,346.51 733.13,348.73 730.43,351.43 ZM 528 350.8 C524.98,352.01 522.51,352.98 522.5,352.97 C522.5,352.95 521.71,352.48 520.75,351.92 C513.58,347.75 530.05,341.55 546.69,342.16 C556.45,342.52 559.3,343.71 557.45,346.65 C556.64,347.93 554.24,348.27 544.97,348.42 C535.81,348.57 532.39,349.05 528,350.8 ZM 722.56 366.81 C721.84,369.06 718.62,369.81 717.49,367.98 C716.25,365.98 707.26,362.13 700.94,360.9 C692.97,359.35 690.81,357.76 693.28,355.3 C694.78,353.79 695.75,353.72 701.43,354.7 C708.27,355.87 716.93,359.42 720.88,362.67 C722.64,364.12 723.07,365.19 722.56,366.81 ZM 537.05 365.56 L 536.91 365.63 C531.84,368.32 530.31,369.14 529.29,368.71 C528.84,368.53 528.5,368.1 527.98,367.48 C526.24,365.38 528.89,362.59 536.13,358.94 C542.1,355.92 553.68,353.28 556.38,354.32 C558.84,355.26 558.4,358.87 555.75,359.47 C554.51,359.75 551.26,360.46 548.52,361.05 C545.78,361.63 540.62,363.66 537.05,365.56 ZM 619.25 320.78 C620.49,321.38 622.76,321.9 624.3,321.93 C627.81,322.02 633.26,319.28 632.74,317.69 C632.3,316.38 623,315.53 619.3,316.46 C616.37,317.2 616.34,319.38 619.25,320.78 ZM 561.15 295.4 C564.75,297.91 568.69,294.88 567.58,290.44 C567.11,288.58 566.3,288 564.18,288 C559.04,288 557.16,292.6 561.15,295.4 ZM 683.09 294.61 C684.68,296.52 688.97,296.02 690.39,293.75 C692.11,291.01 689.95,288.5 685.89,288.5 C683.11,288.5 682.44,288.92 682.17,290.81 C681.99,292.07 682.4,293.78 683.09,294.61 Z" fill="currentColor" />
+ </g>
+ </svg>
+ );
+}
\ No newline at end of file
diff --git a/src/client/src/constants/messages/en.ts b/src/client/src/constants/messages/en.ts
index 894eb423..cb9ec254 100644
--- a/src/client/src/constants/messages/en.ts
+++ b/src/client/src/constants/messages/en.ts
@@ -838,4 +838,70 @@ export const NEW_RULE = "New Rule";
export const LINK_EXISTING_RULE = "Link existing rule";
export const ASSOCIATE = "Associate";
export const RULES = "Rules";
-export const NO_DASH = "-";
\ No newline at end of file
+export const NO_DASH = "-";
+
+// Chat
+export const CHAT_TITLE = "Otter";
+export const CHAT_DESCRIPTION = "Ask questions about your observability data using natural language";
+export const CHAT_SETTINGS_TITLE = "Chat Settings";
+export const CHAT_SETTINGS_DESCRIPTION = "Configure the AI provider for the chat feature";
+export const CHAT_NEW_CHAT = "New Chat";
+export const CHAT_SEARCH_CONVERSATIONS = "Search conversations...";
+export const CHAT_NO_MATCHING_CONVERSATIONS = "No matching conversations";
+export const CHAT_NO_CONVERSATIONS_YET = "No conversations yet";
+export const CHAT_NEW_CONVERSATION = "New conversation";
+export const CHAT_ASK_QUESTION = "Ask a question about your data...";
+export const CHAT_ENTER_TO_SEND = "Press Enter to send, Shift+Enter for new line";
+export const CHAT_EMPTY_TITLE = "Your AI-Powered Observability Copilot";
+export const CHAT_EMPTY_DESCRIPTION = "Query your telemetry data, create dashboards, manage rules, prompts, and more — all through natural language. What would you like to explore?";
+export const CHAT_CONFIGURE_PROVIDER = "Configure your AI provider in";
+export const CHAT_SETTINGS_LINK = "Chat Settings";
+export const CHAT_TO_GET_STARTED = "to get started.";
+export const CHAT_EXAMPLE_Q1 = "Create a GPU monitoring dashboard with utilization, memory, and temperature widgets";
+export const CHAT_EXAMPLE_Q2 = "What are my top 5 most expensive models this week and how much did they cost?";
+export const CHAT_EXAMPLE_Q3 = "Create a rule that triggers when request duration exceeds 5 seconds";
+export const CHAT_EXAMPLE_Q4 = "Show me error rate by provider over the last 24 hours as a line chart";
+export const CHAT_EXAMPLE_Q5 = "Create a prompt template for summarizing customer feedback with a tone variable";
+export const CHAT_EXAMPLE_Q6 = "List all my vault secrets and show which ones are used in evaluations";
+export const CHAT_SQL_LABEL = "SQL Query";
+export const CHAT_COPY = "Copy";
+export const CHAT_COPIED = "Copied";
+export const CHAT_EXECUTE = "Execute";
+export const CHAT_RUNNING = "Running...";
+export const CHAT_QUERY_EXECUTION_FAILED = "Query execution failed";
+export const CHAT_NO_DATA_RETURNED = "No data returned";
+export const CHAT_SAVE_AS_WIDGET = "Save as Widget";
+export const CHAT_SAVE_WIDGET_TITLE = "Save as Widget";
+export const CHAT_WIDGET_TITLE_LABEL = "Title";
+export const CHAT_WIDGET_TITLE_PLACEHOLDER = "Widget title";
+export const CHAT_WIDGET_TYPE_LABEL = "Widget Type";
+export const CHAT_WIDGET_DASHBOARD_LABEL = "Add to Dashboard (optional)";
+export const CHAT_WIDGET_DASHBOARD_PLACEHOLDER = "Select a dashboard";
+export const CHAT_WIDGET_SAVED = "Widget saved successfully";
+export const CHAT_WIDGET_SAVE_FAILED = "Failed to save widget";
+export const CHAT_WIDGET_ENTER_TITLE = "Please enter a title";
+export const CHAT_SETTINGS_PROVIDER_LABEL = "AI Provider";
+export const CHAT_SETTINGS_PROVIDER_PLACEHOLDER = "Select a provider";
+export const CHAT_SETTINGS_MODEL_LABEL = "Model";
+export const CHAT_SETTINGS_MODEL_PLACEHOLDER = "Select a model";
+export const CHAT_SETTINGS_MODEL_HINT = "Models are managed in";
+export const CHAT_SETTINGS_MANAGE_MODELS = "Manage Models";
+export const CHAT_SETTINGS_MODEL_HINT_SUFFIX = ". Custom models you add there will appear here.";
+export const CHAT_SETTINGS_API_KEY_LABEL = "API Key (from Vault)";
+export const CHAT_SETTINGS_API_KEY_PLACEHOLDER = "Select a vault secret";
+export const CHAT_SETTINGS_API_KEY_HINT_PREFIX = "Store your API key in the";
+export const CHAT_SETTINGS_API_KEY_HINT_SUFFIX = "first, then select it here";
+export const CHAT_SETTINGS_SAVE = "Save Configuration";
+export const CHAT_SETTINGS_SAVED = "Chat configuration saved";
+export const CHAT_SETTINGS_SAVE_FAILED = "Failed to save configuration";
+export const CHAT_SETTINGS_LOAD_FAILED = "Failed to load configuration";
+export const CHAT_SETTINGS_FILL_ALL = "Please fill in all fields";
+export const CHAT_SETTINGS_SELECT_PROVIDER_FIRST = "Select a provider first";
+export const CHAT_FAILED_TO_CREATE_CONVERSATION = "Failed to create conversation";
+export const CHAT_FAILED_TO_DELETE_CONVERSATION = "Failed to delete conversation";
+export const CHAT_ROWS = "rows";
+export const CHAT_TOKENS = "tokens";
+export const CHAT_ERROR_PREFIX = "Error:";
+export const CHAT_SOMETHING_WENT_WRONG = "Something went wrong. Please try again.";
+export const CHAT_FAILED_TO_GET_RESPONSE = "Failed to get response";
+export const CHAT_NO_RESPONSE_STREAM = "No response stream";
\ No newline at end of file
diff --git a/src/client/src/constants/sidebar.tsx b/src/client/src/constants/sidebar.tsx
index 442aeaa5..f3d2ee6d 100644
--- a/src/client/src/constants/sidebar.tsx
+++ b/src/client/src/constants/sidebar.tsx
@@ -7,6 +7,7 @@ import {
Home,
Key,
LayoutDashboard,
+ MessageSquare,
MonitorCog,
MonitorPlay,
SettingsIcon,
@@ -17,6 +18,7 @@ import {
import DatabaseConfigSwitch from "@/components/(playground)/sidebar/database-config-switch";
import OpenTelemetrySvg from "@/components/svg/opentelemetry";
import OrganisationSwitch from "@/components/(playground)/sidebar/organisation-switch";
+import Otter from "@/components/svg/otter";
export const ICON_CLASSES = "flex-shrink-0 size-5";
@@ -38,6 +40,12 @@ export const SIDEBAR_ITEMS: SidebarItemProps[] = [
link: "/home",
type: "action",
},
+ {
+ icon: <Otter className={ICON_CLASSES} />,
+ text: "Otter Chat",
+ link: "/chat",
+ type: "action",
+ },
{
icon: <LayoutDashboard className={ICON_CLASSES} />,
text: "Dashboards",
diff --git a/src/client/src/lib/platform/chat/config.ts b/src/client/src/lib/platform/chat/config.ts
new file mode 100644
index 00000000..b2ec911b
--- /dev/null
+++ b/src/client/src/lib/platform/chat/config.ts
@@ -0,0 +1,107 @@
+import { dataCollector } from "../common";
+import { OPENLIT_CHAT_CONFIG_TABLE } from "./table-details";
+import Sanitizer from "@/utils/sanitizer";
+import { getSecrets } from "../vault";
+
+export interface ChatConfig {
+ id?: string;
+ provider: string;
+ model: string;
+ vaultId: string;
+ meta?: string;
+}
+
+export interface ChatConfigWithApiKey extends ChatConfig {
+ apiKey: string;
+}
+
+export async function getChatConfig(
+ databaseConfigId?: string
+): Promise<{ data?: ChatConfig; err?: unknown }> {
+ const query = `
+ SELECT id, provider, model, vault_id AS vaultId, meta,
+ created_at AS createdAt, updated_at AS updatedAt
+ FROM ${OPENLIT_CHAT_CONFIG_TABLE} FINAL
+ ORDER BY updated_at DESC
+ LIMIT 1
+ `;
+
+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);
+
+ if (err) {
+ return { err };
+ }
+
+ const records = data as ChatConfig[];
+ if (!records || records.length === 0) {
+ return { data: undefined };
+ }
+
+ return { data: records[0] };
+}
+
+export async function getChatConfigWithApiKey(
+ databaseConfigId?: string
+): Promise<{ data?: ChatConfigWithApiKey; err?: unknown }> {
+ const { data: config, err } = await getChatConfig(databaseConfigId);
+
+ if (err || !config) {
+ return { err: err || "Chat configuration not found. Please configure your AI provider in Chat Settings." };
+ }
+
+ const { data: secrets, err: secretErr } = await getSecrets(
+ { databaseConfigId },
+ { selectValue: true }
+ );
+
+ if (secretErr || !secrets) {
+ return { err: secretErr || "Failed to retrieve vault secrets" };
+ }
+
+ const secret = (secrets as any[]).find(
+ (s: any) => s.id === config.vaultId
+ );
+
+ if (!secret?.value) {
+ return { err: "API key not found in vault. Please check your chat configuration." };
+ }
+
+ return {
+ data: {
+ ...config,
+ apiKey: secret.value,
+ },
+ };
+}
+
+export async function upsertChatConfig(
+ config: ChatConfig,
+ databaseConfigId?: string
+): Promise<{ data?: string; err?: unknown }> {
+ const sanitizedProvider = Sanitizer.sanitizeValue(config.provider);
+ const sanitizedModel = Sanitizer.sanitizeValue(config.model);
+ const sanitizedVaultId = Sanitizer.sanitizeValue(config.vaultId);
+ const sanitizedMeta = Sanitizer.sanitizeValue(config.meta || "{}");
+
+ const { err } = await dataCollector(
+ {
+ table: OPENLIT_CHAT_CONFIG_TABLE,
+ values: [
+ {
+ provider: sanitizedProvider,
+ model: sanitizedModel,
+ vault_id: sanitizedVaultId,
+ meta: sanitizedMeta,
+ },
+ ],
+ },
+ "insert",
+ databaseConfigId
+ );
+
+ if (err) {
+ return { err };
+ }
+
+ return { data: "Chat configuration saved successfully" };
+}
diff --git a/src/client/src/lib/platform/chat/conversation.ts b/src/client/src/lib/platform/chat/conversation.ts
new file mode 100644
index 00000000..ab37eaae
--- /dev/null
+++ b/src/client/src/lib/platform/chat/conversation.ts
@@ -0,0 +1,389 @@
+import { dataCollector } from "../common";
+import {
+ OPENLIT_CHAT_CONVERSATION_TABLE,
+ OPENLIT_CHAT_MESSAGE_TABLE,
+} from "./table-details";
+import Sanitizer from "@/utils/sanitizer";
+
+export interface Conversation {
+ id: string;
+ title: string;
+ totalPromptTokens: number;
+ totalCompletionTokens: number;
+ totalCost: number;
+ totalMessages: number;
+ provider: string;
+ model: string;
+ createdAt: string;
+ updatedAt: string;
+}
+
+export interface ChatMessage {
+ id: string;
+ conversationId: string;
+ role: "user" | "assistant";
+ content: string;
+ sqlQuery: string;
+ queryResult: string;
+ widgetType: string;
+ promptTokens: number;
+ completionTokens: number;
+ cost: number;
+ queryRowsRead: number;
+ queryExecutionTimeMs: number;
+ queryBytesRead: number;
+ createdAt: string;
+}
+
+export async function getConversations(
+ databaseConfigId?: string
+): Promise<{ data?: Conversation[]; err?: unknown }> {
+ const query = `
+ SELECT
+ id, title,
+ total_prompt_tokens AS totalPromptTokens,
+ total_completion_tokens AS totalCompletionTokens,
+ total_cost AS totalCost,
+ total_messages AS totalMessages,
+ provider, model,
+ created_at AS createdAt,
+ updated_at AS updatedAt
+ FROM ${OPENLIT_CHAT_CONVERSATION_TABLE}
+ ORDER BY updated_at DESC
+ LIMIT 50
+ `;
+
+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);
+
+ if (err) {
+ return { err };
+ }
+
+ return { data: (data as Conversation[]) || [] };
+}
+
+export async function getConversationWithMessages(
+ conversationId: string,
+ databaseConfigId?: string
+): Promise<{ data?: { conversation: Conversation; messages: ChatMessage[] }; err?: unknown }> {
+ const safeId = Sanitizer.sanitizeValue(conversationId);
+
+ const convQuery = `
+ SELECT
+ id, title,
+ total_prompt_tokens AS totalPromptTokens,
+ total_completion_tokens AS totalCompletionTokens,
+ total_cost AS totalCost,
+ total_messages AS totalMessages,
+ provider, model,
+ created_at AS createdAt,
+ updated_at AS updatedAt
+ FROM ${OPENLIT_CHAT_CONVERSATION_TABLE}
+ WHERE id = '${safeId}'
+ LIMIT 1
+ `;
+
+ const { data: convData, err: convErr } = await dataCollector(
+ { query: convQuery },
+ "query",
+ databaseConfigId
+ );
+
+ if (convErr) {
+ return { err: convErr };
+ }
+
+ const conversations = convData as Conversation[];
+ if (!conversations || conversations.length === 0) {
+ return { err: "Conversation not found" };
+ }
+
+ const msgQuery = `
+ SELECT
+ id,
+ conversation_id AS conversationId,
+ role, content,
+ sql_query AS sqlQuery,
+ query_result AS queryResult,
+ widget_type AS widgetType,
+ prompt_tokens AS promptTokens,
+ completion_tokens AS completionTokens,
+ cost,
+ query_rows_read AS queryRowsRead,
+ query_execution_time_ms AS queryExecutionTimeMs,
+ query_bytes_read AS queryBytesRead,
+ created_at AS createdAt
+ FROM ${OPENLIT_CHAT_MESSAGE_TABLE}
+ WHERE conversation_id = '${safeId}'
+ ORDER BY created_at ASC
+ `;
+
+ const { data: msgData, err: msgErr } = await dataCollector(
+ { query: msgQuery },
+ "query",
+ databaseConfigId
+ );
+
+ if (msgErr) {
+ return { err: msgErr };
+ }
+
+ return {
+ data: {
+ conversation: conversations[0],
+ messages: (msgData as ChatMessage[]) || [],
+ },
+ };
+}
+
+export async function createConversation(
+ title: string,
+ provider: string,
+ model: string,
+ databaseConfigId?: string
+): Promise<{ data?: string; err?: unknown }> {
+ const safeTitle = Sanitizer.sanitizeValue(title || "");
+ const safeProvider = Sanitizer.sanitizeValue(provider);
+ const safeModel = Sanitizer.sanitizeValue(model);
+
+ const { err, data } = await dataCollector(
+ {
+ table: OPENLIT_CHAT_CONVERSATION_TABLE,
+ values: [
+ {
+ title: safeTitle,
+ provider: safeProvider,
+ model: safeModel,
+ },
+ ],
+ },
+ "insert",
+ databaseConfigId
+ );
+
+ if (err) {
+ return { err };
+ }
+
+ // Get the created conversation ID
+ const { data: latestData, err: latestErr } = await dataCollector(
+ {
+ query: `SELECT id FROM ${OPENLIT_CHAT_CONVERSATION_TABLE} ORDER BY created_at DESC LIMIT 1`,
+ },
+ "query",
+ databaseConfigId
+ );
+
+ if (latestErr) {
+ return { err: latestErr };
+ }
+
+ const records = latestData as { id: string }[];
+ return { data: records?.[0]?.id };
+}
+
+export async function deleteConversation(
+ conversationId: string,
+ databaseConfigId?: string
+): Promise<{ err?: unknown }> {
+ const safeId = Sanitizer.sanitizeValue(conversationId);
+
+ // Delete messages first
+ const { err: msgErr } = await dataCollector(
+ {
+ query: `DELETE FROM ${OPENLIT_CHAT_MESSAGE_TABLE} WHERE conversation_id = '${safeId}'`,
+ },
+ "exec",
+ databaseConfigId
+ );
+
+ if (msgErr) {
+ return { err: msgErr };
+ }
+
+ // Delete conversation
+ const { err } = await dataCollector(
+ {
+ query: `DELETE FROM ${OPENLIT_CHAT_CONVERSATION_TABLE} WHERE id = '${safeId}'`,
+ },
+ "exec",
+ databaseConfigId
+ );
+
+ return { err };
+}
+
+export async function addMessage(
+ {
+ conversationId,
+ role,
+ content,
+ sqlQuery,
+ queryResult,
+ widgetType,
+ promptTokens,
+ completionTokens,
+ cost,
+ }: {
+ conversationId: string;
+ role: "user" | "assistant";
+ content: string;
+ sqlQuery?: string;
+ queryResult?: string;
+ widgetType?: string;
+ promptTokens?: number;
+ completionTokens?: number;
+ cost?: number;
+ },
+ databaseConfigId?: string
+): Promise<{ data?: string; err?: unknown }> {
+ const { err, data } = await dataCollector(
+ {
+ table: OPENLIT_CHAT_MESSAGE_TABLE,
+ values: [
+ {
+ conversation_id: conversationId,
+ role,
+ content,
+ sql_query: sqlQuery || "",
+ query_result: queryResult || "",
+ widget_type: widgetType || "",
+ prompt_tokens: promptTokens || 0,
+ completion_tokens: completionTokens || 0,
+ cost: cost || 0,
+ },
+ ],
+ },
+ "insert",
+ databaseConfigId
+ );
+
+ if (err) {
+ return { err };
+ }
+
+ // Return the message ID
+ const { data: latestData } = await dataCollector(
+ {
+ query: `SELECT id FROM ${OPENLIT_CHAT_MESSAGE_TABLE} WHERE conversation_id = '${Sanitizer.sanitizeValue(conversationId)}' ORDER BY created_at DESC LIMIT 1`,
+ },
+ "query",
+ databaseConfigId
+ );
+
+ const records = latestData as { id: string }[];
+ return { data: records?.[0]?.id };
+}
+
+export async function updateMessage(
+ messageId: string,
+ updates: {
+ queryResult?: string;
+ queryRowsRead?: number;
+ queryExecutionTimeMs?: number;
+ queryBytesRead?: number;
+ },
+ databaseConfigId?: string
+): Promise<{ err?: unknown }> {
+ const safeId = Sanitizer.sanitizeValue(messageId);
+
+ const updateValues: string[] = [];
+ if (updates.queryResult !== undefined) {
+ updateValues.push(`query_result = '${Sanitizer.sanitizeValue(updates.queryResult)}'`);
+ }
+ if (updates.queryRowsRead !== undefined) {
+ updateValues.push(`query_rows_read = ${updates.queryRowsRead}`);
+ }
+ if (updates.queryExecutionTimeMs !== undefined) {
+ updateValues.push(`query_execution_time_ms = ${updates.queryExecutionTimeMs}`);
+ }
+ if (updates.queryBytesRead !== undefined) {
+ updateValues.push(`query_bytes_read = ${updates.queryBytesRead}`);
+ }
+
+ if (updateValues.length === 0) return {};
+
+ const query = `
+ ALTER TABLE ${OPENLIT_CHAT_MESSAGE_TABLE}
+ UPDATE ${updateValues.join(", ")}
+ WHERE id = '${safeId}'
+ `;
+
+ const { err } = await dataCollector({ query }, "exec", databaseConfigId);
+ return { err };
+}
+
+export async function updateConversation(
+ conversationId: string,
+ updates: {
+ title?: string;
+ addPromptTokens?: number;
+ addCompletionTokens?: number;
+ addCost?: number;
+ incrementMessages?: boolean;
+ },
+ databaseConfigId?: string
+): Promise<{ err?: unknown }> {
+ const safeId = Sanitizer.sanitizeValue(conversationId);
+
+ const updateValues: string[] = [
+ `updated_at = now()`,
+ ];
+
+ if (updates.title !== undefined) {
+ updateValues.push(`title = '${Sanitizer.sanitizeValue(updates.title)}'`);
+ }
+ if (updates.addPromptTokens) {
+ updateValues.push(`total_prompt_tokens = total_prompt_tokens + ${updates.addPromptTokens}`);
+ }
+ if (updates.addCompletionTokens) {
+ updateValues.push(`total_completion_tokens = total_completion_tokens + ${updates.addCompletionTokens}`);
+ }
+ if (updates.addCost) {
+ updateValues.push(`total_cost = total_cost + ${updates.addCost}`);
+ }
+ if (updates.incrementMessages) {
+ updateValues.push(`total_messages = total_messages + 1`);
+ }
+
+ const query = `
+ ALTER TABLE ${OPENLIT_CHAT_CONVERSATION_TABLE}
+ UPDATE ${updateValues.join(", ")}
+ WHERE id = '${safeId}'
+ `;
+
+ const { err } = await dataCollector({ query }, "exec", databaseConfigId);
+ return { err };
+}
+
+export async function getConversationMessages(
+ conversationId: string,
+ limit: number = 20,
+ databaseConfigId?: string
+): Promise<{ data?: ChatMessage[]; err?: unknown }> {
+ const safeId = Sanitizer.sanitizeValue(conversationId);
+
+ const query = `
+ SELECT
+ id,
+ conversation_id AS conversationId,
+ role, content,
+ sql_query AS sqlQuery,
+ prompt_tokens AS promptTokens,
+ completion_tokens AS completionTokens,
+ cost,
+ created_at AS createdAt
+ FROM ${OPENLIT_CHAT_MESSAGE_TABLE}
+ WHERE conversation_id = '${safeId}'
+ ORDER BY created_at ASC
+ LIMIT ${limit}
+ `;
+
+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);
+
+ if (err) {
+ return { err };
+ }
+
+ return { data: (data as ChatMessage[]) || [] };
+}
diff --git a/src/client/src/lib/platform/chat/save-widget.ts b/src/client/src/lib/platform/chat/save-widget.ts
new file mode 100644
index 00000000..772b1232
--- /dev/null
+++ b/src/client/src/lib/platform/chat/save-widget.ts
@@ -0,0 +1,109 @@
+import { createWidget } from "@/lib/platform/manage-dashboard/widget";
+import { dataCollector } from "@/lib/platform/common";
+import { OPENLIT_BOARD_WIDGET_TABLE_NAME } from "@/lib/platform/manage-dashboard/table-details";
+import Sanitizer from "@/utils/sanitizer";
+import crypto from "crypto";
+
+export interface SaveWidgetInput {
+ title: string;
+ description?: string;
+ type: string;
+ query: string;
+ properties?: Record<string, any>;
+ boardId?: string;
+}
+
+/**
+ * Convert hardcoded time references in SQL to Mustache template variables
+ * for dashboard time filter compatibility.
+ */
+export function convertToMustacheTemplate(query: string): string {
+ let result = query;
+
+ result = result.replace(
+ /parseDateTimeBestEffort\s*\(\s*'[^']+'\s*\)\s*(AS\s+start_time)/gi,
+ "parseDateTimeBestEffort('{{filter.timeLimit.start}}') $1"
+ );
+ result = result.replace(
+ /parseDateTimeBestEffort\s*\(\s*'[^']+'\s*\)\s*(AS\s+end_time)/gi,
+ "parseDateTimeBestEffort('{{filter.timeLimit.end}}') $1"
+ );
+ result = result.replace(
+ /Timestamp\s*>=\s*now\(\)\s*-\s*INTERVAL\s+\d+\s+\w+/gi,
+ "Timestamp >= parseDateTimeBestEffort('{{filter.timeLimit.start}}')"
+ );
+ result = result.replace(
+ /Timestamp\s*<=\s*now\(\)/gi,
+ "Timestamp <= parseDateTimeBestEffort('{{filter.timeLimit.end}}')"
+ );
+ result = result.replace(
+ /Timestamp\s*>=\s*toStartOf\w+\(now\(\)\)/gi,
+ "Timestamp >= parseDateTimeBestEffort('{{filter.timeLimit.start}}')"
+ );
+ result = result.replace(
+ /Timestamp\s*>=\s*'\d{4}-\d{2}-\d{2}[^']*'/gi,
+ "Timestamp >= parseDateTimeBestEffort('{{filter.timeLimit.start}}')"
+ );
+ result = result.replace(
+ /Timestamp\s*<=\s*'\d{4}-\d{2}-\d{2}[^']*'/gi,
+ "Timestamp <= parseDateTimeBestEffort('{{filter.timeLimit.end}}')"
+ );
+
+ return result;
+}
+
+/**
+ * Save a chat query as a dashboard widget, optionally adding it to a board.
+ */
+export async function saveQueryAsWidget(input: SaveWidgetInput) {
+ const templatedQuery = convertToMustacheTemplate(input.query);
+
+ const now = new Date().toISOString();
+ const widgetId = crypto.randomUUID();
+
+ const widget = {
+ id: widgetId,
+ title: input.title,
+ description: input.description || "",
+ type: input.type,
+ properties: input.properties || {},
+ config: { query: templatedQuery },
+ createdAt: now,
+ updatedAt: now,
+ };
+
+ const { data: widgetData, err: widgetErr } = await createWidget(widget);
+
+ if (widgetErr) {
+ return { err: widgetErr };
+ }
+
+ // Link widget to board if boardId provided
+ if (input.boardId) {
+ const createdWidgetId = (widgetData as any)?.id || widgetId;
+ const position = JSON.stringify({ x: 0, y: Infinity, w: 4, h: 6 });
+
+ const { err: linkErr } = await dataCollector(
+ {
+ table: OPENLIT_BOARD_WIDGET_TABLE_NAME,
+ values: [
+ {
+ board_id: Sanitizer.sanitizeValue(input.boardId),
+ widget_id: Sanitizer.sanitizeValue(createdWidgetId),
+ position,
+ },
+ ],
+ },
+ "insert"
+ );
+
+ if (linkErr) {
+ return {
+ data: widgetData,
+ warning: "Widget created but could not be added to the dashboard.",
+ };
+ }
+ }
+
+ return { data: widgetData };
+}
diff --git a/src/client/src/lib/platform/chat/schema-context.ts b/src/client/src/lib/platform/chat/schema-context.ts
new file mode 100644
index 00000000..3412b298
--- /dev/null
+++ b/src/client/src/lib/platform/chat/schema-context.ts
@@ -0,0 +1,333 @@
+export function getChatSystemPrompt(): string {
+ return `You are an AI assistant for OpenLIT, an OpenTelemetry-native observability platform. You have two capabilities:
+
+1. **Data Queries**: Convert natural language questions into ClickHouse SQL queries to analyze observability data (traces, metrics, costs, tokens, etc.)
+2. **Platform Management**: Create and manage platform resources using the available tools — rules, contexts, prompts, vault secrets, and custom models.
+
+When the user asks a question about their data, generate a SQL query. When the user asks to create or manage a resource, use the appropriate tool. If unclear, ask for clarification.
+
+## Available Tools (called automatically when needed)
+
+**Rule Engine** — create_rule, update_rule, delete_rule, list_rules, get_rule, link_entity_to_rule, unlink_entity_from_rule, list_rule_entities
+**Context** — create_context, update_context, delete_context, list_contexts
+**Prompt Hub** — create_prompt, update_prompt_version, delete_prompt, list_prompts
+**Vault** — create_vault_secret, update_vault_secret, delete_vault_secret, list_vault_secrets
+**Models** — create_custom_model, update_custom_model, delete_custom_model, list_custom_models
+
+Guidelines:
+- When the user asks to create something (vault secret, rule, context, prompt, model), do it IMMEDIATELY by calling the tool. Do NOT ask for confirmation first — just create it and report what was created.
+- When creating resources, confirm what was created with the key details (name, ID, status).
+- When listing, summarize the results concisely.
+- When the user asks to link a context or prompt to a rule, use link_entity_to_rule.
+- Vault keys are auto-normalized to UPPER_SNAKE_CASE.
+- Before deleting, confirm the resource name/ID with the user if possible.
+- If you need to find an ID before updating or deleting, use the appropriate list tool first.
+
+## Entity Links
+
+After any tool call that creates, updates, lists, or retrieves an entity, you MUST include an entity links block at the END of your response. This block allows the UI to render clickable navigation cards.
+
+Format — place this JSON block at the very end of your message:
+
+\`\`\`entities
+[
+ {"type": "rule", "id": "<uuid>", "name": "<display name>", "url": "/rule-engine/<uuid>"},
+]
+\`\`\`
+
+URL mappings per entity type:
+- **rule**: \`/rule-engine/{id}\`
+- **context**: \`/context/{id}\`
+- **prompt**: \`/prompt-hub/{id}\`
+- **vault**: \`/vault\` (no ID in URL)
+- **model**: \`/settings/manage-models\` (no ID in URL)
+- **evaluation**: \`/evaluations\` (no ID in URL)
+
+Rules:
+- Always include the entities block when a tool returns an entity with an ID.
+- For list operations, include up to 5 entities from the results.
+- For vault secrets, use the key name as the "name" field.
+- For link_entity_to_rule, include BOTH the rule and the linked entity.
+- Do NOT include the entities block for delete operations or when there are no entities to show.
+- The entities block must be valid JSON inside a fenced code block with language "entities".
+
+## Dashboard Generation
+
+When the user asks to create a dashboard, monitoring board, or visualization layout, output a complete dashboard JSON inside a \`\`\`dashboard fenced code block. The UI will automatically render a "Create Dashboard" button from this JSON. Do NOT use any tool for dashboard creation — just output the JSON directly in your response.
+
+CRITICAL RULES for dashboard queries:
+- ALL queries MUST use Mustache template variables for time filtering. NEVER hardcode dates or use now().
+- Every query MUST start with this CTE pattern:
+ \`WITH parseDateTimeBestEffort('{{filter.timeLimit.start}}') AS start_time, parseDateTimeBestEffort('{{filter.timeLimit.end}}') AS end_time\`
+- Use start_time/end_time in WHERE clauses: \`WHERE Timestamp >= start_time AND Timestamp <= end_time\`
+- For otel_metrics_gauge table, use \`TimeUnix\` instead of \`Timestamp\`
+- For STAT_CARD widgets with trend, also compute prev_start_time/prev_end_time and a rate column
+
+Dashboard JSON schema:
+\`\`\`
+{
+ "title": "Dashboard Title",
+ "description": "Description",
+ "widgets": {
+ "<uuid>": {
+ "id": "<same-uuid>",
+ "title": "Widget Title",
+ "description": "Widget description",
+ "type": "STAT_CARD|BAR_CHART|LINE_CHART|PIE_CHART|AREA_CHART|TABLE",
+ "properties": { ... },
+ "config": { "query": "ClickHouse SQL with {{filter.timeLimit.start}} and {{filter.timeLimit.end}}" }
+ }
+ },
+ "layouts": {
+ "lg": [
+ { "i": "<widget-uuid>", "x": 0, "y": 0, "w": 1, "h": 1 }
+ ]
+ }
+}
+\`\`\`
+
+### How properties connect query columns to the UI
+
+The \`properties\` object tells the widget renderer WHICH columns from the query result to display and HOW. The property values MUST exactly match the SQL column aliases.
+
+**STAT_CARD** — Shows a single metric value with optional trend percentage.
+- \`"value"\`: Path to the main value in the query result. Format: \`"0.column_alias"\` where \`0\` means first row and \`column_alias\` is the exact SQL AS name.
+- \`"trend"\`: Path to the trend/rate value: \`"0.rate"\`
+- \`"prefix"\`: Text before value (e.g. \`"$"\`)
+- \`"suffix"\`: Text after value (e.g. \`"s"\`, \`"%"\`)
+- \`"trendSuffix"\`: Text after trend (usually \`"%"\`)
+- \`"color"\`: Hex color
+
+Example — query returns \`{ total_cost: 42.5, rate: 12.3 }\`:
+\`\`\`
+"properties": { "value": "0.total_cost", "prefix": "$", "color": "#F36C06", "trend": "0.rate", "trendSuffix": "%" }
+\`\`\`
+
+**BAR_CHART** — Horizontal/vertical bars.
+- \`"xAxis"\`: SQL column alias for category labels
+- \`"yAxis"\`: SQL column alias for numeric values
+- \`"color"\`: Hex color
+
+Example — query returns \`[{ model: "gpt-4o", model_count: 150 }, ...]\`:
+\`\`\`
+"properties": { "xAxis": "model", "yAxis": "model_count", "color": "#F36C06" }
+\`\`\`
+
+**LINE_CHART** — Time series line.
+- \`"xAxis"\`: SQL column alias for time/x-axis (e.g. \`"request_time"\`)
+- \`"yAxis"\`: SQL column alias for y-axis values (e.g. \`"total"\`)
+- \`"color"\`: Hex color
+
+Example — query returns \`[{ request_time: "2025/06/16 10:00", total: 42 }, ...]\`:
+\`\`\`
+"properties": { "xAxis": "request_time", "yAxis": "total", "color": "#F36C06" }
+\`\`\`
+
+**PIE_CHART** — Distribution pie/donut.
+- \`"labelPath"\`: SQL column alias for slice labels
+- \`"valuePath"\`: SQL column alias for slice values
+- \`"color"\`: Hex color
+
+Example — query returns \`[{ provider: "openai", count: 500 }, ...]\`:
+\`\`\`
+"properties": { "labelPath": "provider", "valuePath": "count", "color": "#F36C06" }
+\`\`\`
+
+**AREA_CHART** — Stacked/multi-series area chart.
+- \`"xAxis"\`: SQL column alias for time axis
+- \`"yAxis"\`: Primary y-axis column (for single series)
+- \`"yAxes"\`: Array of series, each with \`"key"\` matching a SQL column alias and \`"color"\`
+
+Example — query returns \`[{ request_time: "...", prompt_tokens: 100, completion_tokens: 50 }, ...]\`:
+\`\`\`
+"properties": { "xAxis": "request_time", "yAxis": "total_tokens", "yAxes": [{"key": "prompt_tokens", "color": "#10b981"}, {"key": "completion_tokens", "color": "#F36C06"}] }
+\`\`\`
+
+**TABLE** — Raw data table. No column mapping needed, all query columns are shown.
+\`\`\`
+"properties": { "color": "#F36C06" }
+\`\`\`
+
+### Layout grid
+4 columns wide (x: 0-3). Sizes: STAT_CARD w=1,h=1. Charts w=2,h=2. Tables w=4,h=3.
+Generate unique UUIDs for widget IDs (format: "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx").
+
+### CRITICAL: Property values must match SQL aliases exactly
+If your query has \`SELECT COUNT(*) AS total_requests\`, the STAT_CARD value must be \`"0.total_requests"\` — not \`"0.total"\` or \`"0.count"\`.
+If your query has \`SELECT ... AS request_time\`, the LINE_CHART xAxis must be \`"request_time"\` — not \`"time"\` or \`"timestamp"\`.
+
+## Available Tables
+
+### otel_traces
+Primary table for traces and spans. This is the most commonly queried table.
+
+| Column | Type | Description |
+|--------|------|-------------|
+| Timestamp | DateTime64(9) | When the span occurred |
+| TraceId | String | Unique trace identifier |
+| SpanId | String | Unique span identifier |
+| ParentSpanId | String | Parent span ID (empty for root spans) |
+| SpanName | String | Operation name |
+| SpanKind | String | SPAN_KIND_CLIENT, SPAN_KIND_SERVER, SPAN_KIND_INTERNAL |
+| ServiceName | String | Service that generated the span |
+| Duration | UInt64 | Span duration in nanoseconds |
+| StatusCode | String | STATUS_CODE_OK, STATUS_CODE_ERROR, STATUS_CODE_UNSET |
+| StatusMessage | String | Error or status messages |
+| SpanAttributes | Map(String, String) | Key-value span attributes |
+| ResourceAttributes | Map(String, String) | Resource-level attributes |
+| Events.Timestamp | Array(DateTime64(9)) | Event timestamps |
+| Events.Name | Array(String) | Event names |
+| Events.Attributes | Array(Map(String, String)) | Event attributes |
+| Links.TraceId | Array(String) | Linked trace IDs |
+| Links.SpanId | Array(String) | Linked span IDs |
+| ScopeName | String | Instrumentation scope name |
+| ScopeVersion | String | Instrumentation scope version |
+
+### Common SpanAttributes Keys (accessed via SpanAttributes['key'])
+
+**LLM / Gen AI:**
+- \`gen_ai.system\` — LLM provider (openai, anthropic, cohere, etc.)
+- \`gen_ai.operation.name\` — Operation type (chat, completion, embedding, vectordb, etc.)
+- \`gen_ai.request.model\` — Requested model name
+- \`gen_ai.response.model\` — Actual model used in response
+- \`gen_ai.usage.input_tokens\` — Prompt/input token count
+- \`gen_ai.usage.output_tokens\` — Completion/output token count
+- \`gen_ai.usage.cost\` — Estimated cost in USD
+- \`gen_ai.response.finish_reason\` — Why generation stopped (stop, length, etc.)
+- \`gen_ai.application_name\` — Application name
+- \`gen_ai.environment\` — Deployment environment
+- \`gen_ai.endpoint\` — API endpoint URL
+- \`gen_ai.request.temperature\` — Temperature parameter
+- \`gen_ai.request.max_tokens\` — Max tokens requested
+- \`gen_ai.request.top_p\` — Top-p parameter
+- \`gen_ai.request.is_stream\` — Whether streaming was used
+
+**Vector Database:**
+- \`db.system\` — Database system (chroma, pinecone, qdrant, milvus, etc.)
+- \`db.operation\` — Operation type (query, insert, update, delete)
+- \`db.collection.name\` — Collection name
+- \`db.vector.query.top_k\` — Number of results requested
+
+**Resource Attributes (accessed via ResourceAttributes['key']):**
+- \`service.name\` — Service/application name
+- \`deployment.environment\` — Deployment environment (production, staging, etc.)
+
+### otel_metrics_gauge
+Gauge metrics (point-in-time values like GPU temperature, memory usage).
+
+| Column | Type | Description |
+|--------|------|-------------|
+| ResourceAttributes | Map(String, String) | Resource attributes |
+| ScopeName | String | Scope name |
+| ScopeVersion | String | Scope version |
+| MetricName | String | Metric name |
+| Value | Float64 | Metric value |
+| TimeUnix | DateTime64(9) | Measurement timestamp |
+| Attributes | Map(String, String) | Metric attributes |
+
+### otel_metrics_sum
+Counter/sum metrics (cumulative values like token counts).
+
+| Column | Type | Description |
+|--------|------|-------------|
+| ResourceAttributes | Map(String, String) | Resource attributes |
+| ScopeName | String | Scope name |
+| ScopeVersion | String | Scope version |
+| MetricName | String | Metric name |
+| Value | Float64 | Metric value |
+| TimeUnix | DateTime64(9) | Measurement timestamp |
+| Attributes | Map(String, String) | Metric attributes |
+| IsMonotonic | Boolean | Whether counter only increases |
+| AggregationTemporality | String | Delta or cumulative |
+
+### otel_metrics_histogram
+Histogram metrics (distribution data like request durations).
+
+| Column | Type | Description |
+|--------|------|-------------|
+| ResourceAttributes | Map(String, String) | Resource attributes |
+| ScopeName | String | Scope name |
+| ScopeVersion | String | Scope version |
+| MetricName | String | Metric name |
+| Count | UInt64 | Number of observations |
+| Sum | Float64 | Sum of all observations |
+| TimeUnix | DateTime64(9) | Measurement timestamp |
+| Attributes | Map(String, String) | Metric attributes |
+| BucketCounts | Array(UInt64) | Histogram bucket counts |
+| ExplicitBounds | Array(Float64) | Histogram bucket boundaries |
+
+## Rules
+1. Generate valid ClickHouse SQL only.
+2. Use ClickHouse-specific functions: toStartOfHour(), toStartOfDay(), toStartOfMinute(), parseDateTimeBestEffort(), formatDateTime(), toFloat64OrZero(), etc.
+3. For Map columns, use bracket notation: SpanAttributes['gen_ai.request.model']
+4. Always include a LIMIT clause (default LIMIT 100, max LIMIT 1000).
+5. Duration is in nanoseconds — divide by 1e9 to get seconds.
+6. When the user asks about "requests", "calls", or "traces", query otel_traces.
+7. For cost analysis, use toFloat64OrZero(SpanAttributes['gen_ai.usage.cost']).
+8. For token usage, cast to numbers: toUInt64OrZero(SpanAttributes['gen_ai.usage.input_tokens']).
+9. Return the SQL query wrapped in a \`\`\`sql code block.
+10. After the SQL block, briefly explain what the query does (1-2 sentences).
+11. Suggest the best visualization type for the results: STAT_CARD, BAR_CHART, LINE_CHART, PIE_CHART, AREA_CHART, or TABLE.
+12. Format the suggestion as: **Visualization:** TYPE_NAME
+13. **IMPORTANT: Always use the WITH CTE pattern for time filtering.** Every query MUST start with:
+ \`\`\`
+ WITH
+ parseDateTimeBestEffort('START_TIME') AS start_time,
+ parseDateTimeBestEffort('END_TIME') AS end_time
+ \`\`\`
+ Then use \`start_time\` and \`end_time\` in your WHERE clauses: \`WHERE Timestamp >= start_time AND Timestamp <= end_time\`.
+ For "today" use now() for END_TIME and toStartOfDay(now()) for START_TIME.
+ For "last 24 hours" use now() for END_TIME and (now() - INTERVAL 24 HOUR) for START_TIME.
+ This pattern is required so queries can be saved as reusable dashboard widgets with dynamic time ranges.
+
+## Example Queries
+
+**User:** "How many requests did I get today?"
+\`\`\`sql
+WITH
+ parseDateTimeBestEffort(toString(toStartOfDay(now()))) AS start_time,
+ parseDateTimeBestEffort(toString(now())) AS end_time
+SELECT
+ CAST(COUNT(*) AS UInt64) AS total_requests
+FROM otel_traces
+WHERE Timestamp >= start_time AND Timestamp <= end_time
+LIMIT 1
+\`\`\`
+This counts all trace spans recorded since the start of today.
+**Visualization:** STAT_CARD
+
+**User:** "Show me the top 5 most used models"
+\`\`\`sql
+WITH
+ parseDateTimeBestEffort(toString(now() - INTERVAL 7 DAY)) AS start_time,
+ parseDateTimeBestEffort(toString(now())) AS end_time
+SELECT
+ SpanAttributes['gen_ai.request.model'] AS model,
+ CAST(COUNT(*) AS UInt64) AS request_count
+FROM otel_traces
+WHERE Timestamp >= start_time AND Timestamp <= end_time
+ AND SpanAttributes['gen_ai.request.model'] != ''
+GROUP BY model
+ORDER BY request_count DESC
+LIMIT 5
+\`\`\`
+This shows the 5 most frequently used AI models by request count over the last 7 days.
+**Visualization:** BAR_CHART
+
+**User:** "Show request count per hour for the last 24 hours"
+\`\`\`sql
+WITH
+ parseDateTimeBestEffort(toString(now() - INTERVAL 24 HOUR)) AS start_time,
+ parseDateTimeBestEffort(toString(now())) AS end_time
+SELECT
+ toStartOfHour(Timestamp) AS hour,
+ CAST(COUNT(*) AS UInt64) AS request_count
+FROM otel_traces
+WHERE Timestamp >= start_time AND Timestamp <= end_time
+GROUP BY hour
+ORDER BY hour ASC
+LIMIT 100
+\`\`\`
+This shows the hourly distribution of requests over the last 24 hours.
+**Visualization:** LINE_CHART`;
+}
diff --git a/src/client/src/lib/platform/chat/sql-validator.ts b/src/client/src/lib/platform/chat/sql-validator.ts
new file mode 100644
index 00000000..89711d7a
--- /dev/null
+++ b/src/client/src/lib/platform/chat/sql-validator.ts
@@ -0,0 +1,127 @@
+const ALLOWED_TABLES = [
+ "otel_traces",
+ "otel_metrics_gauge",
+ "otel_metrics_sum",
+ "otel_metrics_histogram",
+];
+
+const FORBIDDEN_KEYWORDS = [
+ "INSERT",
+ "UPDATE",
+ "DELETE",
+ "DROP",
+ "CREATE",
+ "ALTER",
+ "TRUNCATE",
+ "GRANT",
+ "REVOKE",
+ "RENAME",
+ "ATTACH",
+ "DETACH",
+ "OPTIMIZE",
+ "KILL",
+];
+
+export interface SQLValidationResult {
+ valid: boolean;
+ query?: string;
+ error?: string;
+}
+
+export function validateSQL(query: string): SQLValidationResult {
+ if (!query || typeof query !== "string" || query.trim().length === 0) {
+ return { valid: false, error: "Empty query" };
+ }
+
+ let cleaned = query.trim();
+
+ // Remove leading/trailing backticks or code block markers
+ if (cleaned.startsWith("```sql")) {
+ cleaned = cleaned.slice(6);
+ } else if (cleaned.startsWith("```")) {
+ cleaned = cleaned.slice(3);
+ }
+ if (cleaned.endsWith("```")) {
+ cleaned = cleaned.slice(0, -3);
+ }
+ cleaned = cleaned.trim();
+
+ // Remove trailing semicolons (ClickHouse client doesn't need them)
+ while (cleaned.endsWith(";")) {
+ cleaned = cleaned.slice(0, -1).trim();
+ }
+
+ // Check for forbidden DDL/DML keywords at statement start or after semicolons
+ const upperQuery = cleaned.toUpperCase();
+ for (const keyword of FORBIDDEN_KEYWORDS) {
+ // Check if query starts with forbidden keyword
+ if (upperQuery.startsWith(keyword + " ") || upperQuery.startsWith(keyword + "\n") || upperQuery.startsWith(keyword + "\t")) {
+ return {
+ valid: false,
+ error: `Query contains forbidden operation: ${keyword}. Only SELECT queries are allowed.`,
+ };
+ }
+ }
+
+ // Must start with SELECT or WITH (for CTEs)
+ if (!upperQuery.startsWith("SELECT") && !upperQuery.startsWith("WITH")) {
+ return {
+ valid: false,
+ error: "Only SELECT queries (or WITH...SELECT) are allowed.",
+ };
+ }
+
+ // Check that only allowed tables are referenced
+ // Extract table names after FROM and JOIN keywords
+ const fromJoinPattern = /(?:FROM|JOIN)\s+(\w+)/gi;
+ let match;
+ while ((match = fromJoinPattern.exec(cleaned)) !== null) {
+ const tableName = match[1].toLowerCase();
+ // Skip subquery aliases and CTEs
+ if (tableName === "select" || tableName === "with") continue;
+ if (!ALLOWED_TABLES.includes(tableName)) {
+ return {
+ valid: false,
+ error: `Table "${match[1]}" is not allowed. Allowed tables: ${ALLOWED_TABLES.join(", ")}`,
+ };
+ }
+ }
+
+ // Enforce LIMIT clause — append if missing
+ if (!/\bLIMIT\b/i.test(cleaned)) {
+ cleaned = cleaned + "\nLIMIT 1000";
+ }
+
+ // Check that existing LIMIT is not unreasonably large
+ const limitMatch = /\bLIMIT\s+(\d+)/i.exec(cleaned);
+ if (limitMatch) {
+ const limitValue = parseInt(limitMatch[1], 10);
+ if (limitValue > 10000) {
+ cleaned = cleaned.replace(
+ /\bLIMIT\s+\d+/i,
+ "LIMIT 1000"
+ );
+ }
+ }
+
+ return { valid: true, query: cleaned };
+}
+
+/**
+ * Extract SQL queries from markdown-formatted LLM responses.
+ * Looks for ```sql code blocks.
+ */
+export function extractSQLFromResponse(response: string): string[] {
+ const sqlBlocks: string[] = [];
+ const regex = /```sql\s*\n([\s\S]*?)```/gi;
+ let match;
+
+ while ((match = regex.exec(response)) !== null) {
+ const sql = match[1].trim();
+ if (sql.length > 0) {
+ sqlBlocks.push(sql);
+ }
+ }
+
+ return sqlBlocks;
+}
diff --git a/src/client/src/lib/platform/chat/stream.ts b/src/client/src/lib/platform/chat/stream.ts
new file mode 100644
index 00000000..ba00f6c6
--- /dev/null
+++ b/src/client/src/lib/platform/chat/stream.ts
@@ -0,0 +1,319 @@
+import { streamText, generateText, stepCountIs } from "ai";
+import { createOpenAI } from "@ai-sdk/openai";
+import { createAnthropic } from "@ai-sdk/anthropic";
+import { google } from "@ai-sdk/google";
+import { createMistral } from "@ai-sdk/mistral";
+import { createCohere } from "@ai-sdk/cohere";
+import { getChatSystemPrompt } from "./schema-context";
+import { validateSQL, extractSQLFromResponse } from "./sql-validator";
+import { dataCollector } from "../common";
+import {
+ addMessage,
+ getConversationMessages,
+ updateConversation,
+} from "./conversation";
+import { getChatTools } from "./tools";
+
+// ==================== Provider Factories ====================
+
+type ProviderFactory = (apiKey: string) => any;
+
+const providerFactories: Record<string, ProviderFactory> = {
+ openai: (apiKey) => createOpenAI({ apiKey }),
+ anthropic: (apiKey) => createAnthropic({ apiKey }),
+ google: () => google,
+ mistral: (apiKey) => createMistral({ apiKey }),
+ cohere: (apiKey) => createCohere({ apiKey }),
+ groq: (apiKey) => createOpenAI({ baseURL: "https://api.groq.com/openai/v1", apiKey }),
+ perplexity: (apiKey) => createOpenAI({ baseURL: "https://api.perplexity.ai", apiKey }),
+ azure: (apiKey) => createOpenAI({
+ baseURL: process.env.AZURE_OPENAI_ENDPOINT || "https://your-resource.openai.azure.com",
+ apiKey,
+ headers: { "api-key": apiKey },
+ }),
+ together: (apiKey) => createOpenAI({ baseURL: "https://api.together.xyz/v1", apiKey }),
+ fireworks: (apiKey) => createOpenAI({ baseURL: "https://api.fireworks.ai/inference/v1", apiKey }),
+ deepseek: (apiKey) => createOpenAI({ baseURL: "https://api.deepseek.com", apiKey }),
+ xai: (apiKey) => createOpenAI({ baseURL: "https://api.x.ai/v1", apiKey }),
+ huggingface: (apiKey) => createOpenAI({ baseURL: "https://api-inference.huggingface.co/v1", apiKey }),
+ replicate: (apiKey) => createOpenAI({ baseURL: "https://openai-proxy.replicate.com/v1", apiKey }),
+};
+
+const VALID_PROVIDERS = Object.keys(providerFactories);
+
+export function getModelInstance(providerId: string, apiKey: string, modelName: string) {
+ if (!VALID_PROVIDERS.includes(providerId)) {
+ throw new Error(`Provider ${providerId} not supported`);
+ }
+ const factory = providerFactories[providerId];
+ const provider = factory(apiKey);
+ if (typeof provider === "function") return provider(modelName);
+ if (typeof provider === "object" && provider !== null) return provider(modelName);
+ throw new Error(`Invalid provider instance for ${providerId}`);
+}
+
+// ==================== Error Formatting ====================
+
+export function formatStreamError(e: any): string {
+ if (e?.statusCode === 401 || e?.data?.error?.code === "invalid_api_key") {
+ return "Invalid API key. Please check your API key in Chat Settings and Vault.";
+ } else if (e?.statusCode === 429) {
+ return "Rate limit exceeded. Please wait a moment and try again.";
+ } else if (e?.statusCode === 403) {
+ return "Access denied. Your API key may not have permission for this model.";
+ } else if (e?.statusCode === 404) {
+ return "Model not found. Please check the model name in Chat Settings.";
+ } else if (e?.statusCode >= 500) {
+ return "The AI provider is experiencing issues. Please try again later.";
+ } else if (e?.message) {
+ return e.message.replace(/[a-zA-Z0-9_\-:=+/]{30,}/g, "***");
+ }
+ return "An error occurred while generating the response.";
+}
+
+// ==================== Build Messages ====================
+
+export async function buildConversationMessages(
+ conversationId: string,
+ content: string
+): Promise<{ role: "user" | "assistant"; content: string }[]> {
+ const { data: history } = await getConversationMessages(conversationId, 20);
+ const messages: { role: "user" | "assistant"; content: string }[] = [];
+
+ if (history && history.length > 0) {
+ for (const msg of history) {
+ if (msg.role === "user" || msg.role === "assistant") {
+ messages.push({ role: msg.role, content: msg.content });
+ }
+ }
+ }
+
+ if (messages.length === 0 || messages[messages.length - 1].content !== content) {
+ messages.push({ role: "user", content });
+ }
+
+ return messages;
+}
+
+// ==================== Stream Chat ====================
+
+export interface StreamChatParams {
+ conversationId: string;
+ content: string;
+ provider: string;
+ apiKey: string;
+ model: string;
+ userId: string;
+ dbConfigId: string;
+}
+
+export interface StreamChatResult {
+ responseText: string;
+ streamError: any;
+}
+
+/**
+ * Execute a streaming chat request. Returns the full response text and any stream error.
+ * Also handles:
+ * - Saving assistant message with token/cost stats
+ * - Executing SQL blocks and appending results
+ * - Generating conversation title for first message
+ * - Handling tool call fallback text
+ */
+export async function streamChatMessage(params: StreamChatParams): Promise<StreamChatResult> {
+ const { conversationId, content, provider, apiKey, model, userId, dbConfigId } = params;
+
+ // Save user message
+ await addMessage({ conversationId, role: "user", content });
+
+ // Build messages
+ const messages = await buildConversationMessages(conversationId, content);
+
+ // Create model instance
+ const modelInstance = getModelInstance(provider, apiKey, model);
+
+ const isFirstMessage = messages.filter((m) => m.role === "user").length === 1;
+ const tools = getChatTools(userId, dbConfigId);
+
+ let streamError: any = null;
+
+ // Shared usage stats — written by onFinish, read after stream consumption
+ let finishStats = { promptTokens: 0, completionTokens: 0, cost: 0 };
+ let finishResolve: () => void;
+ const finishPromise = new Promise<void>((resolve) => { finishResolve = resolve; });
+
+ const result = streamText({
+ model: modelInstance,
+ system: getChatSystemPrompt(),
+ messages,
+ tools,
+ stopWhen: stepCountIs(3),
+ onError: ({ error }) => {
+ streamError = error;
+ },
+ onFinish: async ({ text, usage, steps }) => {
+ const promptTokens = usage?.inputTokens ?? 0;
+ const completionTokens = usage?.outputTokens ?? 0;
+ const cost = (promptTokens * 0.003 + completionTokens * 0.015) / 1000;
+
+ // Store stats so the main flow can use them when saving the message
+ finishStats = { promptTokens, completionTokens, cost };
+
+ // Build tool-call fallback text if no LLM text was produced
+ let finalText = text;
+ if (!finalText && steps) {
+ const summaries: string[] = [];
+ for (const step of steps) {
+ if (step.toolResults) {
+ for (const tr of step.toolResults as any[]) {
+ const r = tr.result;
+ if (r?.success) {
+ let msg = `**${r.message}**`;
+ if (r.details) msg += `\n\n${r.details}`;
+ summaries.push(msg);
+ } else if (r?.error) {
+ summaries.push(`**Error:** ${r.error}`);
+ }
+ }
+ }
+ }
+ if (summaries.length > 0) finalText = summaries.join("\n\n");
+ }
+
+ // Update conversation stats
+ await updateConversation(conversationId, {
+ addPromptTokens: promptTokens,
+ addCompletionTokens: completionTokens,
+ addCost: cost,
+ incrementMessages: true,
+ });
+
+ // Generate title for first message
+ if (isFirstMessage && finalText) {
+ generateConversationTitle(provider, apiKey, model, content, finalText, conversationId).catch(() => {
+ updateConversation(conversationId, {
+ title: content.slice(0, 50) + (content.length > 50 ? "..." : ""),
+ }).catch(() => {});
+ });
+ }
+
+ finishResolve!();
+ },
+ });
+
+ // Consume the stream
+ const fullStream = result.fullStream;
+ const parts: string[] = [];
+ let hasText = false;
+ const toolResultMessages: string[] = [];
+
+ for await (const part of fullStream) {
+ if (part.type === "text-delta" && (part as any).text) {
+ hasText = true;
+ parts.push((part as any).text);
+ } else if (part.type === "tool-result") {
+ const r = (part as any).result;
+ if (r?.success) {
+ let msg = `**${r.message}**`;
+ if (r.details) msg += `\n\n${r.details}`;
+ toolResultMessages.push(msg);
+ } else if (r?.error) {
+ toolResultMessages.push(`**Error:** ${r.error}`);
+ }
+ }
+ }
+
+ if (!hasText && toolResultMessages.length > 0) {
+ parts.push(toolResultMessages.join("\n\n"));
+ }
+
+ let responseText = parts.join("");
+
+ // Handle API errors
+ if (!responseText && streamError) {
+ const errorMsg = formatStreamError(streamError);
+ responseText = `**Error:** ${errorMsg}`;
+ await addMessage({ conversationId, role: "assistant", content: responseText });
+ }
+
+ // Execute SQL blocks in the response and append results BEFORE returning to client
+ if (responseText && !responseText.startsWith("**Error:**")) {
+ responseText = await executeSQLBlocksInResponse(responseText);
+
+ // Wait for onFinish to complete so we have token/cost stats
+ await Promise.race([finishPromise, new Promise((r) => setTimeout(r, 5000))]);
+
+ // Save the enriched message with query results AND token/cost stats
+ await addMessage({
+ conversationId,
+ role: "assistant",
+ content: responseText,
+ promptTokens: finishStats.promptTokens,
+ completionTokens: finishStats.completionTokens,
+ cost: finishStats.cost,
+ });
+ }
+
+ return { responseText, streamError };
+}
+
+/**
+ * Find SQL code blocks in a response, execute them, and append query-result blocks.
+ */
+async function executeSQLBlocksInResponse(text: string): Promise<string> {
+ const sqlBlocks = extractSQLFromResponse(text);
+ if (sqlBlocks.length === 0) return text;
+
+ let enrichedText = text;
+ for (const sql of sqlBlocks) {
+ const validation = validateSQL(sql);
+ if (validation.valid && validation.query) {
+ try {
+ const { data: queryData, err: queryErr } = await dataCollector({
+ query: validation.query,
+ enable_readonly: true,
+ });
+ if (!queryErr && queryData) {
+ const resultJson = JSON.stringify(queryData);
+ const escapedSql = sql.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ const sqlBlockRegex = new RegExp(
+ "```sql\\s*\\n" + escapedSql.replace(/\n/g, "\\n") + "\\s*\\n?```"
+ );
+ if (sqlBlockRegex.test(enrichedText)) {
+ enrichedText = enrichedText.replace(
+ sqlBlockRegex,
+ (match) => `${match}\n\`\`\`query-result\n${resultJson}\n\`\`\``
+ );
+ } else {
+ enrichedText += `\n\n\`\`\`query-result\n${resultJson}\n\`\`\``;
+ }
+ }
+ } catch {
+ // Skip failed queries — user can run manually
+ }
+ }
+ }
+ return enrichedText;
+}
+
+// ==================== Title Generation ====================
+
+async function generateConversationTitle(
+ providerId: string,
+ apiKey: string,
+ modelName: string,
+ userMessage: string,
+ assistantResponse: string,
+ conversationId: string
+) {
+ const modelInstance = getModelInstance(providerId, apiKey, modelName);
+ const { text: title } = await generateText({
+ model: modelInstance,
+ prompt: `Generate a short title (max 6 words) summarizing this conversation. User asked: "${userMessage.slice(0, 200)}" Assistant responded about: "${assistantResponse.slice(0, 200)}". Return ONLY the title text, no quotes or punctuation.`,
+ maxOutputTokens: 20,
+ });
+ const cleanTitle = title.trim().replace(/^["']|["']$/g, "");
+ if (cleanTitle) {
+ await updateConversation(conversationId, { title: cleanTitle });
+ }
+}
diff --git a/src/client/src/lib/platform/chat/table-details.ts b/src/client/src/lib/platform/chat/table-details.ts
new file mode 100644
index 00000000..1999b956
--- /dev/null
+++ b/src/client/src/lib/platform/chat/table-details.ts
@@ -0,0 +1,3 @@
+export const OPENLIT_CHAT_CONFIG_TABLE = "openlit_chat_config";
+export const OPENLIT_CHAT_CONVERSATION_TABLE = "openlit_chat_conversation";
+export const OPENLIT_CHAT_MESSAGE_TABLE = "openlit_chat_message";
diff --git a/src/client/src/lib/platform/chat/tools.ts b/src/client/src/lib/platform/chat/tools.ts
new file mode 100644
index 00000000..473a6800
--- /dev/null
+++ b/src/client/src/lib/platform/chat/tools.ts
@@ -0,0 +1,630 @@
+import { tool, jsonSchema } from "ai";
+import {
+ createRule,
+ updateRule,
+ deleteRule,
+ getRules,
+ getRuleById,
+ addConditionGroupsToRule,
+ addRuleEntity,
+ deleteRuleEntity,
+ getRuleEntities,
+} from "../rule-engine";
+import {
+ createContext,
+ updateContext,
+ deleteContext,
+ getContexts,
+} from "../context";
+import {
+ createPrompt,
+ getPrompts,
+ deletePrompt,
+} from "../prompt";
+import { upsertPromptVersion } from "../prompt/version";
+import { upsertSecret, deleteSecret, getSecrets } from "../vault";
+import {
+ createCustomModel,
+ updateCustomModel,
+ deleteCustomModel,
+ getCustomModels,
+} from "../openground/custom-models-service";
+
+/**
+ * Normalize a vault key name to UPPER_SNAKE_CASE.
+ */
+function normalizeVaultKey(key: string): string {
+ return key
+ .trim()
+ .replace(/([a-z])([A-Z])/g, "$1_$2")
+ .replace(/[\s\-\.]+/g, "_")
+ .replace(/[^a-zA-Z0-9_]/g, "")
+ .replace(/_+/g, "_")
+ .replace(/^_|_$/g, "")
+ .toUpperCase();
+}
+
+export function getChatTools(userId: string, databaseConfigId: string) {
+ return {
+ // ==================== RULE ENGINE ====================
+
+ create_rule: tool<any, any>({
+ description: "Create a new rule in the Rule Engine with optional conditions.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ name: { type: "string", description: "Rule name" },
+ description: { type: "string" },
+ group_operator: { type: "string", enum: ["AND", "OR"] },
+ status: { type: "string", enum: ["ACTIVE", "INACTIVE"] },
+ condition_groups: {
+ type: "array",
+ items: {
+ type: "object",
+ properties: {
+ condition_operator: { type: "string", enum: ["AND", "OR"] },
+ conditions: {
+ type: "array",
+ items: {
+ type: "object",
+ properties: {
+ field: { type: "string" },
+ operator: { type: "string", enum: ["equals", "not_equals", "contains", "not_contains", "starts_with", "ends_with", "gt", "gte", "lt", "lte"] },
+ value: { type: "string" },
+ data_type: { type: "string", enum: ["string", "number", "boolean"] },
+ },
+ required: ["field", "operator", "value"],
+ },
+ },
+ },
+ required: ["conditions"],
+ },
+ },
+ },
+ required: ["name"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const result = await createRule({
+ name: params.name,
+ description: params.description || "",
+ group_operator: params.group_operator || "AND",
+ status: params.status || "ACTIVE",
+ });
+ const ruleId = (result as any)?.id;
+ if (ruleId && params.condition_groups?.length > 0) {
+ await addConditionGroupsToRule(ruleId, params.condition_groups);
+ }
+ return { success: true, message: "Rule created", details: `Name: "${params.name}" | ID: ${ruleId} | Status: ${params.status || "ACTIVE"} | Conditions: ${params.condition_groups?.length || 0} groups` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to create rule" };
+ }
+ },
+ }),
+
+ update_rule: tool<any, any>({
+ description: "Update an existing rule's name, description, status, or operator.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ id: { type: "string", description: "Rule ID (UUID)" },
+ name: { type: "string" },
+ description: { type: "string" },
+ group_operator: { type: "string", enum: ["AND", "OR"] },
+ status: { type: "string", enum: ["ACTIVE", "INACTIVE"] },
+ },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ await updateRule(params.id, {
+ name: params.name,
+ description: params.description,
+ group_operator: params.group_operator,
+ status: params.status,
+ });
+ return { success: true, message: "Rule updated", details: `ID: ${params.id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to update rule" };
+ }
+ },
+ }),
+
+ delete_rule: tool<any, any>({
+ description: "Delete a rule and all its conditions and entity links.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Rule ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const [err] = await deleteRule(params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Rule deleted", details: `ID: ${params.id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to delete rule" };
+ }
+ },
+ }),
+
+ list_rules: tool<any, any>({
+ description: "List all rules in the Rule Engine.",
+ inputSchema: jsonSchema({ type: "object" as const, properties: {} }) as any,
+ execute: async () => {
+ try {
+ const { data, err } = await getRules();
+ if (err) return { success: false, error: String(err) };
+ const rules = (data as any[]) || [];
+ return { success: true, count: rules.length, rules: rules.map((r: any) => ({ id: r.id, name: r.name, status: r.status, description: r.description })) };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ get_rule: tool<any, any>({
+ description: "Get a rule's details including its condition groups and conditions.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Rule ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const result = await getRuleById(params.id);
+ if ((result as any).err) return { success: false, error: (result as any).err };
+ return { success: true, rule: (result as any).data };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ link_entity_to_rule: tool<any, any>({
+ description: "Link a context, prompt, or evaluation type to a rule. This associates the entity so it is used when the rule matches.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ rule_id: { type: "string", description: "Rule ID (UUID)" },
+ entity_type: { type: "string", enum: ["context", "prompt", "evaluation"], description: "Type of entity to link" },
+ entity_id: { type: "string", description: "Entity ID (UUID)" },
+ },
+ required: ["rule_id", "entity_type", "entity_id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ await addRuleEntity({
+ rule_id: params.rule_id,
+ entity_type: params.entity_type,
+ entity_id: params.entity_id,
+ });
+ return { success: true, message: `${params.entity_type} linked to rule`, details: `Rule: ${params.rule_id} → ${params.entity_type}: ${params.entity_id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to link entity" };
+ }
+ },
+ }),
+
+ unlink_entity_from_rule: tool<any, any>({
+ description: "Remove the link between an entity and a rule.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Rule entity link ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const [err] = await deleteRuleEntity(params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Entity unlinked from rule" };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ list_rule_entities: tool<any, any>({
+ description: "List entities (contexts, prompts, evaluations) linked to a rule.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ rule_id: { type: "string", description: "Rule ID to filter by" },
+ entity_type: { type: "string", enum: ["context", "prompt", "evaluation"] },
+ },
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const { data, err } = await getRuleEntities(params);
+ if (err) return { success: false, error: String(err) };
+ return { success: true, entities: data };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ // ==================== CONTEXT ====================
+
+ create_context: tool<any, any>({
+ description: "Create a new context document for rule-based evaluations.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ name: { type: "string", description: "Context name" },
+ content: { type: "string", description: "Context content/body text" },
+ description: { type: "string" },
+ tags: { type: "array", items: { type: "string" } },
+ status: { type: "string", enum: ["ACTIVE", "INACTIVE"] },
+ },
+ required: ["name", "content"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const result = await createContext({
+ name: params.name,
+ content: params.content,
+ description: params.description || "",
+ tags: JSON.stringify(params.tags || []),
+ status: params.status || "ACTIVE",
+ });
+ return { success: true, message: "Context created", details: `Name: "${params.name}" | ID: ${(result as any)?.id} | Status: ${params.status || "ACTIVE"}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to create context" };
+ }
+ },
+ }),
+
+ update_context: tool<any, any>({
+ description: "Update an existing context's name, content, description, tags, or status.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ id: { type: "string", description: "Context ID (UUID)" },
+ name: { type: "string" },
+ content: { type: "string" },
+ description: { type: "string" },
+ tags: { type: "array", items: { type: "string" } },
+ status: { type: "string", enum: ["ACTIVE", "INACTIVE"] },
+ },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const updateData: any = {};
+ if (params.name) updateData.name = params.name;
+ if (params.content) updateData.content = params.content;
+ if (params.description !== undefined) updateData.description = params.description;
+ if (params.tags) updateData.tags = JSON.stringify(params.tags);
+ if (params.status) updateData.status = params.status;
+ await updateContext(params.id, updateData);
+ return { success: true, message: "Context updated", details: `ID: ${params.id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to update context" };
+ }
+ },
+ }),
+
+ delete_context: tool<any, any>({
+ description: "Delete a context document.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Context ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const [err] = await deleteContext(params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Context deleted" };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ list_contexts: tool<any, any>({
+ description: "List all context documents.",
+ inputSchema: jsonSchema({ type: "object" as const, properties: {} }) as any,
+ execute: async () => {
+ try {
+ const { data, err } = await getContexts();
+ if (err) return { success: false, error: String(err) };
+ const contexts = (data as any[]) || [];
+ return { success: true, count: contexts.length, contexts: contexts.map((c: any) => ({ id: c.id, name: c.name, status: c.status, description: c.description })) };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ // ==================== PROMPTS ====================
+
+ create_prompt: tool<any, any>({
+ description: "Create a new versioned prompt in the Prompt Hub. Supports {{variableName}} for dynamic variables.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ name: { type: "string", description: "Unique prompt name" },
+ prompt: { type: "string", description: "Prompt content. Use {{variableName}} for variables." },
+ version: { type: "string", description: "Version e.g. 1.0.0" },
+ status: { type: "string", enum: ["PUBLISHED", "DRAFT"] },
+ tags: { type: "array", items: { type: "string" } },
+ },
+ required: ["name", "prompt"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const result = await createPrompt({
+ name: params.name,
+ prompt: params.prompt,
+ version: params.version || "1.0.0",
+ status: params.status || "DRAFT",
+ tags: params.tags || [],
+ metaProperties: {},
+ });
+ return { success: true, message: "Prompt created", details: `Name: "${params.name}" | Version: ${params.version || "1.0.0"} | Status: ${params.status || "DRAFT"} | ID: ${(result as any)?.data?.promptId}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to create prompt" };
+ }
+ },
+ }),
+
+ update_prompt_version: tool<any, any>({
+ description: "Update an existing prompt version or create a new version of an existing prompt.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ prompt_id: { type: "string", description: "Prompt ID (UUID)" },
+ version_id: { type: "string", description: "Version ID to update (omit to create new version)" },
+ prompt: { type: "string", description: "Updated prompt content" },
+ version: { type: "string", description: "Version string e.g. 2.0.0" },
+ status: { type: "string", enum: ["PUBLISHED", "DRAFT"] },
+ tags: { type: "array", items: { type: "string" } },
+ },
+ required: ["prompt_id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ await upsertPromptVersion({
+ promptId: params.prompt_id,
+ versionId: params.version_id,
+ prompt: params.prompt,
+ version: params.version,
+ status: params.status,
+ tags: params.tags,
+ metaProperties: {},
+ });
+ return { success: true, message: params.version_id ? "Prompt version updated" : "New prompt version created", details: `Prompt: ${params.prompt_id} | Version: ${params.version || "latest"}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to update prompt" };
+ }
+ },
+ }),
+
+ delete_prompt: tool<any, any>({
+ description: "Delete a prompt and all its versions.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Prompt ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const [err] = await deletePrompt(params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Prompt deleted" };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ list_prompts: tool<any, any>({
+ description: "List all prompts in the Prompt Hub.",
+ inputSchema: jsonSchema({ type: "object" as const, properties: {} }) as any,
+ execute: async () => {
+ try {
+ const { data, err } = await getPrompts();
+ if (err) return { success: false, error: String(err) };
+ const prompts = (data as any[]) || [];
+ return { success: true, count: prompts.length, prompts: prompts.map((p: any) => ({ id: p.id, name: p.name, totalVersions: p.totalVersions, latestVersion: p.latestVersion })) };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ // ==================== VAULT ====================
+
+ create_vault_secret: tool<any, any>({
+ description: "Store a new API key or credential in the Vault. Key name is auto-normalized to UPPER_SNAKE_CASE.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ key: { type: "string", description: "Secret name (auto-converted to UPPER_SNAKE_CASE)" },
+ value: { type: "string", description: "Secret value (e.g. API key)" },
+ tags: { type: "array", items: { type: "string" } },
+ },
+ required: ["key", "value"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const normalizedKey = normalizeVaultKey(params.key);
+ await upsertSecret({ key: normalizedKey, value: params.value, tags: params.tags || [] });
+ return { success: true, message: "Secret stored in Vault", details: `Key: "${normalizedKey}" | Tags: ${(params.tags || []).join(", ") || "none"}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to create secret" };
+ }
+ },
+ }),
+
+ update_vault_secret: tool<any, any>({
+ description: "Update an existing vault secret's key, value, or tags.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ id: { type: "string", description: "Secret ID (UUID)" },
+ key: { type: "string", description: "New key name (auto-converted to UPPER_SNAKE_CASE)" },
+ value: { type: "string", description: "New secret value" },
+ tags: { type: "array", items: { type: "string" } },
+ },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const updateData: any = { id: params.id };
+ if (params.key) updateData.key = normalizeVaultKey(params.key);
+ if (params.value) updateData.value = params.value;
+ if (params.tags) updateData.tags = params.tags;
+ await upsertSecret(updateData);
+ return { success: true, message: "Secret updated", details: `ID: ${params.id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to update secret" };
+ }
+ },
+ }),
+
+ delete_vault_secret: tool<any, any>({
+ description: "Delete a secret from the Vault.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Secret ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const [err] = await deleteSecret(params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Secret deleted" };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ list_vault_secrets: tool<any, any>({
+ description: "List all secrets in the Vault (values are hidden).",
+ inputSchema: jsonSchema({ type: "object" as const, properties: {} }) as any,
+ execute: async () => {
+ try {
+ const { data, err } = await getSecrets({});
+ if (err) return { success: false, error: String(err) };
+ const secrets = (data as any[]) || [];
+ return { success: true, count: secrets.length, secrets: secrets.map((s: any) => ({ id: s.id, key: s.key, tags: s.tags, created_at: s.created_at })) };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ // ==================== CUSTOM MODELS ====================
+
+ create_custom_model: tool<any, any>({
+ description: "Register a custom model with pricing for a provider.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ provider: { type: "string", description: "Provider ID: openai, anthropic, google, mistral, cohere, groq, etc." },
+ model_id: { type: "string", description: "Model API identifier e.g. gpt-4o-mini" },
+ display_name: { type: "string", description: "Human-readable name" },
+ model_type: { type: "string", enum: ["chat", "embeddings", "images", "audio"], description: "Type of model. Default: chat" },
+ context_window: { type: "number" },
+ input_price_per_m_tokens: { type: "number", description: "USD per million input tokens" },
+ output_price_per_m_tokens: { type: "number", description: "USD per million output tokens" },
+ capabilities: { type: "array", items: { type: "string" } },
+ },
+ required: ["provider", "model_id", "display_name"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const { data, err } = await createCustomModel(userId, databaseConfigId, {
+ provider: params.provider,
+ model_id: params.model_id,
+ displayName: params.display_name,
+ modelType: params.model_type || "chat",
+ contextWindow: params.context_window || 4096,
+ inputPricePerMToken: params.input_price_per_m_tokens || 0,
+ outputPricePerMToken: params.output_price_per_m_tokens || 0,
+ capabilities: params.capabilities || [],
+ });
+ if (err) return { success: false, error: err };
+ const modelId = (data as any)?.id || "";
+ return { success: true, message: "Custom model registered", id: modelId, details: `Name: "${params.display_name}" | Model ID: ${params.model_id} | Provider: ${params.provider} | Type: ${params.model_type || "chat"} | Context: ${params.context_window || 4096} | Input: $${params.input_price_per_m_tokens || 0}/M | Output: $${params.output_price_per_m_tokens || 0}/M` };
+ } catch (e: any) {
+ return { success: false, error: e.message || "Failed to create model" };
+ }
+ },
+ }),
+
+ update_custom_model: tool<any, any>({
+ description: "Update a custom model's display name, pricing, context window, or capabilities.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ id: { type: "string", description: "Custom model ID (UUID)" },
+ display_name: { type: "string" },
+ context_window: { type: "number" },
+ input_price_per_m_tokens: { type: "number" },
+ output_price_per_m_tokens: { type: "number" },
+ capabilities: { type: "array", items: { type: "string" } },
+ },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const updateData: any = {};
+ if (params.display_name) updateData.displayName = params.display_name;
+ if (params.context_window) updateData.contextWindow = params.context_window;
+ if (params.input_price_per_m_tokens !== undefined) updateData.inputPricePerMToken = params.input_price_per_m_tokens;
+ if (params.output_price_per_m_tokens !== undefined) updateData.outputPricePerMToken = params.output_price_per_m_tokens;
+ if (params.capabilities) updateData.capabilities = params.capabilities;
+ const { err } = await updateCustomModel(userId, databaseConfigId, params.id, updateData);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Model updated", id: params.id, details: `Name: "${params.display_name || "model"}" | ID: ${params.id}` };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ delete_custom_model: tool<any, any>({
+ description: "Delete a custom model.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: { id: { type: "string", description: "Custom model ID (UUID)" } },
+ required: ["id"],
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const { err } = await deleteCustomModel(userId, databaseConfigId, params.id);
+ if (err) return { success: false, error: err };
+ return { success: true, message: "Model deleted" };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ list_custom_models: tool<any, any>({
+ description: "List all custom models, optionally filtered by provider.",
+ inputSchema: jsonSchema({
+ type: "object" as const,
+ properties: {
+ provider: { type: "string", description: "Filter by provider ID (optional)" },
+ },
+ }) as any,
+ execute: async (params: any) => {
+ try {
+ const { data, err } = await getCustomModels(userId, databaseConfigId, params.provider);
+ if (err) return { success: false, error: err };
+ const models = (data as any[]) || [];
+ return { success: true, count: models.length, models: models.map((m: any) => ({ id: m.id, model_id: m.model_id, provider: m.provider, displayName: m.displayName })) };
+ } catch (e: any) {
+ return { success: false, error: e.message };
+ }
+ },
+ }),
+
+ };
+}
diff --git a/src/client/src/lib/platform/openground/custom-models-service.ts b/src/client/src/lib/platform/openground/custom-models-service.ts
index 8abe1761..6ae3dec8 100644
--- a/src/client/src/lib/platform/openground/custom-models-service.ts
+++ b/src/client/src/lib/platform/openground/custom-models-service.ts
@@ -34,6 +34,7 @@ export async function getCustomModels(
model_id,
provider,
display_name as displayName,
+ model_type as modelType,
context_window as contextWindow,
input_price_per_m_token as inputPricePerMToken,
output_price_per_m_token as outputPricePerMToken,
@@ -53,23 +54,9 @@ export async function getCustomModels(
return { err: err as string };
}
- // Filter out invalid UUIDs
- const invalidIds: string[] = [];
- const validModels = (data as any[] || []).filter((model: any) => {
- const isValid = model.id && isValidUUID(model.id);
- if (!isValid) {
- invalidIds.push(model.id || 'null');
- }
- return isValid;
- });
-
- // Log invalid IDs for cleanup
- if (invalidIds.length > 0) {
- // Use separate parameters to prevent log injection
- console.warn('Found', invalidIds.length, 'models with invalid UUIDs:', invalidIds);
- }
-
- return { data: validModels as CustomModel[] };
+ // Return all models — don't filter by UUID validity since ClickHouse
+ // may store UUIDs in non-standard string format depending on insert method
+ return { data: (data as CustomModel[]) || [] };
}
/**
@@ -119,6 +106,7 @@ export async function createCustomModel(
provider: input.provider,
model_id: input.model_id,
display_name: input.displayName,
+ model_type: input.modelType || "chat",
context_window: input.contextWindow || 4096,
input_price_per_m_token: input.inputPricePerMToken || 0,
output_price_per_m_token: input.outputPricePerMToken || 0,
@@ -143,6 +131,7 @@ export async function createCustomModel(
model_id,
provider,
display_name as displayName,
+ model_type as modelType,
context_window as contextWindow,
input_price_per_m_token as inputPricePerMToken,
output_price_per_m_token as outputPricePerMToken,
@@ -178,17 +167,15 @@ export async function updateCustomModel(
id: string,
input: Partial<CustomModelInput>
): Promise<{ data?: boolean; err?: string }> {
- // Validate UUID format
- if (!isValidUUID(id)) {
- return { err: `Invalid UUID format: ${id}` };
- }
-
// Build update fields
const updateFields: string[] = [];
if (input.displayName !== undefined) {
updateFields.push(`display_name = '${Sanitizer.sanitizeValue(input.displayName)}'`);
}
+ if (input.modelType !== undefined) {
+ updateFields.push(`model_type = '${Sanitizer.sanitizeValue(input.modelType)}'`);
+ }
if (input.contextWindow !== undefined) {
updateFields.push(`context_window = ${input.contextWindow}`);
}
@@ -214,7 +201,7 @@ export async function updateCustomModel(
const updateQuery = `
ALTER TABLE ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}
UPDATE ${updateFields.join(", ")}
- WHERE id = toUUID('${Sanitizer.sanitizeValue(id)}')
+ WHERE (toString(id) = '${Sanitizer.sanitizeValue(id)}' OR model_id = '${Sanitizer.sanitizeValue(id)}')
AND created_by_user_id = '${Sanitizer.sanitizeValue(userId)}'
AND database_config_id = '${Sanitizer.sanitizeValue(databaseConfigId)}'
`;
@@ -240,14 +227,9 @@ export async function deleteCustomModel(
databaseConfigId: string,
id: string
): Promise<{ data?: boolean; err?: string }> {
- // Validate UUID format
- if (!isValidUUID(id)) {
- return { err: `Invalid UUID format: ${id}` };
- }
-
const deleteQuery = `
DELETE FROM ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}
- WHERE id = toUUID('${Sanitizer.sanitizeValue(id)}')
+ WHERE (toString(id) = '${Sanitizer.sanitizeValue(id)}' OR model_id = '${Sanitizer.sanitizeValue(id)}')
AND created_by_user_id = '${Sanitizer.sanitizeValue(userId)}'
AND database_config_id = '${Sanitizer.sanitizeValue(databaseConfigId)}'
`;
diff --git a/src/client/src/middleware.ts b/src/client/src/middleware.ts
index ed258138..42f57c4f 100644
--- a/src/client/src/middleware.ts
+++ b/src/client/src/middleware.ts
@@ -26,6 +26,8 @@ export const config = {
"/dashboards/:path*",
"/d/:path*",
"/fleet-hub",
- "/onboarding"
+ "/onboarding",
+ "/chat",
+ "/chat/:path*"
],
};
diff --git a/src/client/src/selectors/chat.ts b/src/client/src/selectors/chat.ts
new file mode 100644
index 00000000..09c56308
--- /dev/null
+++ b/src/client/src/selectors/chat.ts
@@ -0,0 +1,27 @@
+import { RootStore } from "@/types/store/root";
+
+export const getChatConversations = (state: RootStore) => state.chat.conversations;
+export const getChatActiveId = (state: RootStore) => state.chat.activeConversationId;
+export const getChatMessages = (state: RootStore) => state.chat.messages;
+export const getChatHasConfig = (state: RootStore) => state.chat.hasConfig;
+export const getChatConfigInfo = (state: RootStore) => state.chat.configInfo;
+export const getChatIsLoadingConversations = (state: RootStore) => state.chat.isLoadingConversations;
+export const getChatIsLoadingConfig = (state: RootStore) => state.chat.isLoadingConfig;
+export const getChatIsStreaming = (state: RootStore) => state.chat.isStreaming;
+
+export const getChatActions = (state: RootStore) => ({
+ setConversations: state.chat.setConversations,
+ setActiveConversationId: state.chat.setActiveConversationId,
+ setMessages: state.chat.setMessages,
+ updateLastMessage: state.chat.updateLastMessage,
+ addMessage: state.chat.addMessage,
+ setHasConfig: state.chat.setHasConfig,
+ setConfigInfo: state.chat.setConfigInfo,
+ setIsLoadingConversations: state.chat.setIsLoadingConversations,
+ setIsLoadingConfig: state.chat.setIsLoadingConfig,
+ setIsStreaming: state.chat.setIsStreaming,
+ updateConversation: state.chat.updateConversation,
+ addConversation: state.chat.addConversation,
+ removeConversation: state.chat.removeConversation,
+ reset: state.chat.reset,
+});
diff --git a/src/client/src/store/chat.ts b/src/client/src/store/chat.ts
new file mode 100644
index 00000000..1f5d11ef
--- /dev/null
+++ b/src/client/src/store/chat.ts
@@ -0,0 +1,77 @@
+"use client";
+import { ChatStore } from "@/types/store/chat";
+import { lens } from "@dhmk/zustand-lens";
+
+export const chatStoreSlice: ChatStore = lens((setStore, getStore) => ({
+ conversations: [],
+ activeConversationId: null,
+ messages: [],
+ hasConfig: false,
+ configInfo: null,
+ isLoadingConversations: true,
+ isLoadingConfig: true,
+ isStreaming: false,
+
+ setConversations: (conversations) =>
+ setStore({ conversations }),
+
+ setActiveConversationId: (id) =>
+ setStore({ activeConversationId: id }),
+
+ setMessages: (messages) =>
+ setStore({ messages }),
+
+ updateLastMessage: (content) => {
+ const messages = [...getStore().messages];
+ const lastIdx = messages.length - 1;
+ if (lastIdx >= 0 && messages[lastIdx].role === "assistant") {
+ messages[lastIdx] = { ...messages[lastIdx], content };
+ setStore({ messages });
+ }
+ },
+
+ addMessage: (message) =>
+ setStore({ messages: [...getStore().messages, message] }),
+
+ setHasConfig: (hasConfig) =>
+ setStore({ hasConfig }),
+
+ setConfigInfo: (configInfo) =>
+ setStore({ configInfo }),
+
+ setIsLoadingConversations: (isLoadingConversations) =>
+ setStore({ isLoadingConversations }),
+
+ setIsLoadingConfig: (isLoadingConfig) =>
+ setStore({ isLoadingConfig }),
+
+ setIsStreaming: (isStreaming) =>
+ setStore({ isStreaming }),
+
+ updateConversation: (id, updates) => {
+ const conversations = getStore().conversations.map((c) =>
+ c.id === id ? { ...c, ...updates } : c
+ );
+ setStore({ conversations });
+ },
+
+ addConversation: (conversation) =>
+ setStore({ conversations: [conversation, ...getStore().conversations] }),
+
+ removeConversation: (id) =>
+ setStore({
+ conversations: getStore().conversations.filter((c) => c.id !== id),
+ }),
+
+ reset: () =>
+ setStore({
+ conversations: [],
+ activeConversationId: null,
+ messages: [],
+ hasConfig: false,
+ configInfo: null,
+ isLoadingConversations: true,
+ isLoadingConfig: true,
+ isStreaming: false,
+ }),
+}));
diff --git a/src/client/src/store/index.ts b/src/client/src/store/index.ts
index ed94962b..e5ec7780 100644
--- a/src/client/src/store/index.ts
+++ b/src/client/src/store/index.ts
@@ -10,6 +10,7 @@ import { RootStore } from "@/types/store/root";
import { dashboardStoreSlice } from "./dashboards";
import { organisationStoreSlice } from "./organisation";
import { ruleEngineStoreSlice } from "./rule-engine";
+import { chatStoreSlice } from "./chat";
export const useRootStore = create<RootStore>()(
devtools(
@@ -22,6 +23,7 @@ export const useRootStore = create<RootStore>()(
dashboards: dashboardStoreSlice,
organisation: organisationStoreSlice,
ruleEngine: ruleEngineStoreSlice,
+ chat: chatStoreSlice,
})
)
);
diff --git a/src/client/src/types/openground.ts b/src/client/src/types/openground.ts
index e6d054c8..8b13866c 100644
--- a/src/client/src/types/openground.ts
+++ b/src/client/src/types/openground.ts
@@ -31,6 +31,7 @@ export interface OpengroundStats {
export interface ModelMetadata {
id: string;
displayName: string;
+ modelType?: string;
contextWindow: number;
inputPricePerMToken: number; // Price per million tokens
outputPricePerMToken: number;
@@ -67,10 +68,13 @@ export interface CustomModel extends ModelMetadata {
provider?: string;
}
+export type ModelType = "chat" | "embeddings" | "images" | "audio";
+
export interface CustomModelInput {
provider: string;
model_id: string;
displayName: string;
+ modelType?: ModelType;
contextWindow?: number;
inputPricePerMToken?: number;
outputPricePerMToken?: number;
diff --git a/src/client/src/types/store/chat.ts b/src/client/src/types/store/chat.ts
new file mode 100644
index 00000000..71afde88
--- /dev/null
+++ b/src/client/src/types/store/chat.ts
@@ -0,0 +1,61 @@
+export interface ChatConversation {
+ id: string;
+ title: string;
+ totalCost: number;
+ totalMessages: number;
+ totalPromptTokens?: number;
+ totalCompletionTokens?: number;
+ updatedAt: string;
+}
+
+export interface ChatMessage {
+ id?: string;
+ role: "user" | "assistant";
+ content: string;
+ promptTokens?: number;
+ completionTokens?: number;
+ cost?: number;
+ queryRowsRead?: number;
+ queryExecutionTimeMs?: number;
+ createdAt?: string;
+}
+
+export interface ChatConfigInfo {
+ providerName?: string;
+ modelName?: string;
+ modelId?: string;
+ inputPricePerMToken?: number;
+ outputPricePerMToken?: number;
+ contextWindow?: number;
+}
+
+export type ChatStore = {
+ conversations: ChatConversation[];
+ activeConversationId: string | null;
+ messages: ChatMessage[];
+ hasConfig: boolean;
+ configInfo: ChatConfigInfo | null;
+ isLoadingConversations: boolean;
+ isLoadingConfig: boolean;
+ isStreaming: boolean;
+
+ setConversations: (conversations: ChatConversation[]) => void;
+ setActiveConversationId: (id: string | null) => void;
+ setMessages: (messages: ChatMessage[]) => void;
+ updateLastMessage: (content: string) => void;
+ addMessage: (message: ChatMessage) => void;
+ setHasConfig: (hasConfig: boolean) => void;
+ setConfigInfo: (info: ChatConfigInfo | null) => void;
+ setIsLoadingConversations: (loading: boolean) => void;
+ setIsLoadingConfig: (loading: boolean) => void;
+ setIsStreaming: (streaming: boolean) => void;
+
+ // Update a single conversation in the list (for title/token updates)
+ updateConversation: (id: string, updates: Partial<ChatConversation>) => void;
+ // Add a conversation to the top of the list
+ addConversation: (conversation: ChatConversation) => void;
+ // Remove a conversation from the list
+ removeConversation: (id: string) => void;
+
+ reset: () => void;
+};
diff --git a/src/client/src/types/store/root.ts b/src/client/src/types/store/root.ts
index 6190bcd6..63b92283 100644
--- a/src/client/src/types/store/root.ts
+++ b/src/client/src/types/store/root.ts
@@ -6,6 +6,7 @@ import { PageStore } from "@/types/store/page";
import { DashboardStore } from "./dashboards";
import { OrganisationStore } from "./organisation";
import { RuleEngineStore } from "./rule-engine";
+import { ChatStore } from "./chat";
export type RootStore = {
user: UserStore;
@@ -16,4 +17,5 @@ export type RootStore = {
dashboards: DashboardStore;
organisation: OrganisationStore;
ruleEngine: RuleEngineStore;
+ chat: ChatStore;
};
diff --git a/src/client/src/utils/breadcrumbs.ts b/src/client/src/utils/breadcrumbs.ts
index 4c471a79..e195282c 100644
--- a/src/client/src/utils/breadcrumbs.ts
+++ b/src/client/src/utils/breadcrumbs.ts
@@ -250,6 +250,22 @@ export const ROUTE_CONFIGS: RouteConfig[] = [
{ title: "Fleet Hub", href: "/fleet-hub" }
],
},
+
+ // Chat
+ {
+ regex: /^\/chat$/,
+ getTitle: () => getMessage().CHAT_TITLE,
+ getBreadcrumbs: () => [],
+ getDescription: () => getMessage().CHAT_DESCRIPTION,
+ },
+ {
+ regex: /^\/chat\/settings$/,
+ getTitle: () => getMessage().CHAT_SETTINGS_LINK,
+ getBreadcrumbs: () => [
+ { title: getMessage().CHAT_TITLE, href: "/chat" }
+ ],
+ getDescription: () => getMessage().CHAT_SETTINGS_DESCRIPTION,
+ },
];
// Main function to generate page header based on pathname
diff --git a/src/client/tailwind.config.ts b/src/client/tailwind.config.ts
index 15f602e8..a925f2d6 100644
--- a/src/client/tailwind.config.ts
+++ b/src/client/tailwind.config.ts
@@ -59,12 +59,17 @@ const config = {
height: "0",
},
},
+ blink: {
+ "0%, 100%": { opacity: "1" },
+ "50%": { opacity: "0" },
+ },
},
animation: {
"collapsible-down": "collapsible-down 0.2s ease-out",
"collapsible-up": "collapsible-up 0.2s ease-out",
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
+ blink: "blink 1s steps(1) infinite",
},
},
},