Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 14x 108x 14x 14x 44x 44x 14x 112x 112x 43x 69x 69x 1x 68x 5x 63x 14x 112x 112x 112x 112x 37x 3x 3x 255x 255x 255x 6x 34x 14x | import { BuiltInBadge } from "@/components/badge/BuiltInBadge";
import { RunFieldControl } from "@/components/form/RunFields/RunFieldControl";
import {
type CommandBlockers,
type CommandEntry,
commandReady,
} from "@/util/chatCommands";
import type { FormValue, FormValues } from "@/util/runForm";
import { AlertTriangle, CheckCircle2, Workflow, X } from "lucide-react";
import React, { forwardRef } from "react";
/**
* A chat command once it has been chosen: the token that stands for its line
* in the message box, and the form its target takes, drawn above the box.
*
* The token is the command line in code type — what svc-core will parse — so
* a command never reads as a message. The form is the target's run schema
* (`ChatCommandTarget.form`): the same fields, drawn by the same controls
* (`RunFieldControl`), as the skill's own Run form, so a skill takes the same
* inputs wherever it runs. The chat holds the send until nothing in
* `CommandBlockers` is left, and the form's last line says what is.
*/
export interface CommandTokenProps {
entry: CommandEntry;
onRemove: () => void;
}
export const CommandToken: React.FC<CommandTokenProps> = ({
entry,
onRemove,
}) => (
<span
data-testid="command-token"
title={`${entry.target.name} · ${entry.spec.label}`}
className="inline-flex max-w-full min-w-0 items-center gap-0.5 rounded-md border border-indigo-200 dark:border-indigo-800 bg-indigo-50 dark:bg-indigo-950/50 py-0.5 pl-1.5 pr-0.5 text-indigo-700 dark:text-indigo-300"
>
<code className="truncate font-mono text-xs">{entry.line}</code>
<button
type="button"
onClick={onRemove}
aria-label="Remove command"
title="Remove command"
className="shrink-0 rounded p-0.5 text-indigo-400 hover:bg-indigo-100 hover:text-indigo-700 dark:hover:bg-indigo-900/60 dark:hover:text-indigo-200"
>
<X className="size-3" aria-hidden="true" />
</button>
</span>
);
const LIST = new Intl.ListFormat("en-GB", { type: "conjunction" });
/** Field names as the labels show them, in code type: "topic and audience". */
const fieldList = (names: string[]): React.ReactNode =>
LIST.formatToParts(names).map((part, i) =>
part.type === "element" ? (
<code key={i} className="font-mono">
{part.value}
</code>
) : (
<React.Fragment key={i}>{part.value}</React.Fragment>
),
);
/** What still stops the send, or that nothing does. */
const statusLine = (
entry: CommandEntry,
blockers: CommandBlockers,
): React.ReactNode => {
const name = entry.target.name;
if (blockers.missing.length)
return (
<>
Fill in {fieldList(blockers.missing)} to run {name}.
</>
);
const invalid = Object.keys(blockers.invalid);
if (invalid.length)
return (
<>
Check {fieldList(invalid)} to run {name}.
</>
);
if (blockers.request)
return entry.spec.request_hint || `Say what ${name} should work on.`;
return `Ready to run ${name}.`;
};
export interface CommandFormProps {
entry: CommandEntry;
values: FormValues;
/** `toInputs`'s errors for `values`. */
errors: Record<string, string>;
blockers: CommandBlockers;
/** Fields the person has left; their errors show. */
touched: Record<string, boolean>;
/** A send was tried with something missing: every error shows. */
attempted: boolean;
onChange: (name: string, value: FormValue) => void;
onTouch: (name: string) => void;
/** Ctrl+Enter (⌘+Enter) from a field — Enter in one is a new line. */
onSubmit: () => void;
/** Prefixes the controls' ids. */
idPrefix: string;
}
export const CommandForm = forwardRef<HTMLElement, CommandFormProps>(
(
{
entry,
values,
errors,
blockers,
touched,
attempted,
onChange,
onTouch,
onSubmit,
idPrefix,
},
ref,
) => {
const form = entry.target.form;
const fields = form?.fields ?? [];
const ready = commandReady(blockers);
return (
<section
ref={ref}
aria-label={`${entry.target.name} inputs`}
onKeyDown={(e) => {
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
onSubmit();
}
}}
className="rounded-lg border border-indigo-200 dark:border-indigo-800 bg-white dark:bg-gray-900 overflow-hidden"
>
<header className="flex items-start gap-2 px-3 pt-2.5 pb-2">
<Workflow
className="mt-0.5 size-4 shrink-0 text-indigo-500 dark:text-indigo-400"
aria-hidden="true"
/>
<div className="min-w-0 flex-1">
<p className="flex flex-wrap items-center gap-x-1.5 gap-y-0.5">
<span className="text-sm font-medium text-gray-800 dark:text-gray-100">
{entry.target.name}
</span>
{entry.target.built_in && <BuiltInBadge size="sm" />}
<span className="text-xs text-gray-400 dark:text-gray-500">
{entry.spec.label}
</span>
</p>
{entry.target.description && (
<p className="mt-0.5 text-xs text-gray-500 dark:text-gray-400">
{entry.target.description}
</p>
)}
</div>
</header>
{fields.length > 0 && (
// Capped, so a long form scrolls rather than pushing the thread
// off a phone's screen; a container query lays it out by the
// chat's width, not the window's.
<div className="@container max-h-[40vh] overflow-y-auto border-t border-gray-100 dark:border-gray-800 px-3 py-3">
<div className="grid @md:grid-cols-2 gap-x-4 gap-y-3">
{fields.map((field) => {
const error = errors[field.name];
const shown = attempted || touched[field.name];
return (
// `contents`, so the control's own column span still
// applies to the grid.
<div
key={field.name}
data-field={field.name}
className="contents"
onBlur={() => onTouch(field.name)}
>
<RunFieldControl
field={field}
value={values[field.name] ?? ""}
error={shown ? error : undefined}
onChange={(v) => onChange(field.name, v)}
idPrefix={idPrefix}
/>
</div>
);
})}
</div>
</div>
)}
{form && !form.complete && (
<p className="flex items-start gap-1.5 border-t border-gray-100 dark:border-gray-800 px-3 py-1.5 text-[11px] text-amber-700 dark:text-amber-400">
<AlertTriangle className="mt-px size-3 shrink-0" aria-hidden="true" />
Some of this skill's inputs could not be looked up, so the form
may be missing a field.
</p>
)}
<p
role="status"
className={`flex items-start gap-1.5 border-t px-3 py-1.5 text-[11px] ${
ready
? "border-emerald-100 dark:border-emerald-900/40 bg-emerald-50/60 dark:bg-emerald-950/20 text-emerald-700 dark:text-emerald-400"
: "border-gray-100 dark:border-gray-800 bg-gray-50 dark:bg-gray-800/40 text-gray-600 dark:text-gray-400"
}`}
>
{ready && (
<CheckCircle2 className="mt-px size-3 shrink-0" aria-hidden="true" />
)}
<span className="min-w-0">{statusLine(entry, blockers)}</span>
</p>
</section>
);
},
);
CommandForm.displayName = "CommandForm";
export default CommandForm;
|