thread-goal-command.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. export type TuiThreadGoalCommandIntent =
  2. | { kind: 'help' }
  3. | { kind: 'view' }
  4. | {
  5. kind: 'create';
  6. objective: string;
  7. tokenBudget?: number | null;
  8. }
  9. | {
  10. kind: 'budget';
  11. tokenBudget: number | null;
  12. }
  13. | { kind: 'clear' }
  14. | { kind: 'edit' }
  15. | { kind: 'pause' }
  16. | { kind: 'resume' }
  17. | { kind: 'error'; message: string };
  18. export const TUI_THREAD_GOAL_COMMAND_HELP =
  19. '/goal: show the current Goal and its latest status.\n' +
  20. '/goal <objective>: set a persistent objective the agent will pursue across turns.\n' +
  21. '/goal <objective> budget=50K: set objective and token budget in one shot.\n' +
  22. '/goal budget=50K: set/change the token budget on the current goal.\n' +
  23. '/goal budget=clear: remove the token budget (uncapped).\n' +
  24. '/goal clear: remove the current goal.\n' +
  25. '/goal edit: edit the objective inline.\n' +
  26. '/goal pause: pause auto-continuation.\n' +
  27. '/goal resume: resume a paused or blocked goal.';
  28. /**
  29. * Boundary characters that may separate a relaxed `budget=<value>`
  30. * directive from objective text: whitespace, CJK punctuation & kana
  31. * (U+3000–U+30FF), CJK ideographs (U+4E00–U+9FFF), full/half-width forms
  32. * (U+FF00–U+FFEF) and the horizontal ellipsis. Deliberately an ALLOWLIST —
  33. * ASCII punctuation is not a boundary, so technical text keeps its tail.
  34. * Mirrors the Desktop parser in `@mavis/ui` `lib/thread-goal-slash.ts`.
  35. */
  36. const RELAXED_BOUNDARY = '[\\s\\u2026\\u3000-\\u30FF\\u4E00-\\u9FFF\\uFF00-\\uFFEF]';
  37. const LEADING_BUDGET_RE = new RegExp(`^budget=([A-Za-z0-9.]+)(?=$|${RELAXED_BOUNDARY})`, 'iu');
  38. const TRAILING_BUDGET_RE = new RegExp(`(^|${RELAXED_BOUNDARY})budget=([A-Za-z0-9.]+)$`, 'iu');
  39. const SEPARATOR_BEFORE_RE = /[\s…、。!,:;?]+$/u;
  40. const SEPARATOR_AFTER_RE = /^[\s…、。!,:;?]+/u;
  41. function parseTrailingBudget(
  42. body: string,
  43. ): { value: number | null | 'invalid'; head: string } | null {
  44. const trimmed = body.trimEnd();
  45. const lastSpace = trimmed.search(/\s\S*$/);
  46. const tail = lastSpace === -1 ? trimmed : trimmed.slice(lastSpace + 1);
  47. if (!/^budget=[A-Za-z0-9.]*$/i.test(tail)) return null;
  48. const rawValue = tail.slice('budget='.length);
  49. const head = lastSpace === -1 ? '' : trimmed.slice(0, lastSpace).trimEnd();
  50. return { value: parseBudgetValue(rawValue), head };
  51. }
  52. type BudgetDirectiveExtraction =
  53. | { kind: 'none' }
  54. | { kind: 'invalid' }
  55. | { kind: 'double' }
  56. | { kind: 'value'; value: number | null; objective: string };
  57. /**
  58. * Extract a `budget=<value>` directive from the start or end of a body.
  59. * Strict trailing token first (may surface `'invalid'`), then the relaxed
  60. * trailing form (CJK boundary, valid values only), then the relaxed
  61. * leading form; a valid directive at both ends is `'double'`.
  62. */
  63. function extractBudgetDirective(body: string): BudgetDirectiveExtraction {
  64. const trimmed = body.trim();
  65. const strict = parseTrailingBudget(trimmed);
  66. if (strict?.value === 'invalid') return { kind: 'invalid' };
  67. let value: number | null | undefined;
  68. let head: string | undefined;
  69. if (strict) {
  70. value = strict.value;
  71. head = strict.head;
  72. } else {
  73. const relaxed = TRAILING_BUDGET_RE.exec(trimmed);
  74. if (relaxed) {
  75. const parsed = parseBudgetValue(relaxed[2] ?? '');
  76. if (parsed !== 'invalid') {
  77. value = parsed;
  78. head = trimmed.slice(0, relaxed.index + (relaxed[1] ?? '').length);
  79. }
  80. }
  81. }
  82. if (head !== undefined && value !== undefined) {
  83. const objective = head.replace(SEPARATOR_BEFORE_RE, '').trim();
  84. const leftover = LEADING_BUDGET_RE.exec(objective);
  85. if (leftover && parseBudgetValue(leftover[1] ?? '') !== 'invalid') {
  86. return { kind: 'double' };
  87. }
  88. return { kind: 'value', value, objective };
  89. }
  90. const leading = LEADING_BUDGET_RE.exec(trimmed);
  91. if (leading) {
  92. const parsed = parseBudgetValue(leading[1] ?? '');
  93. if (parsed !== 'invalid') {
  94. const objective = trimmed.slice(leading[0].length).replace(SEPARATOR_AFTER_RE, '').trim();
  95. return { kind: 'value', value: parsed, objective };
  96. }
  97. }
  98. return { kind: 'none' };
  99. }
  100. function parseBudgetValue(raw: string): number | null | 'invalid' {
  101. const value = raw.trim();
  102. if (!value) return 'invalid';
  103. const normalized = value.toLowerCase();
  104. if (
  105. normalized === 'clear' ||
  106. normalized === 'null' ||
  107. normalized === 'none' ||
  108. normalized === 'off' ||
  109. value === '0'
  110. ) {
  111. return null;
  112. }
  113. const match = normalized.match(/^([0-9]+(?:\.[0-9]+)?)([km])?$/);
  114. if (!match) return 'invalid';
  115. const [, numberText, suffix] = match;
  116. const parsed = Number(numberText);
  117. if (!Number.isFinite(parsed) || parsed <= 0) return 'invalid';
  118. const multiplier = suffix === 'k' ? 1_000 : suffix === 'm' ? 1_000_000 : 1;
  119. const result = Math.round(parsed * multiplier);
  120. return result > 0 ? result : 'invalid';
  121. }
  122. export function parseTuiThreadGoalCommand(rawArgs: string): TuiThreadGoalCommandIntent {
  123. const tail = rawArgs.trim();
  124. if (!tail) return { kind: 'view' };
  125. const firstSpace = tail.search(/\s/);
  126. const headRaw = firstSpace === -1 ? tail : tail.slice(0, firstSpace);
  127. const head = headRaw.toLowerCase();
  128. if (head === 'budget') {
  129. return {
  130. kind: 'error',
  131. message: '/goal budget needs a value: `/goal budget=50K` or `/goal budget=clear`.',
  132. };
  133. }
  134. if (/^budget=[A-Za-z0-9.]*$/i.test(headRaw) && tail.length === headRaw.length) {
  135. const rawBudget = headRaw.slice('budget='.length);
  136. const parsed = parseBudgetValue(rawBudget);
  137. if (parsed === 'invalid') {
  138. return {
  139. kind: 'error',
  140. message: `Bad budget value: use a positive integer, K/M suffix, or 'clear' (got "${rawBudget}").`,
  141. };
  142. }
  143. return { kind: 'budget', tokenBudget: parsed };
  144. }
  145. switch (head) {
  146. case 'clear':
  147. case 'cancel':
  148. case 'delete':
  149. return tail.length > headRaw.length
  150. ? { kind: 'error', message: '/goal clear takes no arguments.' }
  151. : { kind: 'clear' };
  152. case 'edit':
  153. return tail.length > headRaw.length
  154. ? { kind: 'error', message: '/goal edit takes no arguments.' }
  155. : { kind: 'edit' };
  156. case 'pause':
  157. return tail.length > headRaw.length
  158. ? { kind: 'error', message: '/goal pause takes no arguments.' }
  159. : { kind: 'pause' };
  160. case 'resume':
  161. return tail.length > headRaw.length
  162. ? { kind: 'error', message: '/goal resume takes no arguments.' }
  163. : { kind: 'resume' };
  164. case 'help':
  165. return { kind: 'help' };
  166. default: {
  167. const extraction = extractBudgetDirective(tail);
  168. if (extraction.kind === 'invalid') {
  169. return {
  170. kind: 'error',
  171. message:
  172. 'Bad budget value: use a positive integer, K/M suffix, or `clear`/`null` to remove the cap.',
  173. };
  174. }
  175. if (extraction.kind === 'double') {
  176. return {
  177. kind: 'error',
  178. message: 'budget= is specified more than once: keep a single budget=<value>.',
  179. };
  180. }
  181. if (extraction.kind === 'value') {
  182. return extraction.objective
  183. ? { kind: 'create', objective: extraction.objective, tokenBudget: extraction.value }
  184. : { kind: 'budget', tokenBudget: extraction.value };
  185. }
  186. return { kind: 'create', objective: tail };
  187. }
  188. }
  189. }