|
1 | | -export type ContextItem = { |
2 | | - key: string; |
3 | | - label: string; |
4 | | - tooltip: string; |
| 1 | +import type { AccountInfo } from "@hypr/plugin-auth"; |
| 2 | +import type { DeviceInfo } from "@hypr/plugin-misc"; |
| 3 | +import type { ChatContext } from "@hypr/plugin-template"; |
| 4 | + |
| 5 | +import type { HyprUIMessage } from "./types"; |
| 6 | +import { isRecord } from "./utils"; |
| 7 | + |
| 8 | +export type ContextEntity = |
| 9 | + | { |
| 10 | + kind: "session"; |
| 11 | + key: string; |
| 12 | + chatContext: ChatContext; |
| 13 | + wordCount?: number; |
| 14 | + rawNotePreview?: string; |
| 15 | + participantCount?: number; |
| 16 | + eventTitle?: string; |
| 17 | + removable?: boolean; |
| 18 | + } |
| 19 | + | ({ kind: "account"; key: string } & Partial<AccountInfo>) |
| 20 | + | ({ |
| 21 | + kind: "device"; |
| 22 | + key: string; |
| 23 | + } & Partial<DeviceInfo>); |
| 24 | + |
| 25 | +export type ContextEntityKind = ContextEntity["kind"]; |
| 26 | + |
| 27 | +type ToolOutputAvailablePart = { |
| 28 | + type: string; |
| 29 | + state: "output-available"; |
| 30 | + output?: unknown; |
| 31 | +}; |
| 32 | + |
| 33 | +function isToolOutputAvailablePart( |
| 34 | + value: unknown, |
| 35 | +): value is ToolOutputAvailablePart { |
| 36 | + return ( |
| 37 | + isRecord(value) && |
| 38 | + typeof value.type === "string" && |
| 39 | + value.state === "output-available" |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function parseSearchSessionsOutput(output: unknown): ContextEntity[] { |
| 44 | + if (!isRecord(output) || !Array.isArray(output.results)) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + return output.results.flatMap((item): ContextEntity[] => { |
| 49 | + if (!isRecord(item)) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + if (typeof item.id !== "string" && typeof item.id !== "number") { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + const title = typeof item.title === "string" ? item.title : null; |
| 58 | + const content = typeof item.content === "string" ? item.content : null; |
| 59 | + |
| 60 | + return [ |
| 61 | + { |
| 62 | + kind: "session", |
| 63 | + key: `session:search:${item.id}`, |
| 64 | + chatContext: { |
| 65 | + title, |
| 66 | + date: null, |
| 67 | + rawContent: content, |
| 68 | + enhancedContent: null, |
| 69 | + transcript: null, |
| 70 | + }, |
| 71 | + rawNotePreview: content?.slice(0, 120) ?? undefined, |
| 72 | + removable: true, |
| 73 | + }, |
| 74 | + ]; |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +const toolEntityExtractors: Record< |
| 79 | + string, |
| 80 | + (output: unknown) => ContextEntity[] |
| 81 | +> = { |
| 82 | + search_sessions: parseSearchSessionsOutput, |
5 | 83 | }; |
6 | 84 |
|
7 | | -export type ContextSource = |
8 | | - | { type: "account"; email?: string; userId?: string } |
9 | | - | { type: "device" } |
10 | | - | { type: "session"; title?: string; date?: string } |
11 | | - | { type: "transcript"; wordCount?: number } |
12 | | - | { type: "note"; preview?: string }; |
| 85 | +export function extractToolContextEntities( |
| 86 | + messages: Array<Pick<HyprUIMessage, "parts">>, |
| 87 | +): ContextEntity[] { |
| 88 | + const seen = new Set<string>(); |
| 89 | + const entities: ContextEntity[] = []; |
| 90 | + |
| 91 | + for (const message of messages) { |
| 92 | + if (!Array.isArray(message.parts)) continue; |
| 93 | + for (const part of message.parts) { |
| 94 | + if (!isToolOutputAvailablePart(part) || !part.type.startsWith("tool-")) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + const toolName = part.type.slice(5); |
| 99 | + const extractor = toolEntityExtractors[toolName]; |
| 100 | + if (!extractor) continue; |
| 101 | + |
| 102 | + for (const entity of extractor(part.output)) { |
| 103 | + if (!seen.has(entity.key)) { |
| 104 | + seen.add(entity.key); |
| 105 | + entities.push(entity); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return entities; |
| 112 | +} |
0 commit comments