|
1 | 1 | import { Plugin } from "@opencode/plugin/tui" |
| 2 | +import { CustomAutoLoadSkills } from "@opencode/schema/custom-auto-load-skills" |
2 | 3 | import { CustomContext } from "@opencode/schema/custom-context" |
3 | 4 | import { createMemo, createResource, For, Show } from "solid-js" |
4 | 5 |
|
| 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 | + |
5 | 28 | export function SidebarContext(props: { context: Plugin.Context; sessionID: string }) { |
6 | | - const [view, updateView] = props.context.storage.store("view", { initial: { open: true } }) |
7 | 29 | const theme = props.context.theme |
8 | 30 | const session = createMemo(() => props.context.data.session.get(props.sessionID)) |
9 | 31 | 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), |
16 | 46 | ) |
17 | 47 | const [result] = createResource( |
18 | 48 | () => ({ 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), |
24 | 71 | ) |
25 | | - const files = createMemo(() => result()?.files ?? []) |
26 | 72 |
|
27 | 73 | return ( |
28 | | - <Show when={files().length > 0}> |
| 74 | + <Show when={sections().length > 0}> |
29 | 75 | <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> |
59 | 89 | </box> |
60 | 90 | </Show> |
61 | 91 | ) |
|
0 commit comments