commandline

package module
v0.0.0-...-20b2a55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 17

README

CommandLine Tool

CommandLine Tools implementation for Eino that implements the Tool interface. This enables seamless integration with Eino's LLM capabilities for enhanced natural language processing and generation.

Note: This implementation is inspired by and references the OpenManus project.

Features

  • Implements github.com/cloudwego/eino/components/tool.InvokableTool
  • Easy integration with Eino's tool system
  • Support executing command-line instructions in Docker containers

Installation

go get github.com/cloudwego/eino-ext/components/tool/commandline@latest

Quick Start

Here's a quick example of how to use the commandline tool:

Editor:

package main

import (
	"context"
	"log"

	"github.com/cloudwego/eino-ext/components/tool/commandline"
	"github.com/cloudwego/eino-ext/components/tool/commandline/sandbox"
)

func main() {
	ctx := context.Background()

	op, err := sandbox.NewDockerSandbox(ctx, &sandbox.Config{})
	if err != nil {
		log.Fatal(err)
	}
	// you should ensure that docker has been started before create a docker container
	err = op.Create(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer op.Cleanup(ctx)

	sre, err := commandline.NewStrReplaceEditor(ctx, &commandline.Config{Operator: op})
	if err != nil {
		log.Fatal(err)
	}

	info, err := sre.Info(ctx)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("tool name: %s, tool desc: %s", info.Name, info.Desc)

	content := "hello world"

	log.Println("create file[test.txt]...")
	result, err := sre.Execute(ctx, &commandline.StrReplaceEditorParams{
		Command:  commandline.CreateCommand,
		Path:     "./test.txt",
		FileText: &content,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println("create file result: ", result)

	log.Println("view file[test.txt]...")
	result, err = sre.Execute(ctx, &commandline.StrReplaceEditorParams{
		Command: commandline.ViewCommand,
		Path:    "./test.txt",
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println("view file result: ", result)
}

PyExecutor:

/*
 * Copyright 2025 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"context"
	"log"

	"github.com/cloudwego/eino-ext/components/tool/commandline"
	"github.com/cloudwego/eino-ext/components/tool/commandline/sandbox"
)

func main() {
	ctx := context.Background()
	op, err := sandbox.NewDockerSandbox(ctx, &sandbox.Config{})
	if err != nil {
		log.Fatal(err)
	}
	// you should ensure that docker has been started before create a docker container
	err = op.Create(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer op.Cleanup(ctx)

	exec, err := commandline.NewPyExecutor(ctx, &commandline.PyExecutorConfig{Operator: op}) // use python3 by default
	if err != nil {
		log.Fatal(err)
	}

	info, err := exec.Info(ctx)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("tool name: %s,  tool desc: %s", info.Name, info.Desc)

	code := "print(\"hello world\")"
	log.Printf("execute code:\n%s", code)
	result, err := exec.Execute(ctx, &commandline.Input{Code: code})
	if err != nil {
		log.Fatal(err)
	}
	log.Println("result:\n", result)
}

For More Details

Examples

See the following examples for more usage:

Documentation

Index

Constants

View Source
const (
	SnippetLines     = 4
	MaxResponseLen   = 16000
	TruncatedMessage = "<response clipped><NOTE>To save on context only part of this file has been shown to you. " +
		"You should retry this tool after you have searched inside the file with `grep -n` " +
		"in order to find the line numbers of what you are looking for.</NOTE>"
)

Constants definition

View Source
const StrReplaceEditorDescription = `` /* 988-byte string literal not displayed */

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command string

Command type

const (
	ViewCommand       Command = "view"
	CreateCommand     Command = "create"
	StrReplaceCommand Command = "str_replace"
	InsertCommand     Command = "insert"
	UndoEditCommand   Command = "undo_edit"
)

type CommandOutput

type CommandOutput struct {
	// Stdout contains the data written by the command to standard output (stdout).
	// This is typically the "normal" output of a successful command.
	Stdout string

	// Stderr contains the data written by the command to standard error (stderr).
	// This often includes warnings, errors, or diagnostic messages,
	// even if the command exits successfully (exit code 0).
	Stderr string

	// ExitCode is the numeric exit status returned by the command.
	// - 0 usually indicates success.
	// - Non-zero values (1–255) typically indicate failure.
	// - Special values like 137 (128 + 9) suggest the process was killed by signal 9 (SIGKILL),
	//   often due to exceeding memory limits (OOM).
	ExitCode int
}

CommandOutput represents the result of executing a command in a sandboxed environment. It captures the standard output, standard error, and exit code of the process.

type EditorConfig

type EditorConfig struct {
	Operator Operator
}

type Input

type Input struct {
	Code string `json:"code"`
}

type Operator

type Operator interface {
	ReadFile(ctx context.Context, path string) (string, error)
	WriteFile(ctx context.Context, path string, content string) error
	IsDirectory(ctx context.Context, path string) (bool, error)
	Exists(ctx context.Context, path string) (bool, error)
	RunCommand(ctx context.Context, command []string) (*CommandOutput, error)
}

Operator defines the interface for file operations

type PyExecutor

type PyExecutor struct {
	// contains filtered or unexported fields
}

func NewPyExecutor

func NewPyExecutor(_ context.Context, cfg *PyExecutorConfig) (*PyExecutor, error)

func (*PyExecutor) Execute

func (p *PyExecutor) Execute(ctx context.Context, args *Input) (*CommandOutput, error)

func (*PyExecutor) Info

func (p *PyExecutor) Info(_ context.Context) (*schema.ToolInfo, error)

func (*PyExecutor) InvokableRun

func (p *PyExecutor) InvokableRun(ctx context.Context, argumentsInJSON string, _ ...tool.Option) (string, error)

type PyExecutorConfig

type PyExecutorConfig struct {
	Command  string `json:"command"`
	Operator Operator
}

type StrReplaceEditor

type StrReplaceEditor struct {
	// contains filtered or unexported fields
}

StrReplaceEditor struct definition

func NewStrReplaceEditor

func NewStrReplaceEditor(ctx context.Context, cfg *EditorConfig) (*StrReplaceEditor, error)

NewStrReplaceEditor creates a new editor instance

func (*StrReplaceEditor) Execute

func (e *StrReplaceEditor) Execute(ctx context.Context, params *StrReplaceEditorParams) (string, error)

Execute performs file operations command

func (*StrReplaceEditor) Info

func (*StrReplaceEditor) InvokableRun

func (e *StrReplaceEditor) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)

type StrReplaceEditorParams

type StrReplaceEditorParams struct {
	Command    Command `json:"command"`
	Path       string  `json:"path"`
	FileText   *string `json:"file_text,omitempty"`
	ViewRange  []int   `json:"view_range,omitempty"`
	OldStr     *string `json:"old_str,omitempty"`
	NewStr     *string `json:"new_str,omitempty"`
	InsertLine *int    `json:"insert_line,omitempty"`
}

Directories

Path Synopsis
examples
editor command
pyexecutor command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL