Files
LocalAI/addons/office/internal/tools/create.go
T
mudler-agentandEttore Di Giacinto 16acc8926b feat(addons): simplify Office and discover Tables (#13029)
* feat(tables): list accessible databases

Let agents discover existing databases through the host gateway.
Keep listing actor-scoped and separate from executable tools.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* test(tables): verify database listing authority

Exercise model discovery through the real owner and carrier.
Check shared access, revocation, denial and resource limits.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* feat(tables): bundle database search guidance

Teach agents to discover existing databases before querying records.
Package and verify the skill with the Tables add-on.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* test(tables): cover packaged chat skill gates

Load the real receipt-verified Tables instructions through the temporary
Studio workspace. Check the index and load refusal after runtime, chat
switch and saved-agent grant changes.

This fixture starts at the receipt and broker boundary; packaging tests
cover artifact construction and integrity separately.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* feat(office): parse Markdown and CSV inputs

Convert bounded text inputs into document and workbook structures.
Render supported Markdown formatting without fetching external content.
Preserve spreadsheet fields as strings rather than inferring formulas.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* fix(office): preserve list markers, breaks and PDF page margins

Cover actual DOCX breaks and tabs, explicit zero list starts, empty list rejection, and PDF margin/pagination regressions.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* fix(office): guard Markdown parsing work

Check cancellation and nesting during parser callbacks.
Validate table dimensions before padded table allocation.
Keep parser state isolated to each conversion.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* fix(office): recognize tables before applying limits

Leave mismatched table-like paragraphs as ordinary Markdown.
Keep oversized real tables bounded before allocation.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* feat(office): simplify document creation inputs

Accept Markdown for DOCX and PDF, and CSV for single-sheet XLSX files.
Replace nested creation schemas to reduce model tool grammar complexity.
Keep existing read, edit and PowerPoint behavior and internal office IDs.

Present the add-on as Simple Office and document the compatibility change.
Keep same-style Markdown fragments editable in generated DOCX files.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* fix(ci): align add-on fixtures and test inventories

Tables fixtures must carry the skill their manifest declares. Preserve
its bytes in package tests and mark only binaries executable in UI
fixtures. Assert that installation keeps the shipped skill intact.

Register the new Tables tests and the connector case from master in
exact suite inventories. Include the new database read in tool counts.

Run Office regressions through Ginkgo and check reader close errors.
Keep all document assertions and the existing CI gates intact.

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Assisted-by: nib:gpt-6.1-sol

* fix(ci): restore Office and packaging checks

Keep the generated Office label consistent with Simple Office. Install
Poppler for the PDF layout regressions on Linux and macOS.

Separate successful runtime setup from the 300 ms deadline under test.
Keep the timeout, cancellation, stop and admission assertions intact.

Refresh the packaging inventory from all 209 executed cases, retaining
repeated platform checks and the Office and Tables skill checks.

Assisted-by: nib:gpt-6.1-sol
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
2026-11-02 12:48:08 +01:00

184 lines
5.2 KiB
Go

// SPDX-License-Identifier: MIT
package tools
import (
"bytes"
"encoding/json"
"fmt"
"github.com/mudler/LocalAI/addons/office/internal/deck"
"github.com/mudler/LocalAI/addons/office/internal/deck/pptx"
"github.com/mudler/LocalAI/addons/office/internal/docx"
"github.com/mudler/LocalAI/addons/office/internal/pdf"
"github.com/mudler/LocalAI/addons/office/internal/textinput"
"github.com/mudler/LocalAI/addons/office/internal/xlsx"
)
// A created file is saved as a new file (version zero). If the path is taken,
// the store keeps the old file and the new one is saved next to it.
// decodeTextCreation is deliberately local to the three string creation tools.
// Bound raw JSON before decoding, and never echo decoder errors or user content.
func decodeTextCreation(raw json.RawMessage, v any, field, legacy string) error {
if len(raw) > 1<<20 {
return refuse("arguments exceed 1 MiB; shorten %s", field)
}
var fields map[string]json.RawMessage
if err := json.Unmarshal(raw, &fields); err != nil {
return refuse("arguments must be a JSON object with path and %s strings", field)
}
if _, ok := fields[legacy]; ok {
return refuse("%s is no longer supported; use the %s string", legacy, field)
}
for _, key := range []string{"path", field, "title", "sheet"} {
value, ok := fields[key]
if !ok {
continue
}
var text string
if bytes.Equal(bytes.TrimSpace(value), []byte("null")) || json.Unmarshal(value, &text) != nil {
return refuse("%s must be a string", key)
}
}
if err := decode(raw, v); err != nil {
return refuse("use only path, %s and the optional metadata strings", field)
}
return nil
}
func (w *work) createDocx(raw json.RawMessage) (any, error) {
var a struct {
pathArgs
Title string `json:"title"`
Markdown string `json:"markdown"`
}
if err := decodeTextCreation(raw, &a, "markdown", "blocks"); err != nil {
return nil, err
}
if err := checkPath(a.Path, ".docx"); err != nil {
return nil, err
}
d, err := textinput.ParseMarkdown(w.ctx, a.Markdown)
if err := w.ctx.Err(); err != nil {
return nil, err
}
if err != nil {
return nil, refuse("markdown: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
var buf bytes.Buffer
if err := docx.Write(&buf, docx.Document{Title: a.Title, Blocks: d.Docx}); err != nil {
return nil, refuse("the document cannot be written: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
return w.store(a.Path, ".docx", buf.Bytes(), 0)
}
func (w *work) createXlsx(raw json.RawMessage) (any, error) {
var a struct {
pathArgs
Title string `json:"title"`
CSV string `json:"csv"`
Sheet string `json:"sheet"`
}
if err := decodeTextCreation(raw, &a, "csv", "sheets"); err != nil {
return nil, err
}
if err := checkPath(a.Path, ".xlsx"); err != nil {
return nil, err
}
wb, err := textinput.ParseCSV(w.ctx, a.CSV, a.Sheet, a.Title)
if err := w.ctx.Err(); err != nil {
return nil, err
}
if err != nil {
return nil, refuse("csv: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
var buf bytes.Buffer
if err := xlsx.Write(&buf, wb); err != nil {
return nil, refuse("the workbook cannot be written: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
return w.store(a.Path, ".xlsx", buf.Bytes(), 0)
}
func (w *work) createPptx(raw json.RawMessage) (any, error) {
var a struct {
pathArgs
Title string `json:"title"`
Slides []struct {
Title string `json:"title"`
Bullets []string `json:"bullets"`
} `json:"slides"`
}
if err := decode(raw, &a); err != nil {
return nil, err
}
if err := checkPath(a.Path, ".pptx"); err != nil {
return nil, err
}
if len(a.Slides) == 0 || len(a.Slides) > 200 {
return nil, refuse("slides must hold 1 to 200 entries")
}
var slides []deck.StudioSlide
for i, s := range a.Slides {
sl := deck.StudioSlide{
ID: fmt.Sprintf("s%d", i+1), Title: s.Title, Layout: deck.LayoutText,
Bullets: []deck.StudioBullet{}, Sources: []string{}, Status: deck.SlideOK,
}
if len(s.Bullets) == 0 {
sl.Layout = deck.LayoutTitle
}
for _, b := range s.Bullets {
sl.Bullets = append(sl.Bullets, deck.StudioBullet{Text: b, Evidence: []string{}})
}
slides = append(slides, sl)
}
var buf bytes.Buffer
if err := pptx.Write(&buf, slides, pptx.Options{Title: a.Title, Style: deck.StylePaper}, nil); err != nil {
return nil, refuse("the deck cannot be written: %v", err)
}
return w.store(a.Path, ".pptx", buf.Bytes(), 0)
}
func (w *work) createPdf(raw json.RawMessage) (any, error) {
var a struct {
pathArgs
Title string `json:"title"`
Markdown string `json:"markdown"`
}
if err := decodeTextCreation(raw, &a, "markdown", "blocks"); err != nil {
return nil, err
}
if err := checkPath(a.Path, ".pdf"); err != nil {
return nil, err
}
d, err := textinput.ParseMarkdown(w.ctx, a.Markdown)
if err := w.ctx.Err(); err != nil {
return nil, err
}
if err != nil {
return nil, refuse("markdown: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
data, err := pdf.Create(pdf.Spec{Title: a.Title, Pages: []pdf.Page{{Blocks: d.PDF}}})
if err != nil {
return nil, refuse("the PDF cannot be written: %v", err)
}
if err := w.ctx.Err(); err != nil {
return nil, err
}
return w.store(a.Path, ".pdf", data, 0)
}