• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kshard / thinker / 15083532464

17 May 2025 08:42AM UTC coverage: 33.807% (-24.4%) from 58.222%
15083532464

Pull #20

github

fogfish
update license
Pull Request #20: establish agent taxonomy - prompter, manifold, automata

241 of 612 new or added lines in 28 files covered. (39.38%)

144 existing lines in 5 files now uncovered.

381 of 1127 relevant lines covered (33.81%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.13
/command/softcmd/python.go
1
//
2
// Copyright (C) 2025 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/kshard/thinker
7
//
8

9
package softcmd
10

11
import (
12
        "bytes"
13
        "fmt"
14
        "os"
15
        "os/exec"
16
        "path/filepath"
17

18
        "github.com/kshard/chatter"
19
        "github.com/kshard/thinker"
20
)
21

22
// A unique name for bash (the shell)
23
const PYTHON = "python"
24

25
// Create new python command, defining working dir
26
func Python(dir string) Cmd {
1✔
27
        return Cmd{
1✔
28
                Cmd:    PYTHON,
1✔
29
                About:  "Use python to execute scripts that help you complete your task. Enclose the python code in <codeblock> tags.",
1✔
30
                Syntax: `python <codeblock>source code</codeblock>`,
1✔
31
                Run:    python(dir),
1✔
32
        }
1✔
33
}
1✔
34

35
func pySetup(dir string) error {
1✔
36
        pyenv := filepath.Join(dir, ".venv")
1✔
37
        if _, err := os.Stat(pyenv); err != nil {
2✔
38
                setup := exec.Command("python3", "-m", "venv", ".venv")
1✔
39
                setup.Dir = dir
1✔
40

1✔
41
                _, err := setup.Output()
1✔
42
                if err != nil {
1✔
NEW
43
                        return err
×
NEW
44
                }
×
45

46
                pipreqs := exec.Command(filepath.Join(pyenv, "bin/python"), "-m", "pip", "install", "pigar")
1✔
47
                pipreqs.Dir = dir
1✔
48

1✔
49
                _, err = pipreqs.Output()
1✔
50
                if err != nil {
1✔
NEW
51
                        return err
×
NEW
52
                }
×
53
        }
54

55
        return nil
1✔
56
}
57

58
func pyDeps(dir string) error {
1✔
59
        pyenv := filepath.Join(dir, ".venv")
1✔
60

1✔
61
        deps := exec.Command(filepath.Join(pyenv, "bin/pigar"), "generate", "--question-answer", "yes", "--auto-select")
1✔
62
        deps.Dir = dir
1✔
63

1✔
64
        _, err := deps.Output()
1✔
65
        if err != nil {
1✔
NEW
66
                return err
×
NEW
67
        }
×
68

69
        pip := exec.Command(filepath.Join(pyenv, "bin/python"), "-m", "pip", "install", "-r", "requirements.txt")
1✔
70
        pip.Dir = dir
1✔
71

1✔
72
        _, err = pip.Output()
1✔
73
        if err != nil {
1✔
NEW
74
                return err
×
NEW
75
        }
×
76

77
        return nil
1✔
78
}
79

80
func pyfile(dir, code string) (string, error) {
1✔
81
        fd, err := os.CreateTemp(dir, "job-*.py")
1✔
82
        if err != nil {
1✔
NEW
83
                return "", err
×
NEW
84
        }
×
85
        defer fd.Close()
1✔
86

1✔
87
        _, err = fd.WriteString(code)
1✔
88
        if err != nil {
1✔
NEW
89
                return "", err
×
NEW
90
        }
×
91

92
        return fd.Name(), nil
1✔
93
}
94

95
func python(dir string) func(*chatter.Reply) (float64, CmdOut, error) {
1✔
96
        return func(command *chatter.Reply) (float64, CmdOut, error) {
2✔
97
                if err := pySetup(dir); err != nil {
1✔
NEW
98
                        return 0.0, CmdOut{}, err
×
NEW
99
                }
×
100

101
                code, err := CodeBlock(PYTHON, command.String())
1✔
102
                if err != nil {
1✔
NEW
103
                        return 0.00, CmdOut{Cmd: PYTHON}, err
×
NEW
104
                }
×
105

106
                file, err := pyfile(dir, code)
1✔
107
                if err != nil {
1✔
NEW
108
                        return 0.00, CmdOut{Cmd: PYTHON}, err
×
NEW
109
                }
×
110

111
                if err := pyDeps(dir); err != nil {
1✔
NEW
112
                        return 0.0, CmdOut{}, err
×
NEW
113
                }
×
114

115
                pyenv := filepath.Join(dir, ".venv")
1✔
116
                cmd := exec.Command(filepath.Join(pyenv, "bin/python"), file)
1✔
117
                var stdout bytes.Buffer
1✔
118
                var stderr bytes.Buffer
1✔
119
                cmd.Dir = dir
1✔
120
                cmd.Stdout = &stdout
1✔
121
                cmd.Stderr = &stderr
1✔
122

1✔
123
                if err := cmd.Run(); err != nil {
1✔
NEW
124
                        err = thinker.Feedback(
×
NEW
125
                                fmt.Sprintf("The TOOL:%s has failed, improve the response based on feedback:", PYTHON),
×
NEW
126

×
NEW
127
                                `Strictly adhere python code formatting, use \t, \n where is needed`,
×
NEW
128
                                "Execution of python script is failed with the error: "+err.Error(),
×
NEW
129
                                "The error output is "+stderr.String(),
×
NEW
130
                        )
×
NEW
131
                        return 0.05, CmdOut{Cmd: PYTHON}, err
×
NEW
132
                }
×
133

134
                return 1.0, CmdOut{Cmd: PYTHON, Output: stdout.String()}, nil
1✔
135
        }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc