|
1 | 1 | package server |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "net/http" |
@@ -509,7 +510,49 @@ func (g *gatedProvider) Execute(name, args string) (string, error) { |
509 | 510 | } |
510 | 511 | } |
511 | 512 | } |
512 | | - return g.inner.Execute(name, args) |
| 513 | + res, err := g.inner.Execute(name, args) |
| 514 | + // Surface a created document in the GUI like generated media (path + inline |
| 515 | + // download), otherwise the user is told "created" but never sees the file. |
| 516 | + if err == nil && name == "create_document" && g.emit != nil { |
| 517 | + g.emitDocument(res) |
| 518 | + } |
| 519 | + return res, err |
| 520 | +} |
| 521 | + |
| 522 | +// emitDocument reads the just-created document and emits a media_generated event |
| 523 | +// so the GUI shows a clickable/downloadable card. |
| 524 | +func (g *gatedProvider) emitDocument(result string) { |
| 525 | + var r struct { |
| 526 | + Path string `json:"path"` |
| 527 | + } |
| 528 | + if json.Unmarshal([]byte(result), &r) != nil || r.Path == "" { |
| 529 | + return |
| 530 | + } |
| 531 | + data, err := os.ReadFile(r.Path) |
| 532 | + if err != nil || len(data) > 25*1024*1024 { |
| 533 | + // Still tell the UI where it is, even if too big to inline. |
| 534 | + g.emit("media_generated", map[string]interface{}{"kind": "document", "path": r.Path}) |
| 535 | + return |
| 536 | + } |
| 537 | + mime := "application/octet-stream" |
| 538 | + switch strings.ToLower(filepath.Ext(r.Path)) { |
| 539 | + case ".pdf": |
| 540 | + mime = "application/pdf" |
| 541 | + case ".docx": |
| 542 | + mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" |
| 543 | + case ".txt", ".md": |
| 544 | + mime = "text/plain" |
| 545 | + case ".html": |
| 546 | + mime = "text/html" |
| 547 | + case ".csv": |
| 548 | + mime = "text/csv" |
| 549 | + case ".json": |
| 550 | + mime = "application/json" |
| 551 | + } |
| 552 | + g.emit("media_generated", map[string]interface{}{ |
| 553 | + "kind": "document", "path": r.Path, "mime": mime, |
| 554 | + "data_uri": "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data), |
| 555 | + }) |
513 | 556 | } |
514 | 557 |
|
515 | 558 | func gatedSummary(name, args string) string { |
|
0 commit comments