mirror of
https://github.com/coder/coder.git
synced 2026-06-04 05:28:20 +00:00
80a172f932
- Moves `coderd/chatd/`, `coderd/gitsync/`, `enterprise/coderd/chatd/` under `x/` parent directories to signal instability - Adds `Experimental:` glue code comments in `coderd/coderd.go` > 🤖 This PR was created with the help of Coder Agents, and was reviewed by my human. 🧑💻
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"charm.land/fantasy"
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
type WriteFileOptions struct {
|
|
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
}
|
|
|
|
type WriteFileArgs struct {
|
|
Path string `json:"path"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
func WriteFile(options WriteFileOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"write_file",
|
|
"Write a file to the workspace.",
|
|
func(ctx context.Context, args WriteFileArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if options.GetWorkspaceConn == nil {
|
|
return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return executeWriteFileTool(ctx, conn, args)
|
|
},
|
|
)
|
|
}
|
|
|
|
func executeWriteFileTool(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
args WriteFileArgs,
|
|
) (fantasy.ToolResponse, error) {
|
|
if args.Path == "" {
|
|
return fantasy.NewTextErrorResponse("path is required"), nil
|
|
}
|
|
|
|
if err := conn.WriteFile(ctx, args.Path, strings.NewReader(args.Content)); err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return toolResponse(map[string]any{"ok": true}), nil
|
|
}
|