|
| 1 | +package chattool |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "path" |
| 7 | + "strings" |
| 8 | + "unicode" |
| 9 | + "unicode/utf8" |
| 10 | + |
| 11 | + "charm.land/fantasy" |
| 12 | + "github.com/google/uuid" |
| 13 | + |
| 14 | + "github.com/coder/coder/v2/codersdk/workspacesdk" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + maxPreviewTitleLength = 80 |
| 19 | + maxPreviewURLPathLength = 2048 |
| 20 | +) |
| 21 | + |
| 22 | +// OpenPreviewResult is the open_preview tool result. The chat previews |
| 23 | +// endpoint decodes stored results with this type, so its JSON keys must stay |
| 24 | +// stable. |
| 25 | +type OpenPreviewResult struct { |
| 26 | + OK bool `json:"ok"` |
| 27 | + Title string `json:"title"` |
| 28 | + WorkspaceID uuid.UUID `json:"workspace_id"` |
| 29 | + AgentName string `json:"agent_name"` |
| 30 | + // Exactly one of Port or PreviewID identifies the preview on the agent. |
| 31 | + Port int32 `json:"port,omitempty"` |
| 32 | + PreviewID string `json:"preview_id,omitempty"` |
| 33 | + // Path is the file or directory a PreviewID preview serves, with symlinks |
| 34 | + // resolved. |
| 35 | + Path string `json:"path,omitempty"` |
| 36 | + // URLPath is the path and query a port preview opens at, such as |
| 37 | + // "/?folder=/home/coder". It is empty for the server's root. |
| 38 | + URLPath string `json:"url_path,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +// PreviewWorkspace binds a preview to the workspace connection used to open it. |
| 42 | +type PreviewWorkspace struct { |
| 43 | + Conn workspacesdk.AgentConn |
| 44 | + WorkspaceID uuid.UUID |
| 45 | + AgentName string |
| 46 | +} |
| 47 | + |
| 48 | +// OpenPreviewOptions configures the open_preview tool. |
| 49 | +type OpenPreviewOptions struct { |
| 50 | + GetWorkspace func(context.Context) (PreviewWorkspace, error) |
| 51 | +} |
| 52 | + |
| 53 | +// OpenPreviewArgs are the open_preview tool arguments. |
| 54 | +type OpenPreviewArgs struct { |
| 55 | + Title string `json:"title" description:"Short name for the preview shown in the user's preview selector, such as Docs site or Signup form. At most 80 characters."` |
| 56 | + Port *int `json:"port,omitempty" description:"Port of an existing HTTP server, between 1000 and 65535. Do not combine with path."` |
| 57 | + Path string `json:"path,omitempty" description:"Absolute workspace path to an HTML file or a directory containing index.html. Do not combine with port."` |
| 58 | + URLPath string `json:"url_path,omitempty" description:"Path and query to open on the port's server, such as /?folder=/home/coder/project. Only with port. Defaults to /."` |
| 59 | +} |
| 60 | + |
| 61 | +// IsPreviewURLPath reports whether s is a path and optional query that cannot |
| 62 | +// change the origin when appended to a preview URL. |
| 63 | +func IsPreviewURLPath(s string) bool { |
| 64 | + return len(s) <= maxPreviewURLPathLength && |
| 65 | + strings.HasPrefix(s, "/") && |
| 66 | + !strings.HasPrefix(s, "//") && |
| 67 | + !strings.ContainsAny(s, `\#`) && |
| 68 | + !strings.ContainsFunc(s, unicode.IsControl) |
| 69 | +} |
| 70 | + |
| 71 | +// OpenPreview returns a tool that opens a workspace preview in the user's UI. |
| 72 | +func OpenPreview(options OpenPreviewOptions) fantasy.AgentTool { |
| 73 | + return fantasy.NewAgentTool( |
| 74 | + "open_preview", |
| 75 | + "Open and select an embedded preview for the user. Pass a short title and exactly one of port or path. "+ |
| 76 | + "Use port for an existing HTTP web app; this does not start the app or wait for it to be ready. "+ |
| 77 | + "Add url_path to open the app at a path and query other than /, for example /?folder=/home/coder/project. "+ |
| 78 | + "Use path for an HTML file or a directory containing index.html; the workspace daemon publishes the files through Coder. "+ |
| 79 | + "A file publishes only that file. To include CSS, JavaScript, images, or other pages, publish their directory instead. "+ |
| 80 | + "Directory contents are published, so use a dedicated output directory without secrets. Directory listing is disabled. "+ |
| 81 | + "Call this again to refresh after changing static files, or to restore a preview after its server or the workspace daemon restarts. "+ |
| 82 | + "Path previews last until the workspace daemon stops, and at most 64 paths can be published until it restarts.", |
| 83 | + func(ctx context.Context, args OpenPreviewArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) { |
| 84 | + args.Title = strings.TrimSpace(args.Title) |
| 85 | + if args.Title == "" { |
| 86 | + return fantasy.NewTextErrorResponse("title is required"), nil |
| 87 | + } |
| 88 | + if utf8.RuneCountInString(args.Title) > maxPreviewTitleLength { |
| 89 | + return fantasy.NewTextErrorResponse(fmt.Sprintf("title must be at most %d characters", maxPreviewTitleLength)), nil |
| 90 | + } |
| 91 | + |
| 92 | + args.Path = strings.TrimSpace(args.Path) |
| 93 | + hasPort, hasPath := args.Port != nil, args.Path != "" |
| 94 | + if hasPort == hasPath { |
| 95 | + return fantasy.NewTextErrorResponse("provide exactly one of port or path"), nil |
| 96 | + } |
| 97 | + if hasPath { |
| 98 | + if !path.IsAbs(args.Path) { |
| 99 | + return fantasy.NewTextErrorResponse("path must be absolute"), nil |
| 100 | + } |
| 101 | + // Clean so /out/ and /out share a preview ID. |
| 102 | + args.Path = path.Clean(args.Path) |
| 103 | + } |
| 104 | + |
| 105 | + // Preview hostnames match only four- or five-digit ports. |
| 106 | + var port int32 |
| 107 | + if hasPort { |
| 108 | + if *args.Port < 1000 || *args.Port > 65535 { |
| 109 | + return fantasy.NewTextErrorResponse("port must be between 1000 and 65535"), nil |
| 110 | + } |
| 111 | + port = int32(*args.Port) |
| 112 | + } |
| 113 | + |
| 114 | + args.URLPath = strings.TrimSpace(args.URLPath) |
| 115 | + if args.URLPath == "/" { |
| 116 | + // Store "/" as empty so it is listed as the same preview as no |
| 117 | + // URL path. |
| 118 | + args.URLPath = "" |
| 119 | + } |
| 120 | + if args.URLPath != "" && !hasPort { |
| 121 | + return fantasy.NewTextErrorResponse("url_path can only be used with port"), nil |
| 122 | + } |
| 123 | + if args.URLPath != "" && !IsPreviewURLPath(args.URLPath) { |
| 124 | + return fantasy.NewTextErrorResponse("url_path must start with a single /, contain only a path and query, and have no backslashes, fragments or control characters"), nil |
| 125 | + } |
| 126 | + |
| 127 | + if options.GetWorkspace == nil { |
| 128 | + return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil |
| 129 | + } |
| 130 | + workspace, err := options.GetWorkspace(ctx) |
| 131 | + if err != nil { |
| 132 | + return fantasy.NewTextErrorResponse(err.Error()), nil |
| 133 | + } |
| 134 | + |
| 135 | + result := OpenPreviewResult{ |
| 136 | + OK: true, |
| 137 | + Title: args.Title, |
| 138 | + WorkspaceID: workspace.WorkspaceID, |
| 139 | + AgentName: workspace.AgentName, |
| 140 | + } |
| 141 | + if hasPort { |
| 142 | + result.Port = port |
| 143 | + result.URLPath = args.URLPath |
| 144 | + return marshalToolResponse(result), nil |
| 145 | + } |
| 146 | + |
| 147 | + preview, err := workspace.Conn.OpenPreview(ctx, workspacesdk.OpenPreviewRequest{Path: args.Path}) |
| 148 | + if err != nil { |
| 149 | + return fantasy.NewTextErrorResponse("open file preview: " + err.Error()), nil |
| 150 | + } |
| 151 | + |
| 152 | + result.PreviewID = preview.ID |
| 153 | + result.Path = preview.Path |
| 154 | + return marshalToolResponse(result), nil |
| 155 | + }, |
| 156 | + ) |
| 157 | +} |
0 commit comments