Files
Byron.wang ca3d00229e Feat/Implement structured logging and Trace ID propagation (#552)
* use slog instead of log package and format to new log schema

* update the environment name to LOG_OUTPUT_FORMAT

* add the env to .env.example

* fix log reference error

* change the order of milldlewares

* delete unused code

* fix the concurrently session potential race condition

* fix the log format in tests

* update the duplicate code

* refactor: convert log functions to slog structured format

- Change log.Error/Info/Warn/Debug/Panic to accept msg + key-value pairs
- Remove printf-style formatting from log functions
- Update log calls in internal/cluster, internal/db, internal/core/session_manager
- Remove unused 'initialized' variable from log package
- Remaining files will be updated in follow-up commits

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: update all log call sites to use slog structured format

Convert all log.Error, log.Info, log.Warn, log.Debug, and log.Panic
calls from printf-style formatting to slog key-value pairs.

Before: log.Error("failed to do something: %s", err.Error())
After:  log.Error("failed to do something", "error", err)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: update cmd/ log calls to use slog structured format

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat: implement GnetLogger for structured logging in gnet

* refactor: remove deprecated log visibility functions and related calls

* feat: enhance session management with trace and identity context propagation

* feat: implement serverless transaction handler and writer for plugin runtime

* refactor: rename context field to traceCtx in RealBackwardsInvocation

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Yeuoly <admin@srmxy.cn>
2025-12-30 11:00:48 +08:00

129 lines
3.2 KiB
Go

package bundle
import (
"bytes"
_ "embed"
"errors"
"os"
"path"
"text/template"
"time"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/bundle_entities"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/manifest_entities"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
tea "github.com/charmbracelet/bubbletea"
)
//go:embed templates/icon.svg
var BUNDLE_ICON []byte
//go:embed templates/README.md
var BUNDLE_README []byte
func generateNewBundle() (*bundle_entities.Bundle, error) {
m := newProfile()
p := tea.NewProgram(m)
if result, err := p.Run(); err != nil {
return nil, err
} else {
if _, ok := result.(profile); ok {
author := m.inputs[1].Value()
name := m.inputs[0].Value()
description := m.inputs[2].Value()
bundle := &bundle_entities.Bundle{
Name: name,
Icon: "icon.svg",
Labels: plugin_entities.NewI18nObject(name),
Description: plugin_entities.NewI18nObject(description),
Version: "0.0.1",
Author: author,
Type: manifest_entities.BundleType,
Dependencies: []bundle_entities.Dependency{},
}
return bundle, nil
} else {
return nil, errors.New("invalid profile")
}
}
}
func InitBundle() {
bundle, err := generateNewBundle()
if err != nil {
log.Error("failed to generate new bundle", "error", err)
return
}
// create bundle directory
cwd, err := os.Getwd()
if err != nil {
log.Error("error getting current directory", "error", err)
return
}
bundleDir := path.Join(cwd, bundle.Name)
if err := os.MkdirAll(bundleDir, 0755); err != nil {
log.Error("error creating bundle directory", "error", err)
return
}
success := false
defer func() {
if !success {
os.RemoveAll(bundleDir)
}
}()
// save
bundleYaml := marshalYamlBytes(bundle)
if err := os.WriteFile(path.Join(bundleDir, "manifest.yaml"), bundleYaml, 0644); err != nil {
log.Error("error saving manifest.yaml", "error", err)
return
}
// create README.md
tmpl := template.Must(template.New("README").Parse(string(BUNDLE_README)))
// render the template
var buf bytes.Buffer
if err := tmpl.Execute(&buf, map[string]interface{}{
"Author": bundle.Author,
"Version": bundle.Version,
"Date": time.Now().Format(time.DateOnly),
}); err != nil {
log.Error("error rendering README template", "error", err)
return
}
// save README.md
if err := os.WriteFile(path.Join(bundleDir, "README.md"), buf.Bytes(), 0644); err != nil {
log.Error("error saving README.md", "error", err)
return
}
// create _assets directory
if err := os.MkdirAll(path.Join(bundleDir, "_assets"), 0755); err != nil {
log.Error("error creating _assets directory", "error", err)
return
}
// create _assets/icon.svg
if err := os.WriteFile(path.Join(bundleDir, "_assets", "icon.svg"), BUNDLE_ICON, 0644); err != nil {
log.Error("error saving icon.svg", "error", err)
return
}
// create .github/workflows/plugin-publish.yml
if err := os.MkdirAll(path.Join(bundleDir, ".github", "workflows"), 0755); err != nil {
log.Error("error creating .github/workflows directory", "error", err)
return
}
success = true
log.Info("bundle created successfully", "directory", bundleDir)
}