Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

SkillsPanel.tsx

125 lines3.8 KB

1import { For, Show, createMemo } from 'solid-js'
2import { useAIStore } from '../../store/ai-context'
3import { useStyles } from '../../styles/use-styles'
4import type { SkillsSnapshot } from '../../store/skills-registry'
5import type { Message } from '../../store/ai-store'
6import type { Component } from 'solid-js'
7​
8function parseSkillName(argumentsJson: string | undefined): string | undefined {
9 if (!argumentsJson) return undefined
10 try {
11 const parsed: unknown = JSON.parse(argumentsJson)
12 if (
13 parsed &&
14 typeof parsed === 'object' &&
15 'name' in parsed &&
16 typeof parsed.name === 'string'
17 ) {
18 return parsed.name
19 }
20 } catch {
21 return undefined
22 }
23 return undefined
25​
26function loadedFromMessages(messages: Array<Message>): Array<string> {
27 const names = new Set<string>()
28 for (const message of messages) {
29 for (const toolCall of message.toolCalls ?? []) {
30 if (toolCall.name === 'load_skill') {
31 const name = parseSkillName(toolCall.arguments)
32 if (name) names.add(name)
33 }
34 }
35 for (const part of message.parts ?? []) {
36 if (part.type === 'tool-call' && part.toolName === 'load_skill') {
37 const name = parseSkillName(part.arguments)
38 if (name) names.add(name)
39 }
40 }
41 }
42 return [...names]
44​
45export const SkillsPanel: Component = () => {
46 const { state } = useAIStore()
47 const styles = useStyles()
48​
49 const snapshot = createMemo((): SkillsSnapshot | undefined => {
50 const hookId = state.hooks.activeHookId
51 if (hookId && state.skills.snapshots[hookId]) {
52 return state.skills.snapshots[hookId]
53 }
54 const first = Object.values(state.skills.snapshots)[0]
55 return first
56 })
57​
58 const conversation = createMemo(() => {
59 const hookId = state.hooks.activeHookId
60 if (!hookId) return undefined
61 return state.conversations[hookId]
62 })
63​
64 const loaded = createMemo(() => {
65 const fromSnap = new Set(snapshot()?.activated ?? [])
66 for (const name of loadedFromMessages(conversation()?.messages ?? [])) {
67 fromSnap.add(name)
68 }
69 return fromSnap
70 })
71​
72 const catalog = createMemo(() => snapshot()?.catalog ?? [])
73​
74 return (
75 <div
76 class={styles().memoryPanel.container}
77 data-testid="ai-devtools-skills-panel"
78 >
79 <Show
80 when={catalog().length > 0 || loaded().size > 0}
81 fallback={
82 <div class={styles().memoryPanel.empty}>
83 No skills on this run. Add `withSkills(...)` to `chat()` and the
84 catalog will appear here. A skill lights up when the model calls
85 `load_skill`.
86 </div>
87 }
88 >
89 <div class={styles().memoryPanel.section}>
90 <div class={styles().memoryPanel.sectionTitle}>Catalog</div>
91 <Show
92 when={catalog().length > 0}
93 fallback={
94 <div class={styles().memoryPanel.sectionEmpty}>
95 Loaded via `load_skill`, but the catalog snapshot has not
96 arrived yet.
97 </div>
98 }
99 >
100 <ul class={styles().memoryPanel.list}>
101 <For each={catalog()}>
102 {(skill) => {
103 const isLoaded = () => loaded().has(skill.name)
104 return (
105 <li class={styles().memoryPanel.row}>
106 <span class={styles().memoryPanel.badge}>
107 {skill.name}
108 </span>
109 <Show when={isLoaded()}>
110 <span class={styles().memoryPanel.badge}>loaded</span>
111 </Show>
112 <span class={styles().memoryPanel.sectionEmpty}>
113 {skill.description}
114 </span>
115 </li>
116 )
117 }}
118 </For>
119 </ul>
120 </Show>
121 </div>
122 </Show>
123 </div>
124 )