@useavalon/caelence-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +201 -0
  2. package/bin/harness.ts +10 -0
  3. package/package.json +75 -0
  4. package/skills/copy-editor/SKILL.md +74 -0
  5. package/skills/copy-editor/references/anti-slop.md +62 -0
  6. package/skills/copy-editor/references/ui-states.md +46 -0
  7. package/skills/copy-editor/scripts/lint-copy.py +78 -0
  8. package/skills/copy-harry-dry/SKILL.md +120 -0
  9. package/skills/copy-rmbc/SKILL.md +97 -0
  10. package/skills/copywriting/SKILL.md +73 -0
  11. package/skills/impeccable/SKILL.md +133 -0
  12. package/skills/impeccable/references/visual-slop.md +36 -0
  13. package/src/cli/main.test.ts +36 -0
  14. package/src/cli/main.ts +384 -0
  15. package/src/cli/media-models.ts +82 -0
  16. package/src/cli/models.ts +43 -0
  17. package/src/cli/openrouter-live.test.ts +35 -0
  18. package/src/cli/openrouter-live.ts +72 -0
  19. package/src/cli/openrouter-modality.test.ts +25 -0
  20. package/src/cli/openrouter-modality.ts +83 -0
  21. package/src/cli/picker.test.ts +53 -0
  22. package/src/cli/picker.ts +48 -0
  23. package/src/cli/plain-text.ts +15 -0
  24. package/src/cli/slash-dispatch.test.ts +213 -0
  25. package/src/cli/slash-dispatch.ts +298 -0
  26. package/src/cli/slash.test.ts +113 -0
  27. package/src/cli/slash.ts +119 -0
  28. package/src/cli/theme.test.ts +10 -0
  29. package/src/cli/theme.ts +44 -0
  30. package/src/cli/tui-layout.test.ts +33 -0
  31. package/src/cli/tui-layout.ts +33 -0
  32. package/src/cli/tui-screen.test.ts +33 -0
  33. package/src/cli/tui-screen.ts +13 -0
  34. package/src/cli/tui.tsx +735 -0
  35. package/src/config.ts +86 -0
  36. package/src/core/approval.test.ts +60 -0
  37. package/src/core/approval.ts +83 -0
  38. package/src/core/auto-router.test.ts +31 -0
  39. package/src/core/auto-router.ts +66 -0
  40. package/src/core/errors.ts +67 -0
  41. package/src/core/events.ts +85 -0
  42. package/src/core/hooks.test.ts +66 -0
  43. package/src/core/hooks.ts +94 -0
  44. package/src/core/ids.ts +6 -0
  45. package/src/core/instructions.test.ts +49 -0
  46. package/src/core/instructions.ts +44 -0
  47. package/src/core/logger.ts +45 -0
  48. package/src/core/mcp.ts +191 -0
  49. package/src/core/mode.test.ts +63 -0
  50. package/src/core/mode.ts +60 -0
  51. package/src/core/run-turn.test.ts +339 -0
  52. package/src/core/run-turn.ts +487 -0
  53. package/src/core/session.test.ts +203 -0
  54. package/src/core/session.ts +349 -0
  55. package/src/core/stdio-mcp.ts +143 -0
  56. package/src/core/strands.ts +262 -0
  57. package/src/core/usage.test.ts +69 -0
  58. package/src/core/usage.ts +171 -0
  59. package/src/desktop/bridge.test.ts +331 -0
  60. package/src/desktop/bridge.ts +729 -0
  61. package/src/desktop/open-local.test.ts +40 -0
  62. package/src/desktop/open-local.ts +52 -0
  63. package/src/desktop/preview-cache.test.ts +36 -0
  64. package/src/desktop/preview-cache.ts +63 -0
  65. package/src/desktop/secrets.test.ts +44 -0
  66. package/src/desktop/secrets.ts +60 -0
  67. package/src/evals/runner.test.ts +43 -0
  68. package/src/evals/runner.ts +225 -0
  69. package/src/index.ts +132 -0
  70. package/src/init.test.ts +54 -0
  71. package/src/init.ts +117 -0
  72. package/src/integrations/catalog-data.json +424 -0
  73. package/src/integrations/catalog.ts +81 -0
  74. package/src/integrations/connect.ts +231 -0
  75. package/src/integrations/integrations.test.ts +329 -0
  76. package/src/integrations/marks/airtable.svg +1 -0
  77. package/src/integrations/marks/algolia-mcp.svg +1 -0
  78. package/src/integrations/marks/canva.jpg +0 -0
  79. package/src/integrations/marks/cloudflare-mcp.svg +1 -0
  80. package/src/integrations/marks/contentful-mcp.svg +1 -0
  81. package/src/integrations/marks/craft.png +0 -0
  82. package/src/integrations/marks/dinero.png +0 -0
  83. package/src/integrations/marks/figma.svg +1 -0
  84. package/src/integrations/marks/gitlab.svg +1 -0
  85. package/src/integrations/marks/google-calendar.svg +1 -0
  86. package/src/integrations/marks/google-docs.svg +1 -0
  87. package/src/integrations/marks/google-drive.svg +1 -0
  88. package/src/integrations/marks/google-gmail.svg +1 -0
  89. package/src/integrations/marks/google-sheets.svg +1 -0
  90. package/src/integrations/marks/google-slides.svg +1 -0
  91. package/src/integrations/marks/granola.png +0 -0
  92. package/src/integrations/marks/jira.svg +1 -0
  93. package/src/integrations/marks/linear.svg +1 -0
  94. package/src/integrations/marks/microsoft-365.svg +7 -0
  95. package/src/integrations/marks/miro.svg +1 -0
  96. package/src/integrations/marks/mixpanel-mcp.svg +1 -0
  97. package/src/integrations/marks/monday.png +0 -0
  98. package/src/integrations/marks/mux-mcp.png +0 -0
  99. package/src/integrations/marks/neon-mcp.svg +1 -0
  100. package/src/integrations/marks/notion.svg +1 -0
  101. package/src/integrations/marks/paddle-mcp.svg +1 -0
  102. package/src/integrations/marks/posthog-mcp.svg +1 -0
  103. package/src/integrations/marks/prismic-mcp.svg +1 -0
  104. package/src/integrations/marks/replicate-mcp.svg +1 -0
  105. package/src/integrations/marks/resend-mcp.svg +1 -0
  106. package/src/integrations/marks/sanity-mcp.svg +1 -0
  107. package/src/integrations/marks/sceneplane.png +0 -0
  108. package/src/integrations/marks/sentry-mcp.svg +1 -0
  109. package/src/integrations/marks/stripe-mcp.svg +1 -0
  110. package/src/integrations/marks/supabase-mcp.svg +1 -0
  111. package/src/integrations/marks/upstash-mcp.svg +1 -0
  112. package/src/integrations/marks/vercel.svg +1 -0
  113. package/src/integrations/marks/webflow.svg +1 -0
  114. package/src/integrations/mcp-oauth.test.ts +39 -0
  115. package/src/integrations/mcp-oauth.ts +330 -0
  116. package/src/integrations/oauth-clients.test.ts +77 -0
  117. package/src/integrations/oauth-clients.ts +127 -0
  118. package/src/integrations/oauth.ts +145 -0
  119. package/src/integrations/remote-mcp.ts +307 -0
  120. package/src/integrations/store.ts +149 -0
  121. package/src/media/openrouter-generate.test.ts +80 -0
  122. package/src/media/openrouter-generate.ts +178 -0
  123. package/src/media/prefs.ts +54 -0
  124. package/src/observability/categories.ts +41 -0
  125. package/src/observability/create.test.ts +56 -0
  126. package/src/observability/create.ts +45 -0
  127. package/src/observability/experiments.ts +113 -0
  128. package/src/observability/fake.ts +191 -0
  129. package/src/observability/index.ts +57 -0
  130. package/src/observability/judges.ts +91 -0
  131. package/src/observability/langfuse.ts +275 -0
  132. package/src/observability/noop.ts +33 -0
  133. package/src/observability/observability.test.ts +113 -0
  134. package/src/observability/prompts.ts +98 -0
  135. package/src/observability/regression-items.ts +158 -0
  136. package/src/observability/sanitize.test.ts +26 -0
  137. package/src/observability/sanitize.ts +57 -0
  138. package/src/observability/types.ts +122 -0
  139. package/src/observability/version.ts +41 -0
  140. package/src/product.ts +2 -0
  141. package/src/runtime.test.ts +98 -0
  142. package/src/runtime.ts +438 -0
  143. package/src/skills/author-skill.ts +49 -0
  144. package/src/skills/create-skill.test.ts +108 -0
  145. package/src/skills/create-skill.ts +70 -0
  146. package/src/skills/loader.test.ts +122 -0
  147. package/src/skills/loader.ts +162 -0
  148. package/src/skills/registry.test.ts +190 -0
  149. package/src/skills/registry.ts +402 -0
  150. package/src/skills/skill-command.ts +53 -0
  151. package/src/skills/user-dir.ts +8 -0
  152. package/src/tools/git.test.ts +71 -0
  153. package/src/tools/git.ts +126 -0
  154. package/src/tools/local.ts +258 -0
  155. package/src/tools/paths.test.ts +24 -0
  156. package/src/tools/paths.ts +39 -0
  157. package/src/tools/read-skill.ts +43 -0
  158. package/src/tools/subagent.test.ts +77 -0
  159. package/src/tools/subagent.ts +74 -0
  160. package/src/tools/todo.test.ts +25 -0
  161. package/src/tools/todo.ts +70 -0
@@ -0,0 +1,298 @@
1
+ import { resolveOpenRouter } from "../config.ts";
2
+ import { AGENT_MODES, tryParseAgentMode } from "../core/mode.ts";
3
+ import type { Session } from "../core/session.ts";
4
+ import { maskSecret, writeOpenRouterKey } from "../desktop/secrets.ts";
5
+ import { listEvalSuites, runNamedEval } from "../evals/runner.ts";
6
+ import { type GeneratedMedia, generateImage, generateVideo } from "../media/openrouter-generate.ts";
7
+ import { readMediaPrefs, writeMediaPrefs } from "../media/prefs.ts";
8
+ import type { HarnessRuntime } from "../runtime.ts";
9
+ import { parseSkillCommand, SKILL_COMMAND_USAGE } from "../skills/skill-command.ts";
10
+ import { type MediaModelOption, mediaModelLabel, mediaPickerItems } from "./media-models.ts";
11
+ import { modelPickerItems } from "./models.ts";
12
+ import { loadLiveTargetNames } from "./openrouter-live.ts";
13
+ import { loadModalityModels, type MediaModality } from "./openrouter-modality.ts";
14
+ import { matchListedSession, type PickerItem, sessionPickerItems } from "./picker.ts";
15
+ import { formatSkillList, parseSlashLine, resolveSlashSubmit, SLASH_HELP } from "./slash.ts";
16
+
17
+ export type SlashPickerKind = "model" | "mode" | "resume" | "image" | "video" | "transcribe";
18
+
19
+ export type SlashOutcome =
20
+ | { kind: "text"; text: string }
21
+ | { kind: "applied" }
22
+ | { kind: "incomplete"; draft: string }
23
+ | { kind: "hold" }
24
+ | { kind: "clear" }
25
+ | { kind: "session"; session: Session }
26
+ | { kind: "picker"; title: string; picker: SlashPickerKind; items: PickerItem[] }
27
+ | { kind: "media"; media: GeneratedMedia; caption: string }
28
+ | { kind: "exit" };
29
+
30
+ const PICKER_KINDS = new Set<SlashPickerKind>([
31
+ "model",
32
+ "mode",
33
+ "resume",
34
+ "image",
35
+ "video",
36
+ "transcribe",
37
+ ]);
38
+
39
+ export function isSlashPickerKind(value: unknown): value is SlashPickerKind {
40
+ return typeof value === "string" && PICKER_KINDS.has(value as SlashPickerKind);
41
+ }
42
+
43
+ async function mediaModels(
44
+ harness: HarnessRuntime,
45
+ modality: MediaModality,
46
+ ): Promise<readonly MediaModelOption[]> {
47
+ const or = resolveOpenRouter(harness.config);
48
+ return loadModalityModels({
49
+ modality,
50
+ apiKey: harness.hasApiKey ? or.apiKey : "",
51
+ baseUrl: or.baseUrl,
52
+ });
53
+ }
54
+
55
+ async function generateMediaOutcome(
56
+ harness: HarnessRuntime,
57
+ kind: "image" | "video",
58
+ prompt: string,
59
+ ): Promise<SlashOutcome> {
60
+ if (!harness.hasApiKey) {
61
+ return { kind: "text", text: "OpenRouter API key is missing. Add it in Settings." };
62
+ }
63
+ const or = resolveOpenRouter(harness.config);
64
+ const prefs = readMediaPrefs();
65
+ try {
66
+ if (kind === "image") {
67
+ const media = await generateImage({
68
+ prompt,
69
+ model: prefs.imageModel,
70
+ apiKey: or.apiKey,
71
+ baseUrl: or.baseUrl,
72
+ });
73
+ const models = await mediaModels(harness, "image");
74
+ return {
75
+ kind: "media",
76
+ media,
77
+ caption: `Image · ${mediaModelLabel(models, prefs.imageModel)}`,
78
+ };
79
+ }
80
+ const media = await generateVideo({
81
+ prompt,
82
+ model: prefs.videoModel,
83
+ apiKey: or.apiKey,
84
+ baseUrl: or.baseUrl,
85
+ });
86
+ const models = await mediaModels(harness, "video");
87
+ return {
88
+ kind: "media",
89
+ media,
90
+ caption: `Clip · ${mediaModelLabel(models, prefs.videoModel)}`,
91
+ };
92
+ } catch (err) {
93
+ return { kind: "text", text: err instanceof Error ? err.message : String(err) };
94
+ }
95
+ }
96
+
97
+ export async function dispatchSlash(harness: HarnessRuntime, raw: string): Promise<SlashOutcome> {
98
+ const { cmd, arg } = parseSlashLine(raw);
99
+ switch (cmd) {
100
+ case "help":
101
+ return { kind: "text", text: SLASH_HELP };
102
+ case "skills":
103
+ return { kind: "text", text: formatSkillList(harness.skills) };
104
+ case "skill":
105
+ case "skill add":
106
+ case "skill find":
107
+ case "skill new":
108
+ case "skill remove": {
109
+ const payload = cmd === "skill" ? arg : `${cmd.slice("skill ".length)} ${arg}`.trim();
110
+ const parsed = parseSkillCommand(payload);
111
+ if ("error" in parsed) return { kind: "text", text: parsed.error };
112
+ if (parsed.action === "find") {
113
+ return { kind: "text", text: await harness.findSkills(parsed.query) };
114
+ }
115
+ if (parsed.action === "add") {
116
+ const result = await harness.addSkill(parsed.source, {
117
+ scope: parsed.scope,
118
+ ...(parsed.skill ? { skill: parsed.skill } : {}),
119
+ });
120
+ return { kind: "text", text: "error" in result ? result.error : `Loaded ${result.rel}` };
121
+ }
122
+ if (parsed.action === "remove") {
123
+ const result = harness.removeSkill(parsed.name);
124
+ return {
125
+ kind: "text",
126
+ text: result.ok ? `Removed ${parsed.name}` : (result.error ?? "Could not remove."),
127
+ };
128
+ }
129
+ if (parsed.action === "new") {
130
+ const result = await harness.authorSkill(parsed.name, parsed.brief);
131
+ return { kind: "text", text: "error" in result ? result.error : `Wrote ${result.rel}` };
132
+ }
133
+ return { kind: "text", text: SKILL_COMMAND_USAGE };
134
+ }
135
+ case "clear":
136
+ harness.setSession(undefined);
137
+ return { kind: "clear" };
138
+ case "model": {
139
+ if (arg) {
140
+ harness.setModel(arg);
141
+ return { kind: "applied" };
142
+ }
143
+ const live = harness.hasApiKey
144
+ ? await loadLiveTargetNames(resolveOpenRouter(harness.config))
145
+ : undefined;
146
+ const items = modelPickerItems(harness.modelId, live);
147
+ return {
148
+ kind: "picker",
149
+ title: "Model",
150
+ picker: "model",
151
+ items,
152
+ };
153
+ }
154
+ case "mode": {
155
+ if (arg) {
156
+ const next = tryParseAgentMode(arg);
157
+ if (!next) return { kind: "text", text: "Mode is ask, plan, or agent." };
158
+ harness.setMode(next);
159
+ return { kind: "applied" };
160
+ }
161
+ return {
162
+ kind: "picker",
163
+ title: "Mode",
164
+ picker: "mode",
165
+ items: AGENT_MODES.map((item) => ({ id: item, label: item })),
166
+ };
167
+ }
168
+ case "compact": {
169
+ const ok = await harness.compact();
170
+ return { kind: "text", text: ok ? "Compacted earlier turns." : "Nothing to compact." };
171
+ }
172
+ case "image": {
173
+ if (arg) return generateMediaOutcome(harness, "image", arg);
174
+ return {
175
+ kind: "picker",
176
+ title: "Image model",
177
+ picker: "image",
178
+ items: mediaPickerItems(await mediaModels(harness, "image"), readMediaPrefs().imageModel),
179
+ };
180
+ }
181
+ case "video": {
182
+ if (arg) return generateMediaOutcome(harness, "video", arg);
183
+ return {
184
+ kind: "picker",
185
+ title: "Video model",
186
+ picker: "video",
187
+ items: mediaPickerItems(await mediaModels(harness, "video"), readMediaPrefs().videoModel),
188
+ };
189
+ }
190
+ case "transcribe": {
191
+ const models = await mediaModels(harness, "transcription");
192
+ if (arg) {
193
+ writeMediaPrefs({ transcribeModel: arg });
194
+ return { kind: "applied" };
195
+ }
196
+ return {
197
+ kind: "picker",
198
+ title: "Speech model",
199
+ picker: "transcribe",
200
+ items: mediaPickerItems(models, readMediaPrefs().transcribeModel),
201
+ };
202
+ }
203
+ case "resume": {
204
+ const list = await harness.store.list();
205
+ if (list.length === 0) return { kind: "text", text: "No saved sessions." };
206
+ if (arg) {
207
+ const session = matchListedSession(list, arg) ?? (await harness.store.get(arg));
208
+ if (!session) return { kind: "text", text: `No session matches ${arg}` };
209
+ return { kind: "session", session };
210
+ }
211
+ return {
212
+ kind: "picker",
213
+ title: "Resume",
214
+ picker: "resume",
215
+ items: sessionPickerItems(list),
216
+ };
217
+ }
218
+ case "eval": {
219
+ const evalsDir = `${harness.cwd}/${harness.config.evalsDir}`;
220
+ if (!arg) {
221
+ const names = await listEvalSuites(evalsDir);
222
+ return { kind: "text", text: names.length ? names.join("\n") : "No eval suites." };
223
+ }
224
+ try {
225
+ const result = await runNamedEval({
226
+ config: harness.config,
227
+ cwd: harness.cwd,
228
+ name: arg,
229
+ skills: harness.skills,
230
+ chat: harness.chat,
231
+ observability: harness.observability,
232
+ });
233
+ return {
234
+ kind: "text",
235
+ text: `${result.name} ${result.ok ? "ok" : "fail"} · ${result.steps.map((s) => `${s.id}:${s.ok ? "ok" : "fail"}`).join(" ")}`,
236
+ };
237
+ } catch (err) {
238
+ return { kind: "text", text: err instanceof Error ? err.message : String(err) };
239
+ }
240
+ }
241
+ case "settings": {
242
+ const body = arg.trim();
243
+ if (!body || body === "key") {
244
+ const hint = harness.hasApiKey
245
+ ? `Key on this machine · ${maskSecret(process.env.OPENROUTER_API_KEY ?? "")}`
246
+ : "No key on this machine.";
247
+ return { kind: "text", text: `${hint}\n/settings key <token>` };
248
+ }
249
+ const token = body.startsWith("key ") ? body.slice(4).trim() : body;
250
+ if (!token) return { kind: "text", text: "/settings key <token>" };
251
+ writeOpenRouterKey(token);
252
+ harness.setApiKey(token);
253
+ return { kind: "text", text: `Key saved · ${maskSecret(token)}` };
254
+ }
255
+ case "exit":
256
+ case "quit":
257
+ return { kind: "exit" };
258
+ default:
259
+ return { kind: "text", text: `Unknown command /${cmd}. /help for the list.` };
260
+ }
261
+ }
262
+
263
+ export function applyPickerChoice(
264
+ harness: HarnessRuntime,
265
+ picker: SlashPickerKind,
266
+ id: string,
267
+ ): { kind: "applied" } | { kind: "text"; text: string } | { kind: "session-id"; id: string } {
268
+ if (picker === "model") {
269
+ harness.setModel(id);
270
+ return { kind: "applied" };
271
+ }
272
+ if (picker === "mode") {
273
+ const next = tryParseAgentMode(id);
274
+ if (!next) return { kind: "text", text: "Mode is ask, plan, or agent." };
275
+ harness.setMode(next);
276
+ return { kind: "applied" };
277
+ }
278
+ if (picker === "image") {
279
+ writeMediaPrefs({ imageModel: id });
280
+ return { kind: "applied" };
281
+ }
282
+ if (picker === "video") {
283
+ writeMediaPrefs({ videoModel: id });
284
+ return { kind: "applied" };
285
+ }
286
+ if (picker === "transcribe") {
287
+ writeMediaPrefs({ transcribeModel: id });
288
+ return { kind: "applied" };
289
+ }
290
+ return { kind: "session-id", id };
291
+ }
292
+
293
+ export async function runSlashLine(harness: HarnessRuntime, raw: string): Promise<SlashOutcome> {
294
+ const resolved = resolveSlashSubmit(raw);
295
+ if (resolved?.action === "hold") return { kind: "hold" };
296
+ if (resolved?.action === "complete") return { kind: "incomplete", draft: resolved.line };
297
+ return dispatchSlash(harness, resolved?.action === "send" ? resolved.line : raw);
298
+ }
@@ -0,0 +1,113 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { Skill } from "../skills/loader.ts";
3
+ import { plainTerminalText } from "./plain-text.ts";
4
+ import {
5
+ completeSlashCommand,
6
+ filterSlashCommands,
7
+ formatSkillList,
8
+ formatSlashCommand,
9
+ isHelpAlias,
10
+ parseSlashLine,
11
+ resolveSlashSubmit,
12
+ SLASH_COMMANDS,
13
+ SLASH_HELP,
14
+ } from "./slash.ts";
15
+
16
+ describe("slash palette", () => {
17
+ test("lists every command on /", () => {
18
+ expect(filterSlashCommands("/").map((item) => item.name)).toEqual(
19
+ SLASH_COMMANDS.map((item) => item.name),
20
+ );
21
+ });
22
+
23
+ test("filters as you type", () => {
24
+ expect(filterSlashCommands("/sk").map((item) => item.name)).toEqual([
25
+ "skills",
26
+ "skill add",
27
+ "skill find",
28
+ "skill new",
29
+ "skill remove",
30
+ ]);
31
+ expect(filterSlashCommands("/skill a").map((item) => item.name)).toEqual(["skill add"]);
32
+ expect(filterSlashCommands("/mode").map((item) => item.name)).toEqual(["model", "mode"]);
33
+ expect(filterSlashCommands("/help").map((item) => item.name)).toEqual(["help"]);
34
+ expect(filterSlashCommands("/image").map((item) => item.name)).toEqual(["image"]);
35
+ });
36
+
37
+ test("puts argument slots on the command, not the hint", () => {
38
+ const evalCmd = SLASH_COMMANDS.find((item) => item.name === "eval");
39
+ expect(evalCmd).toMatchObject({ slot: "[name]", hint: "run an eval suite" });
40
+ expect(formatSlashCommand(evalCmd!)).toBe("/eval [name]");
41
+ expect(SLASH_HELP).toContain("/eval [name] · run an eval suite");
42
+ expect(SLASH_HELP).toContain("/image [prompt] · generate an image");
43
+ expect(SLASH_HELP).not.toContain("/eval ·");
44
+ });
45
+
46
+ test("completes commands that take an argument with a trailing space", () => {
47
+ expect(completeSlashCommand({ name: "help", hint: "x" })).toBe("/help");
48
+ expect(completeSlashCommand({ name: "skill add", hint: "x", arg: true })).toBe("/skill add ");
49
+ });
50
+
51
+ test("parses two-word skill commands", () => {
52
+ expect(parseSlashLine("/skill add anthropics/skills@frontend-design")).toEqual({
53
+ cmd: "skill add",
54
+ arg: "anthropics/skills@frontend-design",
55
+ });
56
+ });
57
+
58
+ test("treats --help as a command, not a chat turn", () => {
59
+ expect(isHelpAlias("--help")).toBe(true);
60
+ expect(isHelpAlias("help")).toBe(true);
61
+ expect(isHelpAlias("what is help")).toBe(false);
62
+ });
63
+
64
+ test("lists skills as name and source only", () => {
65
+ const skills = [
66
+ {
67
+ name: "copywriting",
68
+ description: "Routes copy work through a long folded YAML description that must not dump.",
69
+ body: "# huge",
70
+ path: "/tmp/copywriting/SKILL.md",
71
+ relPath: "copywriting/SKILL.md",
72
+ source: "host",
73
+ },
74
+ ] satisfies Skill[];
75
+ expect(formatSkillList(skills)).toBe("copywriting host");
76
+ expect(formatSkillList(skills)).not.toContain("Routes copy");
77
+ });
78
+ });
79
+
80
+ describe("resolveSlashSubmit", () => {
81
+ test("keeps commands that still need an argument in the composer", () => {
82
+ expect(resolveSlashSubmit("/skill find")).toEqual({ action: "send", line: "/skill find" });
83
+ expect(resolveSlashSubmit("/skill add")).toEqual({ action: "complete", line: "/skill add " });
84
+ expect(resolveSlashSubmit("/skill find billing")).toEqual({
85
+ action: "send",
86
+ line: "/skill find billing",
87
+ });
88
+ });
89
+
90
+ test("completes a unique prefix instead of sending an unknown command", () => {
91
+ expect(resolveSlashSubmit("/ima")).toEqual({ action: "complete", line: "/image " });
92
+ expect(resolveSlashSubmit("/vid")).toEqual({ action: "complete", line: "/video " });
93
+ });
94
+
95
+ test("does not send a bare slash or an unknown prefix", () => {
96
+ expect(resolveSlashSubmit("/")).toEqual({ action: "hold" });
97
+ expect(resolveSlashSubmit("/asdf")).toEqual({ action: "hold" });
98
+ });
99
+
100
+ test("opens pickers for empty model, mode, and media commands", () => {
101
+ expect(resolveSlashSubmit("/image")).toEqual({ action: "send", line: "/image" });
102
+ expect(resolveSlashSubmit("/mode")).toEqual({ action: "send", line: "/mode" });
103
+ expect(resolveSlashSubmit("/settings")).toEqual({ action: "send", line: "/settings" });
104
+ });
105
+ });
106
+
107
+ describe("plainTerminalText", () => {
108
+ test("strips markdown markers", () => {
109
+ expect(plainTerminalText("I'm **caelence**, a `coding` agent")).toBe(
110
+ "I'm caelence, a coding agent",
111
+ );
112
+ });
113
+ });
@@ -0,0 +1,119 @@
1
+ export interface SlashCommand {
2
+ name: string;
3
+ hint: string;
4
+ arg?: boolean;
5
+ /** Placeholder shown after the name, e.g. `[name]`. */
6
+ slot?: string;
7
+ }
8
+
9
+ export const SLASH_COMMANDS: SlashCommand[] = [
10
+ { name: "help", hint: "this list" },
11
+ { name: "skills", hint: "loaded skills" },
12
+ { name: "skill add", hint: "install a bundled or skills.sh skill", arg: true },
13
+ { name: "skill find", hint: "search bundled skills and skills.sh", arg: true },
14
+ { name: "skill new", hint: "author a project skill", arg: true },
15
+ { name: "skill remove", hint: "remove a user skill", arg: true },
16
+ { name: "eval", hint: "run an eval suite", arg: true, slot: "[name]" },
17
+ { name: "clear", hint: "new session" },
18
+ { name: "resume", hint: "pick a session by title" },
19
+ { name: "model", hint: "pick a model" },
20
+ { name: "mode", hint: "ask, plan, or agent" },
21
+ { name: "compact", hint: "summarize earlier turns" },
22
+ { name: "image", hint: "generate an image", arg: true, slot: "[prompt]" },
23
+ { name: "video", hint: "generate a clip", arg: true, slot: "[prompt]" },
24
+ { name: "transcribe", hint: "speech to text model" },
25
+ { name: "settings", hint: "OpenRouter API key", arg: true },
26
+ { name: "exit", hint: "quit" },
27
+ ];
28
+
29
+ export function filterSlashCommands(raw: string): SlashCommand[] {
30
+ const text = raw.startsWith("/") ? raw.slice(1) : raw;
31
+ const body = text.trimStart().toLowerCase();
32
+ if (!body) return SLASH_COMMANDS;
33
+ return SLASH_COMMANDS.filter(
34
+ (item) => item.name.startsWith(body) || body.startsWith(`${item.name} `),
35
+ );
36
+ }
37
+
38
+ export function parseSlashLine(raw: string): { cmd: string; arg: string } {
39
+ const body = raw.replace(/^\//, "").trim();
40
+ const named = [...SLASH_COMMANDS].sort((a, b) => b.name.length - a.name.length);
41
+ const match = named.find((item) => body === item.name || body.startsWith(`${item.name} `));
42
+ if (match) return { cmd: match.name, arg: body.slice(match.name.length).trim() };
43
+ const [cmd, ...rest] = body.split(/\s+/);
44
+ return { cmd: cmd ?? "", arg: rest.join(" ") };
45
+ }
46
+
47
+ export function completeSlashCommand(command: SlashCommand): string {
48
+ return command.arg ? `/${command.name} ` : `/${command.name}`;
49
+ }
50
+
51
+ export function formatSlashCommand(command: SlashCommand): string {
52
+ return command.slot ? `/${command.name} ${command.slot}` : `/${command.name}`;
53
+ }
54
+
55
+ /** Empty submit opens a picker instead of requiring an argument. */
56
+ export const PICKS_WHEN_EMPTY = new Set([
57
+ "model",
58
+ "mode",
59
+ "resume",
60
+ "image",
61
+ "video",
62
+ "transcribe",
63
+ ]);
64
+
65
+ /** Empty submit stays in the composer until the argument is typed. */
66
+ export const REQUIRES_ARG = new Set(["skill add", "skill new", "skill remove"]);
67
+
68
+ export type SlashSubmit =
69
+ | { action: "send"; line: string }
70
+ | { action: "complete"; line: string }
71
+ | { action: "hold" };
72
+
73
+ /**
74
+ * Enter on a slash line. Commands that still need an argument stay in the
75
+ * composer. Unique prefixes complete. Unknown prefixes are not sent as chat.
76
+ */
77
+ export function resolveSlashSubmit(raw: string): SlashSubmit | null {
78
+ const trimmed = raw.trim();
79
+ if (!trimmed.startsWith("/") && !isHelpAlias(trimmed)) return null;
80
+ if (isHelpAlias(trimmed)) return { action: "send", line: "/help" };
81
+ if (trimmed === "/") return { action: "hold" };
82
+
83
+ const { cmd, arg } = parseSlashLine(trimmed);
84
+ const typed = trimmed.replace(/^\//, "").trim().toLowerCase();
85
+ const exact = SLASH_COMMANDS.find((item) => item.name === cmd);
86
+ if (exact && (typed === exact.name || typed.startsWith(`${exact.name} `))) {
87
+ if (!arg && REQUIRES_ARG.has(exact.name)) {
88
+ return { action: "complete", line: completeSlashCommand(exact) };
89
+ }
90
+ if (!arg && PICKS_WHEN_EMPTY.has(exact.name)) return { action: "send", line: trimmed };
91
+ return { action: "send", line: trimmed };
92
+ }
93
+
94
+ const matches = filterSlashCommands(trimmed);
95
+ if (matches.length === 1 && matches[0]) {
96
+ return { action: "complete", line: completeSlashCommand(matches[0]) };
97
+ }
98
+ return { action: "hold" };
99
+ }
100
+
101
+ export function draftAfterPicker(kind: string): string | undefined {
102
+ if (kind === "image") return "/image ";
103
+ if (kind === "video") return "/video ";
104
+ return undefined;
105
+ }
106
+
107
+ export function isHelpAlias(text: string): boolean {
108
+ const value = text.trim().toLowerCase();
109
+ return value === "help" || value === "--help" || value === "-h";
110
+ }
111
+
112
+ export function formatSkillList(skills: Array<{ name: string; source: string }>): string {
113
+ if (skills.length === 0) return "No skills loaded. /skill add copywriting or /skill find";
114
+ return skills.map((skill) => `${skill.name} ${skill.source}`).join("\n");
115
+ }
116
+
117
+ export const SLASH_HELP = SLASH_COMMANDS.map(
118
+ (item) => `${formatSlashCommand(item)} · ${item.hint}`,
119
+ ).join("\n");
@@ -0,0 +1,10 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { CAELENCE_THEME, PLAIN_THEME, resolveTheme } from "./theme.ts";
3
+
4
+ describe("theme", () => {
5
+ test("defaults to caelence", () => {
6
+ expect(resolveTheme(undefined).brand).toBe(CAELENCE_THEME.brand);
7
+ expect(resolveTheme("plain").name).toBe(PLAIN_THEME.name);
8
+ expect(resolveTheme({ ...CAELENCE_THEME, danger: "#ff0000" }).danger).toBe("#ff0000");
9
+ });
10
+ });
@@ -0,0 +1,44 @@
1
+ export type ThemeName = "caelence" | "plain";
2
+
3
+ export interface Theme {
4
+ name: string;
5
+ brand: string;
6
+ action: string;
7
+ ink: string;
8
+ muted: string;
9
+ line: string;
10
+ danger: string;
11
+ warning: string;
12
+ done: string;
13
+ }
14
+
15
+ /** Dark-terminal mapping of Caelence role colours. */
16
+ export const CAELENCE_THEME: Theme = {
17
+ name: "caelence",
18
+ brand: "#9BB35B",
19
+ action: "#9BB35B",
20
+ ink: "#E8E4DC",
21
+ muted: "#9A958C",
22
+ line: "#5C5A55",
23
+ danger: "#E4796E",
24
+ warning: "#D4A054",
25
+ done: "#9BB35B",
26
+ };
27
+
28
+ export const PLAIN_THEME: Theme = {
29
+ name: "plain",
30
+ brand: "green",
31
+ action: "green",
32
+ ink: "white",
33
+ muted: "gray",
34
+ line: "gray",
35
+ danger: "red",
36
+ warning: "yellow",
37
+ done: "green",
38
+ };
39
+
40
+ export function resolveTheme(theme: ThemeName | Theme | undefined): Theme {
41
+ if (!theme) return CAELENCE_THEME;
42
+ if (typeof theme === "string") return theme === "plain" ? PLAIN_THEME : CAELENCE_THEME;
43
+ return { ...CAELENCE_THEME, ...theme };
44
+ }
@@ -0,0 +1,33 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ clipLabel,
4
+ sidebarWidth,
5
+ takeVisibleLines,
6
+ tuiChromeRows,
7
+ visibleTranscriptCount,
8
+ } from "./tui-layout.ts";
9
+
10
+ describe("tui layout", () => {
11
+ test("shows a session rail only when the terminal is wide enough", () => {
12
+ expect(sidebarWidth(80)).toBe(0);
13
+ expect(sidebarWidth(88)).toBe(26);
14
+ });
15
+
16
+ test("keeps a minimum transcript viewport", () => {
17
+ expect(visibleTranscriptCount(12, 20)).toBe(3);
18
+ expect(visibleTranscriptCount(40, 10)).toBe(30);
19
+ });
20
+
21
+ test("clips to the newest lines", () => {
22
+ expect(takeVisibleLines(["a", "b", "c", "d"], 2)).toEqual(["c", "d"]);
23
+ });
24
+
25
+ test("clips session titles to the rail", () => {
26
+ expect(clipLabel("Billing copy for the new site", 12)).toBe("Billing cop…");
27
+ });
28
+
29
+ test("counts chrome for approval and pickers", () => {
30
+ expect(tuiChromeRows({ approval: false, pickerCount: 0, slashCount: 0 })).toBe(5);
31
+ expect(tuiChromeRows({ approval: true, pickerCount: 3, slashCount: 0 })).toBe(14);
32
+ });
33
+ });
@@ -0,0 +1,33 @@
1
+ export function sidebarWidth(columns: number): number {
2
+ return columns >= 88 ? 26 : 0;
3
+ }
4
+
5
+ export function visibleTranscriptCount(rows: number, chromeRows: number): number {
6
+ return Math.max(3, rows - Math.max(0, chromeRows));
7
+ }
8
+
9
+ export function takeVisibleLines<T>(lines: T[], count: number): T[] {
10
+ if (count <= 0 || lines.length <= count) return lines;
11
+ return lines.slice(lines.length - count);
12
+ }
13
+
14
+ export function clipLabel(text: string, width: number): string {
15
+ const value = text.replace(/\s+/g, " ").trim();
16
+ if (value.length <= width) return value;
17
+ if (width <= 1) return "…";
18
+ return `${value.slice(0, width - 1).trimEnd()}…`;
19
+ }
20
+
21
+ export function tuiChromeRows(input: {
22
+ approval: boolean;
23
+ pickerCount: number;
24
+ slashCount: number;
25
+ }): number {
26
+ return (
27
+ 3 +
28
+ 2 +
29
+ (input.approval ? 4 : 0) +
30
+ (input.pickerCount > 0 ? Math.min(input.pickerCount, 8) + 2 : 0) +
31
+ (input.slashCount > 0 ? Math.min(input.slashCount, 8) + 1 : 0)
32
+ );
33
+ }
@@ -0,0 +1,33 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { ENTER_ALT_SCREEN, enterAltScreen, LEAVE_ALT_SCREEN, SHOW_CURSOR } from "./tui-screen.ts";
3
+
4
+ describe("alternate screen", () => {
5
+ test("is a no-op when stdout is not a TTY", () => {
6
+ const writes: string[] = [];
7
+ const stdout = {
8
+ isTTY: false,
9
+ write(chunk: string) {
10
+ writes.push(chunk);
11
+ return true;
12
+ },
13
+ } as unknown as NodeJS.WriteStream;
14
+ const restore = enterAltScreen(stdout);
15
+ restore();
16
+ expect(writes).toEqual([]);
17
+ });
18
+
19
+ test("enters and leaves the alternate screen on a TTY", () => {
20
+ const writes: string[] = [];
21
+ const stdout = {
22
+ isTTY: true,
23
+ write(chunk: string) {
24
+ writes.push(chunk);
25
+ return true;
26
+ },
27
+ } as unknown as NodeJS.WriteStream;
28
+ const restore = enterAltScreen(stdout);
29
+ restore();
30
+ expect(writes[0]).toBe(ENTER_ALT_SCREEN);
31
+ expect(writes[1]).toBe(`${LEAVE_ALT_SCREEN}${SHOW_CURSOR}`);
32
+ });
33
+ });