Skip to content

Commit 89ddb5f

Browse files
committed
feat(tui): split the context sidebar into instructions and skills
1 parent 342132b commit 89ddb5f

7 files changed

Lines changed: 199 additions & 47 deletions

File tree

‎PATCH.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Clicking a markdown link to a directory (trailing `/`, e.g. `[core](packages/cor
115115

116116
## Context Sidebar
117117

118-
Plugin (`custom.context` + TUI `custom.sidebar.context`): the sidebar lists ambient and session-injected context files for the current session. Two or fewer stay expanded; more collapse behind a click toggle.
118+
Plugin (`custom.context` + TUI `custom.sidebar.context`): the sidebar lists the current session's context in two subsections: **Instructions** (ambient and session-injected instruction files; home paths show as `~`, and a symlinked global config dir shows the configured path) and **Skills** (by name: mentions, `skill` messages, and completed `skill` tool calls in the post-compaction window, plus skills from `custom.skills.auto-load` via `custom-auto-load-skills`). Empty subsections are hidden; neither collapses.
119119

120120
## Pull Request Sidebar
121121

‎packages/core/src/plugin/custom/auto-load-skills.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * as CustomAutoLoadSkillsPlugin from "./auto-load-skills.js"
33
import { SystemPart } from "@opencode/ai"
44
import { define } from "@opencode/plugin/effect/plugin"
55
import type { SessionHooks } from "@opencode/plugin/effect/session"
6+
import { CustomAutoLoadSkills } from "@opencode/schema/custom-auto-load-skills"
67
import { Effect, Option, Schema } from "effect"
78

89
const Options = Schema.Struct({ skills: Schema.Array(Schema.String) })
@@ -16,10 +17,23 @@ export const Plugin = define({
1617
id: "custom.skills.auto-load",
1718
effect: Effect.fn("CustomAutoLoadSkillsPlugin")(function* (ctx) {
1819
const selection = names(ctx.options)
19-
if (selection.length === 0) return
2020
const selected = <T extends { readonly id: string; readonly name: string }>(skill: T) =>
2121
selection.includes(skill.id.toLowerCase()) || selection.includes(skill.name.toLowerCase())
2222

23+
// Backs the TUI context sidebar; registered even when nothing is configured so the call never fails.
24+
yield* ctx.rpc
25+
.register(CustomAutoLoadSkills.Contract, {
26+
list: (input) =>
27+
Effect.gen(function* () {
28+
const session = yield* ctx.session.get({ sessionID: input.sessionID })
29+
if (session.parentID) return { skills: [] }
30+
return { skills: (yield* ctx.skill.list()).data.filter(selected).map((skill) => skill.name) }
31+
}).pipe(Effect.orDie),
32+
})
33+
.pipe(Effect.orDie)
34+
35+
if (selection.length === 0) return
36+
2337
yield* ctx.skill.transform((editor) => {
2438
editor
2539
.list()

‎packages/core/src/plugin/custom/context.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export const Plugin = define({
2929
Effect.gen(function* () {
3030
const loaded = yield* discovery.list()
3131
const history = yield* ctx.session.context({ sessionID: input.sessionID })
32-
return contextFiles(location.project.directory, global.home, config, configReal, loaded, history)
32+
return {
33+
...contextFiles(location.project.directory, global.home, config, configReal, loaded, history),
34+
skills: contextSkills(history),
35+
}
3336
}).pipe(Effect.orDie),
3437
})
3538
.pipe(Effect.orDie)
@@ -59,6 +62,27 @@ export function contextFiles(
5962
}
6063
}
6164

65+
// History is the post-compaction window, so skills summarized away no longer count. Skills enter it as
66+
// user-message mentions, `skill` messages, or completed `skill` tool calls.
67+
export function contextSkills(history: ReadonlyArray<SessionMessage.Info>) {
68+
const names = history.flatMap((message) => {
69+
// Text compaction renders skills in its recent section as `[Skill activated: name]` (see messageToText).
70+
if (message.type === "compaction")
71+
return message.status === "completed"
72+
? Array.from(message.recent.matchAll(/^\[Skill activated: (.+)\]$/gm), (match) => match[1])
73+
: []
74+
if (message.type === "user") return message.skills?.map((skill) => skill.name) ?? []
75+
if (message.type === "skill") return [message.name]
76+
if (message.type !== "assistant") return []
77+
return message.content.flatMap((part) => {
78+
if (part.type !== "tool" || part.name !== "skill" || part.state.status !== "completed") return []
79+
const name = part.state.metadata?.name
80+
return typeof name === "string" ? [name] : []
81+
})
82+
})
83+
return [...new Set(names)]
84+
}
85+
6286
// Discovery stores real paths, so a config dir that is a symlink shows its target.
6387
// Display the path the user configured instead.
6488
function display(root: string, home: string, config: string, configReal: string, file: string) {

‎packages/core/test/plugin/custom.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { PluginHooks } from "@opencode/core/plugin/hooks"
1414
import { PluginHost } from "@opencode/core/plugin/host"
1515
import { AbsolutePath } from "@opencode/core/schema"
1616
import { Session } from "@opencode/core/session"
17+
import { Skill } from "@opencode/schema/skill"
1718
import { Tool } from "@opencode/schema/tool"
1819
import { SessionMessage } from "@opencode/schema/session-message"
1920
import { DateTime, Effect } from "effect"
@@ -108,6 +109,69 @@ it.effect("collects context files without duplicates and flags unavailable disco
108109
}),
109110
)
110111

112+
it.effect("collects loaded skills from mentions, skill messages, and completed skill tool calls", () =>
113+
Effect.sync(() => {
114+
const time = { created: DateTime.makeUnsafe(0) }
115+
const tool = (id: string, status: "completed" | "error", name?: string) =>
116+
SessionMessage.AssistantTool.make({
117+
type: "tool",
118+
id,
119+
name: "skill",
120+
state:
121+
status === "completed"
122+
? SessionMessage.ToolStateCompleted.make({
123+
status,
124+
input: {},
125+
content: [{ type: "text", text: "ok" }],
126+
metadata: name === undefined ? undefined : { name },
127+
})
128+
: SessionMessage.ToolStateError.make({
129+
status,
130+
input: {},
131+
error: { type: "tool", message: "failed" },
132+
}),
133+
time,
134+
})
135+
const history = [
136+
SessionMessage.User.make({
137+
id: SessionMessage.ID.make("msg_user"),
138+
type: "user",
139+
text: "use it",
140+
skills: [{ id: Skill.ID.make("alpha"), name: Skill.Name.make("alpha") }],
141+
time,
142+
}),
143+
SessionMessage.Skill.make({
144+
id: SessionMessage.ID.make("msg_skill"),
145+
type: "skill",
146+
skill: Skill.ID.make("beta"),
147+
name: Skill.Name.make("beta"),
148+
text: "beta body",
149+
time,
150+
}),
151+
SessionMessage.Assistant.make({
152+
id: SessionMessage.ID.make("msg_assistant"),
153+
type: "assistant",
154+
agent: Agent.ID.make("build"),
155+
model: { id: Model.ID.make("sonnet"), providerID: Provider.ID.make("anthropic") },
156+
content: [tool("call_1", "completed", "gamma"), tool("call_2", "error"), tool("call_3", "completed", "alpha")],
157+
time,
158+
}),
159+
]
160+
const compacted = SessionMessage.CompactionCompleted.make({
161+
id: SessionMessage.ID.make("msg_compaction"),
162+
type: "compaction",
163+
status: "completed",
164+
reason: "auto",
165+
summary: "[Skill activated: ignored]",
166+
recent: "[Skill activated: delta]\ndelta body\n\n[User]: hi\n[Skill activated: alpha]\nalpha body",
167+
time,
168+
})
169+
expect(CustomContextPlugin.contextSkills(history)).toEqual(["alpha", "beta", "gamma"])
170+
expect(CustomContextPlugin.contextSkills([compacted, ...history])).toEqual(["delta", "alpha", "beta", "gamma"])
171+
expect(CustomContextPlugin.contextSkills([])).toEqual([])
172+
}),
173+
)
174+
111175
it.effect("normalizes pull request hosts and always includes github.com", () =>
112176
Effect.sync(() => {
113177
expect(CustomPullRequestsPlugin.hostList(undefined)).toEqual(["github.com"])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export * as CustomAutoLoadSkills from "./custom-auto-load-skills.js"
2+
3+
import { Schema } from "effect"
4+
import { Rpc } from "./rpc.js"
5+
import { Session } from "./session.js"
6+
7+
const Input = Schema.toStandardSchemaV1(Schema.Struct({ sessionID: Session.ID }))
8+
const Output = Schema.toStandardSchemaV1(Schema.Struct({ skills: Schema.Array(Schema.String) }))
9+
10+
export const Contract = Rpc.define({
11+
id: "custom-auto-load-skills",
12+
methods: {
13+
list: { input: Input, output: Output },
14+
},
15+
events: {},
16+
})

‎packages/schema/src/custom-context.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { Session } from "./session.js"
66

77
const Input = Schema.toStandardSchemaV1(Schema.Struct({ sessionID: Session.ID }))
88
const Output = Schema.toStandardSchemaV1(
9-
Schema.Struct({ files: Schema.Array(Schema.String), unavailable: Schema.Boolean }),
9+
Schema.Struct({
10+
files: Schema.Array(Schema.String),
11+
skills: Schema.Array(Schema.String),
12+
unavailable: Schema.Boolean,
13+
}),
1014
)
1115

1216
export const Contract = Rpc.define({

‎packages/tui/src/feature-plugins/custom/sidebar-context.tsx‎

Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,91 @@
11
import { Plugin } from "@opencode/plugin/tui"
2+
import { CustomAutoLoadSkills } from "@opencode/schema/custom-auto-load-skills"
23
import { CustomContext } from "@opencode/schema/custom-context"
34
import { createMemo, createResource, For, Show } from "solid-js"
45

6+
function Section(props: { context: Plugin.Context; title: string; items: readonly string[]; last: boolean }) {
7+
const theme = props.context.theme
8+
9+
return (
10+
<box>
11+
<text fg={theme.text.muted} wrapMode="none" truncate>
12+
{props.last ? "└─ " : "├─ "}
13+
<span style={{ fg: theme.text.base }}>{props.title}</span>
14+
</text>
15+
<For each={props.items}>
16+
{(item, index) => (
17+
<text fg={theme.text.muted} wrapMode="none" truncate>
18+
{props.last ? " " : "│ "}
19+
{index() === props.items.length - 1 ? "└─ " : "├─ "}
20+
{item}
21+
</text>
22+
)}
23+
</For>
24+
</box>
25+
)
26+
}
27+
528
export function SidebarContext(props: { context: Plugin.Context; sessionID: string }) {
6-
const [view, updateView] = props.context.storage.store("view", { initial: { open: true } })
729
const theme = props.context.theme
830
const session = createMemo(() => props.context.data.session.get(props.sessionID))
931
const directory = createMemo(() => (session()?.location ?? props.context.location)?.directory)
10-
// Edited context files and nested loads both land in the transcript as system or synthetic messages.
11-
const revision = createMemo(
12-
() =>
13-
(props.context.data.session.message.list(props.sessionID) ?? []).filter(
14-
(message) => message.type === "synthetic" || message.type === "system",
15-
).length,
32+
// Edited instructions, nested loads, and skill loads land in the transcript as system, synthetic, user, or
33+
// skill messages, or as a completed skill tool call inside an assistant message that already exists.
34+
const revision = createMemo(() =>
35+
(props.context.data.session.message.list(props.sessionID) ?? []).reduce((count, message) => {
36+
if (message.type === "assistant")
37+
return count + message.content.filter((part) => part.type === "tool" && part.name === "skill" && part.state.status === "completed").length
38+
return message.type === "user" ||
39+
message.type === "skill" ||
40+
message.type === "synthetic" ||
41+
message.type === "system" ||
42+
message.type === "compaction"
43+
? count + 1
44+
: count
45+
}, 0),
1646
)
1747
const [result] = createResource(
1848
() => ({ sessionID: props.sessionID, directory: directory(), revision: revision() }),
19-
(source) =>
20-
props.context.client
21-
.rpc(CustomContext.Contract)
22-
.list({ sessionID: source.sessionID }, { location: { directory: source.directory } })
23-
.catch(() => undefined),
49+
(source) => {
50+
const input = { sessionID: source.sessionID }
51+
const options = { location: { directory: source.directory } }
52+
return Promise.all([
53+
props.context.client.rpc(CustomContext.Contract).list(input, options),
54+
props.context.client
55+
.rpc(CustomAutoLoadSkills.Contract)
56+
.list(input, options)
57+
.catch(() => ({ skills: [] })),
58+
])
59+
.then(([context, autoLoaded]) => ({
60+
...context,
61+
skills: [...new Set([...autoLoaded.skills, ...context.skills])],
62+
}))
63+
.catch(() => undefined)
64+
},
65+
)
66+
const sections = createMemo(() =>
67+
[
68+
{ title: "Instructions", items: result()?.files ?? [] },
69+
{ title: "Skills", items: result()?.skills ?? [] },
70+
].filter((section) => section.items.length > 0),
2471
)
25-
const files = createMemo(() => result()?.files ?? [])
2672

2773
return (
28-
<Show when={files().length > 0}>
74+
<Show when={sections().length > 0}>
2975
<box>
30-
<box
31-
flexDirection="row"
32-
gap={1}
33-
onMouseDown={() => {
34-
if (files().length <= 2) return
35-
void updateView((draft) => {
36-
draft.open = !draft.open
37-
}).catch((error) => console.error("Failed to persist context sidebar state", error))
38-
}}
39-
>
40-
<Show when={files().length > 2}>
41-
<text fg={theme.text.base}>{view.open ? "▼" : "▶"}</text>
42-
</Show>
43-
<text fg={theme.text.base}>
44-
<b>Context</b>
45-
<Show when={files().length > 2 && !view.open}>
46-
<span style={{ fg: theme.text.muted }}> ({files().length})</span>
47-
</Show>
48-
</text>
49-
</box>
50-
<Show when={files().length <= 2 || view.open}>
51-
<For each={files()}>
52-
{(file) => (
53-
<text fg={theme.text.muted} wrapMode="none" truncate>
54-
{file}
55-
</text>
56-
)}
57-
</For>
58-
</Show>
76+
<text fg={theme.text.base}>
77+
<b>Context</b>
78+
</text>
79+
<For each={sections()}>
80+
{(section, index) => (
81+
<Section
82+
context={props.context}
83+
title={section.title}
84+
items={section.items}
85+
last={index() === sections().length - 1}
86+
/>
87+
)}
88+
</For>
5989
</box>
6090
</Show>
6191
)

0 commit comments

Comments
 (0)