@surfaces-ui/chat 0.0.0-stage → 0.2.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 (61) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +40 -2
  4. package/dist/AnalyticsChat.d.ts +82 -0
  5. package/dist/AnalyticsChat.js +168 -0
  6. package/dist/AnalyticsChat.js.map +1 -0
  7. package/dist/Approval.d.ts +18 -0
  8. package/dist/Approval.js +116 -0
  9. package/dist/Approval.js.map +1 -0
  10. package/dist/Chat.d.ts +112 -0
  11. package/dist/Chat.js +119 -0
  12. package/dist/Chat.js.map +1 -0
  13. package/dist/ChatComposer.d.ts +117 -0
  14. package/dist/ChatComposer.js +186 -0
  15. package/dist/ChatComposer.js.map +1 -0
  16. package/dist/ChatPanels.d.ts +34 -0
  17. package/dist/ChatPanels.js +75 -0
  18. package/dist/ChatPanels.js.map +1 -0
  19. package/dist/DataPanel.d.ts +19 -0
  20. package/dist/DataPanel.js +20 -0
  21. package/dist/DataPanel.js.map +1 -0
  22. package/dist/Menu.d.ts +37 -0
  23. package/dist/Menu.js +85 -0
  24. package/dist/Menu.js.map +1 -0
  25. package/dist/PageStudio.d.ts +103 -0
  26. package/dist/PageStudio.js +166 -0
  27. package/dist/PageStudio.js.map +1 -0
  28. package/dist/SessionList.d.ts +64 -0
  29. package/dist/SessionList.js +225 -0
  30. package/dist/SessionList.js.map +1 -0
  31. package/dist/appComponents.d.ts +6 -0
  32. package/dist/appComponents.js +100 -0
  33. package/dist/appComponents.js.map +1 -0
  34. package/dist/conversation.d.ts +66 -0
  35. package/dist/conversation.js +119 -0
  36. package/dist/conversation.js.map +1 -0
  37. package/dist/icons.d.ts +33 -0
  38. package/dist/icons.js +34 -0
  39. package/dist/icons.js.map +1 -0
  40. package/dist/index.d.ts +18 -0
  41. package/dist/index.js +19 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/selectors.d.ts +106 -0
  44. package/dist/selectors.js +160 -0
  45. package/dist/selectors.js.map +1 -0
  46. package/dist/sessions.d.ts +71 -0
  47. package/dist/sessions.js +194 -0
  48. package/dist/sessions.js.map +1 -0
  49. package/dist/speech.d.ts +43 -0
  50. package/dist/speech.js +89 -0
  51. package/dist/speech.js.map +1 -0
  52. package/dist/thread.d.ts +199 -0
  53. package/dist/thread.js +225 -0
  54. package/dist/thread.js.map +1 -0
  55. package/dist/useChat.d.ts +121 -0
  56. package/dist/useChat.js +293 -0
  57. package/dist/useChat.js.map +1 -0
  58. package/dist/voice.d.ts +41 -0
  59. package/dist/voice.js +128 -0
  60. package/dist/voice.js.map +1 -0
  61. package/package.json +71 -3
@@ -0,0 +1,119 @@
1
+ import { agentEvent, scriptedAgent } from "@surfaces-ui/agent";
2
+ import { StepStatus } from "@surfaces-ui/proto";
3
+ import { A2UI_VERSION, scriptEventReply } from "@surfaces-ui/spec";
4
+ import { emptyThread, parseThread, reduceThread } from "./thread.js";
5
+ /** A thread as a document to keep. A reply still streaming is kept as it stands; the session and usage are not kept. */
6
+ export function conversationDocument(thread, meta) {
7
+ const plain = JSON.parse(JSON.stringify({ messages: thread.messages.map((m) => ({ ...m, streaming: false })), surfaces: thread.surfaces, suggestions: thread.suggestions }));
8
+ return { type: "conversation", id: meta.id, title: meta.title, ...(meta.description ? { description: meta.description } : {}), thread: plain };
9
+ }
10
+ /** Checks a conversation document from storage, a library or anywhere else before it is used. Undefined for anything that is not one. */
11
+ export function parseConversation(input) {
12
+ if (!input || typeof input !== "object" || Array.isArray(input))
13
+ return undefined;
14
+ const record = input;
15
+ if (record.type !== "conversation" || typeof record.title !== "string")
16
+ return undefined;
17
+ const thread = parseThread(record.thread);
18
+ if (!thread)
19
+ return undefined;
20
+ return {
21
+ type: "conversation",
22
+ id: typeof record.id === "string" ? record.id : "",
23
+ title: record.title,
24
+ ...(typeof record.description === "string" && record.description ? { description: record.description } : {}),
25
+ thread: { messages: thread.messages, surfaces: thread.surfaces, suggestions: thread.suggestions },
26
+ };
27
+ }
28
+ /** A conversation document's thread, ready for `Chat`'s `initialThread` or `MessageList`. */
29
+ export function conversationThread(document) {
30
+ return { ...emptyThread, messages: document.thread.messages, surfaces: document.thread.surfaces, suggestions: document.thread.suggestions };
31
+ }
32
+ /** The events one written reply becomes, ending in `done`. */
33
+ export function scriptEvents(reply, options) {
34
+ const events = [];
35
+ let steps = 0;
36
+ let previous;
37
+ for (const part of reply) {
38
+ // Text arrives as pieces of one stream, so two written paragraphs in a row need a break between them.
39
+ if ("text" in part)
40
+ events.push(agentEvent.text(previous && "text" in previous ? `\n\n${part.text}` : part.text));
41
+ else if ("steps" in part) {
42
+ for (const step of part.steps) {
43
+ const id = `s${++steps}`;
44
+ events.push(agentEvent.step({ id, title: step.title, status: StepStatus.RUNNING, tool: step.tool ?? "" }));
45
+ events.push(agentEvent.step({ id, title: step.title, status: StepStatus.DONE, detail: step.detail ?? "", tool: step.tool ?? "" }));
46
+ }
47
+ }
48
+ else {
49
+ const { id, components, data } = part.surface;
50
+ events.push(agentEvent.surface({ version: A2UI_VERSION, createSurface: { surfaceId: id, catalogId: options.catalogId } }));
51
+ events.push(agentEvent.surface({ version: A2UI_VERSION, updateComponents: { surfaceId: id, components } }));
52
+ if (data !== undefined)
53
+ events.push(agentEvent.surface({ version: A2UI_VERSION, updateDataModel: { surfaceId: id, path: "/", value: data } }));
54
+ }
55
+ previous = part;
56
+ }
57
+ if (options.suggestions?.length)
58
+ events.push(agentEvent.suggestions(options.suggestions));
59
+ events.push(agentEvent.done(options.sessionId ?? "script", options.answeredBy));
60
+ return events;
61
+ }
62
+ /** What is offered after a turn: its own suggestions, or what the person says next in the script. */
63
+ const offered = (script, index) => script.turns[index]?.suggestions ?? (script.turns[index + 1] ? [script.turns[index + 1].say] : []);
64
+ /**
65
+ * A written conversation as a thread, already played: the first `turns`
66
+ * turns (all of them by default), each folded through `reduceThread` exactly
67
+ * as a live reply would be, so what is drawn is what a real agent sending
68
+ * the same content would draw.
69
+ */
70
+ export function scriptThread(script, catalog, turns = script.turns.length) {
71
+ let thread = emptyThread;
72
+ script.turns.slice(0, Math.max(0, turns)).forEach((turn, index) => {
73
+ thread = reduceThread(thread, { type: "user", text: turn.say }, catalog);
74
+ thread = reduceThread(thread, { type: "begin" }, catalog);
75
+ for (const event of scriptEvents(turn.reply, { catalogId: catalog.id, answeredBy: script.answeredBy, suggestions: offered(script, index) }))
76
+ thread = reduceThread(thread, { type: "event", event }, catalog);
77
+ thread = reduceThread(thread, { type: "end" }, catalog);
78
+ });
79
+ return thread;
80
+ }
81
+ const plain = (text) => text.trim().toLowerCase().replace(/\s+/g, " ");
82
+ /**
83
+ * An agent that answers from a written conversation: a message that matches
84
+ * a turn's `say` gets that turn's reply, an event from a drawn component gets
85
+ * the script's answer to it (`events`), and anything else is told what the
86
+ * script can answer. It is not a model and its replies say so through
87
+ * `answeredBy`. Use it to let people carry on a sample conversation.
88
+ */
89
+ export function scriptAgent(script, options) {
90
+ const base = { catalogId: options.catalog.id, answeredBy: script.answeredBy };
91
+ // The furthest turn played, so an answer to an event can offer the way on again.
92
+ let next = Math.max(0, options.played ?? 0);
93
+ const onward = () => (script.turns[next] ? [script.turns[next].say] : []);
94
+ return scriptedAgent(function* (request) {
95
+ const input = request.input;
96
+ if (input?.case === "text") {
97
+ const index = script.turns.findIndex((turn) => plain(turn.say) === plain(input.value));
98
+ if (index >= 0) {
99
+ next = Math.max(next, index + 1);
100
+ yield* scriptEvents(script.turns[index].reply, { ...base, suggestions: offered(script, index) });
101
+ }
102
+ else
103
+ yield* scriptEvents([{ text: "This conversation is written out in advance, so it can only answer what it was written for. Pick one of the suggestions below, or press a control in a component above." }], {
104
+ ...base,
105
+ suggestions: script.turns.map((turn) => turn.say),
106
+ });
107
+ }
108
+ else if (input?.case === "action") {
109
+ const context = (input.value.context ?? {});
110
+ const name = input.value.name ?? "";
111
+ const reply = scriptEventReply(script, name, context) ?? options.onAction?.({ name, surfaceId: input.value.surfaceId ?? "", context });
112
+ yield* scriptEvents(reply ?? [{ text: `Noted (${input.value.name ?? "event"}). This conversation is written out in advance, so nothing else happens.` }], { ...base, suggestions: onward() });
113
+ }
114
+ else {
115
+ yield* scriptEvents([{ text: "This conversation is written out in advance and asks for no approvals." }], base);
116
+ }
117
+ }, options.delayMs ?? 0);
118
+ }
119
+ //# sourceMappingURL=conversation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.js","sourceRoot":"","sources":["../src/conversation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,UAAU,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnG,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAA0D,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC3H,OAAO,EAAmB,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAiBtF,wHAAwH;AACxH,MAAM,UAAU,oBAAoB,CAAC,MAAkB,EAAE,IAAyD;IAChH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAmC,CAAC;IAC/M,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC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,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC;iBAClD,CAAC,CAAC;QACP,CAAC;aAAM,IAAI,KAAK,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;YACpC,MAAM,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,IAAI,EAAE,CAA4B,CAAC;YACvE,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;YACpC,MAAM,KAAK,GAAG,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;YACvI,KAAK,CAAC,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC,EAAE,IAAI,EAAE,UAAU,KAAK,CAAC,KAAK,CAAC,IAAI,IAAI,OAAO,0EAA0E,EAAE,CAAC,EAAE,EAAE,GAAG,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAChM,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,wEAAwE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QAClH,CAAC;IACH,CAAC,EAAE,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { type AgentBackend, type AgentEvent, agentEvent, scriptedAgent } from \"@surfaces-ui/agent\";\nimport type { JsonObject } from \"@bufbuild/protobuf\";\nimport { StepStatus } from \"@surfaces-ui/proto\";\nimport { A2UI_VERSION, type Catalog, type ConversationScript, type ScriptPart, scriptEventReply } from \"@surfaces-ui/spec\";\nimport { type ChatThread, emptyThread, parseThread, reduceThread } from \"./thread.js\";\n\n/**\n * A conversation as a document: a title and the thread, as plain JSON. It\n * can be saved beside pages in a surface library (kind \"conversation\"),\n * opened again with `Chat`'s `initialThread`, and shown to someone else.\n * Whatever is in it is checked again when it is opened (`parseConversation`)\n * and when its surfaces are drawn.\n */\nexport interface ConversationDocument {\n type: \"conversation\";\n id: string;\n title: string;\n description?: string;\n thread: Pick<ChatThread, \"messages\" | \"surfaces\" | \"suggestions\">;\n}\n\n/** A thread as a document to keep. A reply still streaming is kept as it stands; the session and usage are not kept. */\nexport function conversationDocument(thread: ChatThread, meta: { id: string; title: string; description?: string }): ConversationDocument {\n const plain = JSON.parse(JSON.stringify({ messages: thread.messages.map((m) => ({ ...m, streaming: false })), surfaces: thread.surfaces, suggestions: thread.suggestions })) as ConversationDocument[\"thread\"];\n return { type: \"conversation\", id: meta.id, title: meta.title, ...(meta.description ? { description: meta.description } : {}), thread: plain };\n}\n\n/** Checks a conversation document from storage, a library or anywhere else before it is used. Undefined for anything that is not one. */\nexport function parseConversation(input: unknown): ConversationDocument | undefined {\n if (!input || typeof input !== \"object\" || Array.isArray(input)) return undefined;\n const record = input as Record<string, unknown>;\n if (record.type !== \"conversation\" || typeof record.title !== \"string\") return undefined;\n const thread = parseThread(record.thread);\n if (!thread) return undefined;\n return {\n type: \"conversation\",\n id: typeof record.id === \"string\" ? record.id : \"\",\n title: record.title,\n ...(typeof record.description === \"string\" && record.description ? { description: record.description } : {}),\n thread: { messages: thread.messages, surfaces: thread.surfaces, suggestions: thread.suggestions },\n };\n}\n\n/** A conversation document's thread, ready for `Chat`'s `initialThread` or `MessageList`. */\nexport function conversationThread(document: ConversationDocument): ChatThread {\n return { ...emptyThread, messages: document.thread.messages, surfaces: document.thread.surfaces, suggestions: document.thread.suggestions };\n}\n\n/** The events one written reply becomes, ending in `done`. */\nexport function scriptEvents(reply: ScriptPart[], options: { catalogId: string; answeredBy: string; suggestions?: string[]; sessionId?: string }): AgentEvent[] {\n const events: AgentEvent[] = [];\n let steps = 0;\n let previous: ScriptPart | undefined;\n for (const part of reply) {\n // Text arrives as pieces of one stream, so two written paragraphs in a row need a break between them.\n if (\"text\" in part) events.push(agentEvent.text(previous && \"text\" in previous ? `\\n\\n${part.text}` : part.text));\n else if (\"steps\" in part) {\n for (const step of part.steps) {\n const id = `s${++steps}`;\n events.push(agentEvent.step({ id, title: step.title, status: StepStatus.RUNNING, tool: step.tool ?? \"\" }));\n events.push(agentEvent.step({ id, title: step.title, status: StepStatus.DONE, detail: step.detail ?? \"\", tool: step.tool ?? \"\" }));\n }\n } else {\n const { id, components, data } = part.surface;\n events.push(agentEvent.surface({ version: A2UI_VERSION, createSurface: { surfaceId: id, catalogId: options.catalogId } }));\n events.push(agentEvent.surface({ version: A2UI_VERSION, updateComponents: { surfaceId: id, components } } as unknown as JsonObject));\n if (data !== undefined) events.push(agentEvent.surface({ version: A2UI_VERSION, updateDataModel: { surfaceId: id, path: \"/\", value: data } } as unknown as JsonObject));\n }\n previous = part;\n }\n if (options.suggestions?.length) events.push(agentEvent.suggestions(options.suggestions));\n events.push(agentEvent.done(options.sessionId ?? \"script\", options.answeredBy));\n return events;\n}\n\n/** What is offered after a turn: its own suggestions, or what the person says next in the script. */\nconst offered = (script: ConversationScript, index: number): string[] => script.turns[index]?.suggestions ?? (script.turns[index + 1] ? [script.turns[index + 1]!.say] : []);\n\n/**\n * A written conversation as a thread, already played: the first `turns`\n * turns (all of them by default), each folded through `reduceThread` exactly\n * as a live reply would be, so what is drawn is what a real agent sending\n * the same content would draw.\n */\nexport function scriptThread(script: ConversationScript, catalog: Catalog, turns = script.turns.length): ChatThread {\n let thread = emptyThread;\n script.turns.slice(0, Math.max(0, turns)).forEach((turn, index) => {\n thread = reduceThread(thread, { type: \"user\", text: turn.say }, catalog);\n thread = reduceThread(thread, { type: \"begin\" }, catalog);\n for (const event of scriptEvents(turn.reply, { catalogId: catalog.id, answeredBy: script.answeredBy, suggestions: offered(script, index) })) thread = reduceThread(thread, { type: \"event\", event }, catalog);\n thread = reduceThread(thread, { type: \"end\" }, catalog);\n });\n return thread;\n}\n\nexport interface ScriptAgentOptions {\n catalog: Catalog;\n /** Pause between events, so a reply arrives a piece at a time. */\n delayMs?: number;\n /** How many turns the thread it carries on from has already played (`scriptThread`'s `turns`), so it knows what to offer next. */\n played?: number;\n /**\n * What to answer when a control in a drawn component sends an event the\n * script's own `events` do not cover. Without an answer the event is\n * acknowledged in a line.\n */\n onAction?(action: { name: string; surfaceId: string; context: Record<string, unknown> }): ScriptPart[] | undefined;\n}\n\nconst plain = (text: string) => text.trim().toLowerCase().replace(/\\s+/g, \" \");\n\n/**\n * An agent that answers from a written conversation: a message that matches\n * a turn's `say` gets that turn's reply, an event from a drawn component gets\n * the script's answer to it (`events`), and anything else is told what the\n * script can answer. It is not a model and its replies say so through\n * `answeredBy`. Use it to let people carry on a sample conversation.\n */\nexport function scriptAgent(script: ConversationScript, options: ScriptAgentOptions): AgentBackend {\n const base = { catalogId: options.catalog.id, answeredBy: script.answeredBy };\n // The furthest turn played, so an answer to an event can offer the way on again.\n let next = Math.max(0, options.played ?? 0);\n const onward = () => (script.turns[next] ? [script.turns[next]!.say] : []);\n return scriptedAgent(function* (request) {\n const input = request.input;\n if (input?.case === \"text\") {\n const index = script.turns.findIndex((turn) => plain(turn.say) === plain(input.value));\n if (index >= 0) {\n next = Math.max(next, index + 1);\n yield* scriptEvents(script.turns[index]!.reply, { ...base, suggestions: offered(script, index) });\n }\n else\n yield* scriptEvents([{ text: \"This conversation is written out in advance, so it can only answer what it was written for. Pick one of the suggestions below, or press a control in a component above.\" }], {\n ...base,\n suggestions: script.turns.map((turn) => turn.say),\n });\n } else if (input?.case === \"action\") {\n const context = (input.value.context ?? {}) as Record<string, unknown>;\n const name = input.value.name ?? \"\";\n const reply = scriptEventReply(script, name, context) ?? options.onAction?.({ name, surfaceId: input.value.surfaceId ?? \"\", context });\n yield* scriptEvents(reply ?? [{ text: `Noted (${input.value.name ?? \"event\"}). This conversation is written out in advance, so nothing else happens.` }], { ...base, suggestions: onward() });\n } else {\n yield* scriptEvents([{ text: \"This conversation is written out in advance and asks for no approvals.\" }], base);\n }\n }, options.delayMs ?? 0);\n}\n"]}
@@ -0,0 +1,33 @@
1
+ /** Small outline icons for the chat's controls, drawn in the text colour at the text's size. */
2
+ declare const PATHS: {
3
+ readonly plus: "M12 5v14 M5 12h14";
4
+ readonly send: "M12 19V5 M6 11l6-6 6 6";
5
+ readonly stop: "M7 7h10v10H7z";
6
+ readonly mic: "M12 4a3 3 0 0 0-3 3v5a3 3 0 0 0 6 0V7a3 3 0 0 0-3-3z M6 11a6 6 0 0 0 12 0 M12 17v3";
7
+ readonly talk: "M4 12h2 M8 8v8 M12 5v14 M16 8v8 M20 12h-2";
8
+ readonly chevron: "M7 10l5 5 5-5";
9
+ readonly next: "M10 7l5 5-5 5";
10
+ readonly back: "M14 7l-5 5 5 5";
11
+ readonly data: "M5 6c0-1.7 3.1-3 7-3s7 1.3 7 3-3.1 3-7 3-7-1.3-7-3z M5 6v12c0 1.7 3.1 3 7 3s7-1.3 7-3V6 M5 12c0 1.7 3.1 3 7 3s7-1.3 7-3";
12
+ readonly file: "M6 3h8l4 4v14H6z M14 3v4h4";
13
+ readonly skill: "M5 5h9a3 3 0 0 1 3 3v12H8a3 3 0 0 1-3-3z M5 17a3 3 0 0 1 3-3h9";
14
+ readonly plug: "M9 3v5 M15 3v5 M7 8h10v3a5 5 0 0 1-10 0z M12 16v5";
15
+ readonly agent: "M4 7h9M17 7h3M4 17h3M11 17h9M15 5v4M9 15v4";
16
+ readonly model: "M12 3l8 4.5v9L12 21l-8-4.5v-9z M12 12l8-4.5 M12 12v9 M12 12L4 7.5";
17
+ readonly sessions: "M4 6h16 M4 12h16 M4 18h10";
18
+ readonly chat: "M4 5h16v11H10l-5 4v-4H4z";
19
+ readonly close: "M6 6l12 12 M18 6L6 18";
20
+ readonly check: "M5 12l5 5 9-10";
21
+ readonly pin: "M9 4h6l-1 6 3 3H7l3-3z M12 13v7";
22
+ readonly search: "M11 5a6 6 0 1 0 0 12 6 6 0 0 0 0-12z M20 20l-4.5-4.5";
23
+ readonly more: "M4.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M10.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M16.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0";
24
+ readonly bell: "M18 16v-5a6 6 0 0 0-12 0v5l-2 2h16z M10 21a2 2 0 0 0 4 0";
25
+ readonly about: "M12 4a8 8 0 1 0 0 16 8 8 0 0 0 0-16z M12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z";
26
+ };
27
+ export type ChatIconName = keyof typeof PATHS;
28
+ /** Every name ChatIcon draws. */
29
+ export declare const CHAT_ICONS: ChatIconName[];
30
+ export declare function ChatIcon({ name }: {
31
+ name: ChatIconName;
32
+ }): import("react").JSX.Element;
33
+ export {};
package/dist/icons.js ADDED
@@ -0,0 +1,34 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /** Small outline icons for the chat's controls, drawn in the text colour at the text's size. */
3
+ const PATHS = {
4
+ plus: "M12 5v14 M5 12h14",
5
+ send: "M12 19V5 M6 11l6-6 6 6",
6
+ stop: "M7 7h10v10H7z",
7
+ mic: "M12 4a3 3 0 0 0-3 3v5a3 3 0 0 0 6 0V7a3 3 0 0 0-3-3z M6 11a6 6 0 0 0 12 0 M12 17v3",
8
+ talk: "M4 12h2 M8 8v8 M12 5v14 M16 8v8 M20 12h-2",
9
+ chevron: "M7 10l5 5 5-5",
10
+ next: "M10 7l5 5-5 5",
11
+ back: "M14 7l-5 5 5 5",
12
+ data: "M5 6c0-1.7 3.1-3 7-3s7 1.3 7 3-3.1 3-7 3-7-1.3-7-3z M5 6v12c0 1.7 3.1 3 7 3s7-1.3 7-3V6 M5 12c0 1.7 3.1 3 7 3s7-1.3 7-3",
13
+ file: "M6 3h8l4 4v14H6z M14 3v4h4",
14
+ skill: "M5 5h9a3 3 0 0 1 3 3v12H8a3 3 0 0 1-3-3z M5 17a3 3 0 0 1 3-3h9",
15
+ plug: "M9 3v5 M15 3v5 M7 8h10v3a5 5 0 0 1-10 0z M12 16v5",
16
+ agent: "M4 7h9M17 7h3M4 17h3M11 17h9M15 5v4M9 15v4",
17
+ model: "M12 3l8 4.5v9L12 21l-8-4.5v-9z M12 12l8-4.5 M12 12v9 M12 12L4 7.5",
18
+ sessions: "M4 6h16 M4 12h16 M4 18h10",
19
+ chat: "M4 5h16v11H10l-5 4v-4H4z",
20
+ close: "M6 6l12 12 M18 6L6 18",
21
+ check: "M5 12l5 5 9-10",
22
+ pin: "M9 4h6l-1 6 3 3H7l3-3z M12 13v7",
23
+ search: "M11 5a6 6 0 1 0 0 12 6 6 0 0 0 0-12z M20 20l-4.5-4.5",
24
+ // Three dots across, for an overflow menu: the drawing ItemControls' "more" uses.
25
+ more: "M4.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M10.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M16.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0",
26
+ bell: "M18 16v-5a6 6 0 0 0-12 0v5l-2 2h16z M10 21a2 2 0 0 0 4 0",
27
+ about: "M12 4a8 8 0 1 0 0 16 8 8 0 0 0 0-16z M12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z",
28
+ };
29
+ /** Every name ChatIcon draws. */
30
+ export const CHAT_ICONS = Object.keys(PATHS);
31
+ export function ChatIcon({ name }) {
32
+ return (_jsx("svg", { className: "sf-chat-icon", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.7", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: PATHS[name] }) }));
33
+ }
34
+ //# sourceMappingURL=icons.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"icons.js","sourceRoot":"","sources":["../src/icons.tsx"],"names":[],"mappings":";AAAA,gGAAgG;AAChG,MAAM,KAAK,GAAG;IACZ,IAAI,EAAE,mBAAmB;IACzB,IAAI,EAAE,wBAAwB;IAC9B,IAAI,EAAE,eAAe;IACrB,GAAG,EAAE,oFAAoF;IACzF,IAAI,EAAE,2CAA2C;IACjD,OAAO,EAAE,eAAe;IACxB,IAAI,EAAE,eAAe;IACrB,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE,yHAAyH;IAC/H,IAAI,EAAE,4BAA4B;IAClC,KAAK,EAAE,gEAAgE;IACvE,IAAI,EAAE,mDAAmD;IACzD,KAAK,EAAE,4CAA4C;IACnD,KAAK,EAAE,mEAAmE;IAC1E,QAAQ,EAAE,2BAA2B;IACrC,IAAI,EAAE,0BAA0B;IAChC,KAAK,EAAE,uBAAuB;IAC9B,KAAK,EAAE,gBAAgB;IACvB,GAAG,EAAE,iCAAiC;IACtC,MAAM,EAAE,sDAAsD;IAC9D,kFAAkF;IAClF,IAAI,EAAE,mJAAmJ;IACzJ,IAAI,EAAE,0DAA0D;IAChE,KAAK,EAAE,yEAAyE;CACxE,CAAC;AAGX,iCAAiC;AACjC,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAmB,CAAC;AAE/D,MAAM,UAAU,QAAQ,CAAC,EAAE,IAAI,EAA0B;IACvD,OAAO,CACL,cAAK,SAAS,EAAC,cAAc,EAAC,OAAO,EAAC,WAAW,EAAC,IAAI,EAAC,MAAM,EAAC,MAAM,EAAC,cAAc,EAAC,WAAW,EAAC,KAAK,EAAC,aAAa,EAAC,OAAO,EAAC,cAAc,EAAC,OAAO,iBAAa,MAAM,EAAC,SAAS,EAAC,OAAO,YACrL,eAAM,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,GAAI,GACpB,CACP,CAAC;AACJ,CAAC","sourcesContent":["/** Small outline icons for the chat's controls, drawn in the text colour at the text's size. */\nconst PATHS = {\n plus: \"M12 5v14 M5 12h14\",\n send: \"M12 19V5 M6 11l6-6 6 6\",\n stop: \"M7 7h10v10H7z\",\n mic: \"M12 4a3 3 0 0 0-3 3v5a3 3 0 0 0 6 0V7a3 3 0 0 0-3-3z M6 11a6 6 0 0 0 12 0 M12 17v3\",\n talk: \"M4 12h2 M8 8v8 M12 5v14 M16 8v8 M20 12h-2\",\n chevron: \"M7 10l5 5 5-5\",\n next: \"M10 7l5 5-5 5\",\n back: \"M14 7l-5 5 5 5\",\n data: \"M5 6c0-1.7 3.1-3 7-3s7 1.3 7 3-3.1 3-7 3-7-1.3-7-3z M5 6v12c0 1.7 3.1 3 7 3s7-1.3 7-3V6 M5 12c0 1.7 3.1 3 7 3s7-1.3 7-3\",\n file: \"M6 3h8l4 4v14H6z M14 3v4h4\",\n skill: \"M5 5h9a3 3 0 0 1 3 3v12H8a3 3 0 0 1-3-3z M5 17a3 3 0 0 1 3-3h9\",\n plug: \"M9 3v5 M15 3v5 M7 8h10v3a5 5 0 0 1-10 0z M12 16v5\",\n agent: \"M4 7h9M17 7h3M4 17h3M11 17h9M15 5v4M9 15v4\",\n model: \"M12 3l8 4.5v9L12 21l-8-4.5v-9z M12 12l8-4.5 M12 12v9 M12 12L4 7.5\",\n sessions: \"M4 6h16 M4 12h16 M4 18h10\",\n chat: \"M4 5h16v11H10l-5 4v-4H4z\",\n close: \"M6 6l12 12 M18 6L6 18\",\n check: \"M5 12l5 5 9-10\",\n pin: \"M9 4h6l-1 6 3 3H7l3-3z M12 13v7\",\n search: \"M11 5a6 6 0 1 0 0 12 6 6 0 0 0 0-12z M20 20l-4.5-4.5\",\n // Three dots across, for an overflow menu: the drawing ItemControls' \"more\" uses.\n more: \"M4.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M10.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0 M16.8 12a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0-2.4 0\",\n bell: \"M18 16v-5a6 6 0 0 0-12 0v5l-2 2h16z M10 21a2 2 0 0 0 4 0\",\n about: \"M12 4a8 8 0 1 0 0 16 8 8 0 0 0 0-16z M12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z\",\n} as const;\n\nexport type ChatIconName = keyof typeof PATHS;\n/** Every name ChatIcon draws. */\nexport const CHAT_ICONS = Object.keys(PATHS) as ChatIconName[];\n\nexport function ChatIcon({ name }: { name: ChatIconName }) {\n return (\n <svg className=\"sf-chat-icon\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" focusable=\"false\">\n <path d={PATHS[name]} />\n </svg>\n );\n}\n"]}
@@ -0,0 +1,18 @@
1
+ export * from "./thread.js";
2
+ export * from "./useChat.js";
3
+ export * from "./Chat.js";
4
+ export * from "./PageStudio.js";
5
+ export * from "./Approval.js";
6
+ export * from "./conversation.js";
7
+ export { ChatComposer, Composer, type ComposerProps, type AttachResult, type SpeechConsent, type ComposerContext, type ComposerToggle } from "./ChatComposer.js";
8
+ export * from "./DataPanel.js";
9
+ export * from "./Menu.js";
10
+ export * from "./icons.js";
11
+ export * from "./selectors.js";
12
+ export * from "./speech.js";
13
+ export * from "./voice.js";
14
+ export * from "./sessions.js";
15
+ export * from "./SessionList.js";
16
+ export * from "./ChatPanels.js";
17
+ export * from "./AnalyticsChat.js";
18
+ export * from "./appComponents.js";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ export * from "./thread.js";
2
+ export * from "./useChat.js";
3
+ export * from "./Chat.js";
4
+ export * from "./PageStudio.js";
5
+ export * from "./Approval.js";
6
+ export * from "./conversation.js";
7
+ export { ChatComposer, Composer } from "./ChatComposer.js";
8
+ export * from "./DataPanel.js";
9
+ export * from "./Menu.js";
10
+ export * from "./icons.js";
11
+ export * from "./selectors.js";
12
+ export * from "./speech.js";
13
+ export * from "./voice.js";
14
+ export * from "./sessions.js";
15
+ export * from "./SessionList.js";
16
+ export * from "./ChatPanels.js";
17
+ export * from "./AnalyticsChat.js";
18
+ export * from "./appComponents.js";
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAwG,MAAM,mBAAmB,CAAC;AACjK,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC","sourcesContent":["export * from \"./thread.js\";\nexport * from \"./useChat.js\";\nexport * from \"./Chat.js\";\nexport * from \"./PageStudio.js\";\nexport * from \"./Approval.js\";\nexport * from \"./conversation.js\";\nexport { ChatComposer, Composer, type ComposerProps, type AttachResult, type SpeechConsent, type ComposerContext, type ComposerToggle } from \"./ChatComposer.js\";\nexport * from \"./DataPanel.js\";\nexport * from \"./Menu.js\";\nexport * from \"./icons.js\";\nexport * from \"./selectors.js\";\nexport * from \"./speech.js\";\nexport * from \"./voice.js\";\nexport * from \"./sessions.js\";\nexport * from \"./SessionList.js\";\nexport * from \"./ChatPanels.js\";\nexport * from \"./AnalyticsChat.js\";\nexport * from \"./appComponents.js\";\n"]}
@@ -0,0 +1,106 @@
1
+ import { type AgentInfo, type ToolConnector } from "@surfaces-ui/proto";
2
+ import type { ChatSettings } from "./useChat.js";
3
+ /** A panel a menu's "View all…" or "Manage…" opens. */
4
+ export type ChatPanel = "sessions" | "data" | "setup";
5
+ /**
6
+ * Where models run (a provider: a hosted API, a server, a model on this
7
+ * machine), with the models each offers. A host that can reach several gives
8
+ * them to the Model menu; picking a model there sets `provider` and `model`,
9
+ * and the host answers with its own backend. A provider never carries a key.
10
+ */
11
+ export interface ChatProvider {
12
+ id: string;
13
+ label: string;
14
+ /** One line on where it runs. */
15
+ detail?: string;
16
+ models: {
17
+ id: string;
18
+ label?: string;
19
+ detail?: string;
20
+ level?: ModelLevel;
21
+ }[];
22
+ }
23
+ /**
24
+ * How capable a model is, on the host's own scale: drawn in the Model menu as
25
+ * `of` bars rising left to right, `value` of them filled, beside `label`
26
+ * ("Everyday"). The word is what is read; the bars are only drawn.
27
+ */
28
+ export interface ModelLevel {
29
+ value: number;
30
+ of: number;
31
+ label: string;
32
+ }
33
+ /** A model's level as rising bars and its word, on the line with its name. */
34
+ export declare function ModelLevelBars({ level }: {
35
+ level: ModelLevel;
36
+ }): import("react").JSX.Element;
37
+ export interface SelectorProps {
38
+ agents: AgentInfo[];
39
+ settings: ChatSettings;
40
+ onSettingsChange(settings: ChatSettings): void;
41
+ /** Opens the panel with the whole list. Without it, a menu lists everything. */
42
+ onManage?(panel: ChatPanel): void;
43
+ /** Items a menu shows before "View all…". Default 5. */
44
+ limit?: number;
45
+ /** Icons only, for a narrow composer; each keeps its full name as its label and tooltip. */
46
+ compact?: boolean;
47
+ }
48
+ /** The agent the settings name, or the default one. */
49
+ export declare const currentAgent: (agents: AgentInfo[], settings: ChatSettings) => AgentInfo | undefined;
50
+ /** The skill and connector ids on now: what the settings say, or each one's default. */
51
+ export declare function onIds(agent: AgentInfo | undefined, settings: ChatSettings): {
52
+ skills: string[];
53
+ connectors: string[];
54
+ };
55
+ /** Turns one skill or connector on or off, writing both lists so the choice is explicit from then on. */
56
+ export declare function toggle(agent: AgentInfo | undefined, settings: ChatSettings, kind: "skills" | "connectors", id: string): ChatSettings;
57
+ export interface ModelMenuProps extends SelectorProps {
58
+ /** Several providers: the menu groups their models under each one's name. */
59
+ providers?: ChatProvider[];
60
+ /** "Manage providers…": the host's own page for them. */
61
+ onManageProviders?(): void;
62
+ }
63
+ /** The provider and model the settings name, or the first offered. */
64
+ export declare function currentProvider(providers: ChatProvider[], settings: ChatSettings): {
65
+ provider: ChatProvider | undefined;
66
+ model: {
67
+ id: string;
68
+ label?: string;
69
+ detail?: string;
70
+ level?: ModelLevel;
71
+ } | undefined;
72
+ };
73
+ /**
74
+ * Which model answers. With `providers`, every model of every provider in
75
+ * groups, so the person sees where it runs; otherwise the agent's models.
76
+ */
77
+ export declare function ModelMenu(props: ModelMenuProps): import("react").JSX.Element | null;
78
+ export interface SetupMenuProps extends SelectorProps {
79
+ /** Data connections a person may browse and attach from. */
80
+ sources?: {
81
+ id: string;
82
+ label: string;
83
+ }[];
84
+ /** "Add a connection…": the host opens its own form (`ConnectionBuilder`). */
85
+ onAddConnection?(): void;
86
+ /** A tool connector that needs a person to sign in. The host opens `signInUrl` its own way. */
87
+ onConnect?(connector: ToolConnector): void;
88
+ }
89
+ type Section = "agent" | "skills" | "connectors";
90
+ /** Which sections the setup menu has, and what each says it stands at. */
91
+ export declare function setupSummary(props: Pick<SetupMenuProps, "agents" | "settings" | "sources" | "onAddConnection">): {
92
+ sections: {
93
+ id: Section;
94
+ title: string;
95
+ detail: string;
96
+ }[];
97
+ text: string;
98
+ };
99
+ /**
100
+ * The agent, its skills and its connectors in one menu. Each section opens
101
+ * in place, with a way back, so it works the same in a narrow frame and by
102
+ * touch: Enter or → goes in, ← or Escape comes back. Each ends in "View
103
+ * all…" or "Manage…", which opens the setup panel.
104
+ */
105
+ export declare function SetupMenu(props: SetupMenuProps): import("react").JSX.Element | null;
106
+ export {};
@@ -0,0 +1,160 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { safeUrl } from "@surfaces-ui/spec";
3
+ import { ConnectorStatus } from "@surfaces-ui/proto";
4
+ import { useEffect, useRef, useState } from "react";
5
+ import { MenuButton, MenuGroup, MenuItem } from "./Menu.js";
6
+ import { ChatIcon } from "./icons.js";
7
+ /** A model's level as rising bars and its word, on the line with its name. */
8
+ export function ModelLevelBars({ level }) {
9
+ const of = Math.max(1, Math.min(5, Math.round(level.of)));
10
+ const value = Math.max(0, Math.min(of, Math.round(level.value)));
11
+ return (_jsxs("span", { className: "sf-chat-model-level", children: [_jsx("span", { className: "sf-chat-model-bars", "aria-hidden": "true", children: Array.from({ length: of }, (_, i) => (_jsx("span", { "data-on": i < value || undefined, style: { blockSize: `${Math.round(((i + 1) / of) * 100)}%` } }, i))) }), level.label] }));
12
+ }
13
+ /** The agent the settings name, or the default one. */
14
+ export const currentAgent = (agents, settings) => agents.find((a) => a.id === settings.agentId) ?? agents.find((a) => a.isDefault) ?? agents[0];
15
+ /** The skill and connector ids on now: what the settings say, or each one's default. */
16
+ export function onIds(agent, settings) {
17
+ return {
18
+ skills: settings.skillIds ?? (agent?.skills ?? []).filter((s) => s.defaultOn).map((s) => s.id),
19
+ connectors: settings.connectorIds ?? (agent?.connectors ?? []).filter((c) => c.defaultOn && c.status === ConnectorStatus.CONNECTED).map((c) => c.id),
20
+ };
21
+ }
22
+ /** Turns one skill or connector on or off, writing both lists so the choice is explicit from then on. */
23
+ export function toggle(agent, settings, kind, id) {
24
+ const now = onIds(agent, settings);
25
+ const list = now[kind].includes(id) ? now[kind].filter((x) => x !== id) : [...now[kind], id];
26
+ return { ...settings, skillIds: kind === "skills" ? list : now.skills, connectorIds: kind === "connectors" ? list : now.connectors };
27
+ }
28
+ const STATUS_WORD = { [ConnectorStatus.NEEDS_SIGN_IN]: "Needs sign-in", [ConnectorStatus.UNAVAILABLE]: "Unavailable" };
29
+ /** The provider and model the settings name, or the first offered. */
30
+ export function currentProvider(providers, settings) {
31
+ const provider = providers.find((p) => p.id === settings.provider) ?? providers[0];
32
+ const model = provider?.models.find((m) => m.id === settings.model) ?? provider?.models[0];
33
+ return { provider, model };
34
+ }
35
+ /**
36
+ * Which model answers. With `providers`, every model of every provider in
37
+ * groups, so the person sees where it runs; otherwise the agent's models.
38
+ */
39
+ export function ModelMenu(props) {
40
+ const { agents, settings, onSettingsChange, compact, providers, onManage, limit = 5 } = props;
41
+ if (providers && providers.length > 0) {
42
+ const { provider, model } = currentProvider(providers, settings);
43
+ // A few models from each provider, the chosen one always among them; the rest are in the setup panel.
44
+ const few = (p) => (onManage ? p.models.filter((m, i) => i < limit || (p.id === provider?.id && m.id === model?.id)) : p.models);
45
+ const total = providers.reduce((n, p) => n + p.models.length, 0);
46
+ const hidden = providers.some((p) => few(p).length < p.models.length);
47
+ const text = model?.label || model?.id || provider?.label || "Model";
48
+ return (_jsx(MenuButton, { label: `Model: ${text}${provider && provider.label !== text ? `, ${provider.label}` : ""}`, text: text, icon: "model", iconOnly: compact, children: (close) => (_jsxs(_Fragment, { children: [providers.map((p) => (_jsx(MenuGroup, { label: p.label, children: few(p).map((m) => (_jsx(MenuItem, { role: "radio", checked: p.id === provider?.id && m.id === model?.id, detail: m.detail, onSelect: () => {
49
+ onSettingsChange({ ...settings, provider: p.id, model: m.id });
50
+ close();
51
+ }, children: m.level ? (_jsxs("span", { className: "sf-chat-model-name", children: [_jsx("span", { children: m.label || m.id }), _jsx(ModelLevelBars, { level: m.level })] })) : (m.label || m.id) }, m.id))) }, p.id))), onManage && hidden && (_jsxs("button", { type: "button", className: "sf-chat-menu-more", onClick: () => {
52
+ close();
53
+ onManage("setup");
54
+ }, children: ["View all ", total, " models\u2026"] })), props.onManageProviders && (_jsx("button", { type: "button", className: "sf-chat-menu-more", onClick: () => {
55
+ close();
56
+ props.onManageProviders?.();
57
+ }, children: "Manage providers\u2026" }))] })) }));
58
+ }
59
+ const agent = currentAgent(agents, settings);
60
+ const models = agent?.models ?? [];
61
+ if (models.length === 0)
62
+ return null;
63
+ const chosen = settings.model || agent?.defaultModel || "";
64
+ const name = (id) => models.find((m) => m.id === id)?.label || id || "Default";
65
+ return (_jsx(MenuButton, { label: `Model: ${name(chosen)}`, text: name(chosen), icon: "model", iconOnly: compact, children: (close) => (_jsx(MenuGroup, { label: "Model", children: models.map((m) => (_jsx(MenuItem, { role: "radio", checked: m.id === chosen, detail: [m.id === agent?.defaultModel ? "Default" : "", m.description].filter(Boolean).join(" · "), onSelect: () => {
66
+ onSettingsChange({ ...settings, model: m.id === agent?.defaultModel ? "" : m.id });
67
+ close();
68
+ }, children: m.label || m.id }, m.id))) })) }));
69
+ }
70
+ function more(count, hidden, label, onManage, close) {
71
+ if (!onManage)
72
+ return null;
73
+ return (_jsx("button", { type: "button", className: "sf-chat-menu-more", onClick: () => {
74
+ close();
75
+ onManage("setup");
76
+ }, children: hidden ? `View all ${count} ${label}…` : `Manage ${label}…` }));
77
+ }
78
+ /** Which sections the setup menu has, and what each says it stands at. */
79
+ export function setupSummary(props) {
80
+ const { agents, settings, sources = [] } = props;
81
+ const agent = currentAgent(agents, settings);
82
+ const on = onIds(agent, settings);
83
+ const skills = agent?.skills ?? [];
84
+ const tools = agent?.connectors ?? [];
85
+ const source = sources.find((s) => s.id === settings.sourceId);
86
+ const sections = [];
87
+ if (agents.length > 1)
88
+ sections.push({ id: "agent", title: "Agent", detail: agent?.name ?? "" });
89
+ if (skills.length > 0)
90
+ sections.push({ id: "skills", title: "Skills", detail: `${on.skills.length} of ${skills.length} on` });
91
+ if (tools.length > 0 || sources.length > 0 || props.onAddConnection)
92
+ sections.push({ id: "connectors", title: "Connectors", detail: [tools.length > 0 ? `${on.connectors.length} of ${tools.length} tools on` : "", source ? `data: ${source.label}` : ""].filter(Boolean).join(" · ") });
93
+ const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
94
+ const text = [agents.length > 1 ? (agent?.name ?? "") : "", skills.length > 0 ? plural(on.skills.length, "skill") : "", tools.length > 0 ? plural(on.connectors.length, "tool") : ""].filter(Boolean).join(" · ") || "Connectors";
95
+ return { sections, text };
96
+ }
97
+ /**
98
+ * The agent, its skills and its connectors in one menu. Each section opens
99
+ * in place, with a way back, so it works the same in a narrow frame and by
100
+ * touch: Enter or → goes in, ← or Escape comes back. Each ends in "View
101
+ * all…" or "Manage…", which opens the setup panel.
102
+ */
103
+ export function SetupMenu(props) {
104
+ const { sections, text } = setupSummary(props);
105
+ if (sections.length === 0)
106
+ return null;
107
+ return (_jsx(MenuButton, { label: `Agent setup: ${text}`, text: text, icon: "agent", iconOnly: props.compact, className: "sf-chat-setup-menu", children: (close) => _jsx(SetupContent, { ...props, sections: sections, close: close }) }));
108
+ }
109
+ function SetupContent(props) {
110
+ const { agents, settings, onSettingsChange, onManage, limit = 5, sources = [], onAddConnection, onConnect, sections, close } = props;
111
+ // With one section there is nothing to choose between: open on it.
112
+ const only = sections.length === 1 ? sections[0].id : undefined;
113
+ const [view, setView] = useState(only ?? "root");
114
+ const box = useRef(null);
115
+ const from = useRef(undefined);
116
+ useEffect(() => {
117
+ // Focus the first choice in a section, or the row just left when coming back.
118
+ const target = view === "root" && from.current ? box.current?.querySelector(`[data-section="${from.current}"]`) : box.current?.querySelector("button");
119
+ target?.focus();
120
+ }, [view]);
121
+ const go = (next) => {
122
+ if (next === "root" && view !== "root")
123
+ from.current = view;
124
+ setView(next);
125
+ };
126
+ const onKeyDown = (event) => {
127
+ if (view === "root" || only)
128
+ return;
129
+ if (event.key === "Escape" || event.key === "ArrowLeft") {
130
+ event.stopPropagation();
131
+ event.preventDefault();
132
+ go("root");
133
+ }
134
+ };
135
+ const agent = currentAgent(agents, settings);
136
+ const on = onIds(agent, settings);
137
+ const title = sections.find((s) => s.id === view)?.title ?? "";
138
+ return (_jsx("div", { ref: box, className: "sf-chat-setup", onKeyDown: onKeyDown, children: _jsx("div", { className: "sf-chat-setup-view", "data-to": view === "root" ? "back" : "in", children: view === "root" ? (_jsx(MenuGroup, { label: "Agent setup", children: sections.map((s) => (_jsxs("button", { type: "button", className: "sf-chat-menu-item sf-chat-menu-section", "data-section": s.id, "aria-label": `${s.title}: ${s.detail}`, onClick: () => go(s.id), onKeyDown: (e) => {
139
+ if (e.key === "ArrowRight") {
140
+ e.preventDefault();
141
+ go(s.id);
142
+ }
143
+ }, children: [_jsx("span", { className: "sf-chat-menu-mark" }), _jsxs("span", { className: "sf-chat-menu-text", children: [s.title, _jsx("span", { className: "sf-chat-menu-detail", children: s.detail })] }), _jsx(ChatIcon, { name: "next" })] }, s.id))) })) : (_jsxs(_Fragment, { children: [!only && (_jsxs("button", { type: "button", className: "sf-chat-menu-back", onClick: () => go("root"), children: [_jsx(ChatIcon, { name: "back" }), _jsx("span", { children: "Agent setup" })] })), view === "agent" && (_jsx(MenuGroup, { label: title, children: agents.map((a) => (_jsx(MenuItem, { role: "radio", checked: a.id === agent?.id, detail: a.description, onSelect: () => {
144
+ // Another agent offers other models, skills and connectors: start from its defaults.
145
+ onSettingsChange({ agentId: a.id, ...(settings.sourceId ? { sourceId: settings.sourceId } : {}) });
146
+ go("root");
147
+ }, children: a.name }, a.id))) })), view === "skills" && (_jsxs(_Fragment, { children: [_jsx(MenuGroup, { label: title, children: (onManage ? (agent?.skills ?? []).slice(0, limit) : (agent?.skills ?? [])).map((s) => (_jsx(MenuItem, { role: "checkbox", checked: on.skills.includes(s.id), detail: s.description, onSelect: () => onSettingsChange(toggle(agent, settings, "skills", s.id)), children: s.name }, s.id))) }), more(agent?.skills.length ?? 0, (agent?.skills.length ?? 0) > limit, "skills", onManage, close)] })), view === "connectors" && (_jsx(ConnectorChoices, { agent: agent, settings: settings, onSettingsChange: onSettingsChange, sources: sources, onAddConnection: onAddConnection, onConnect: onConnect, limit: onManage ? limit : Number.POSITIVE_INFINITY, close: close })), view === "connectors" && more((agent?.connectors.length ?? 0) + sources.length, (agent?.connectors.length ?? 0) > limit || sources.length > limit, "connectors", onManage, close)] })) }, view) }));
148
+ }
149
+ function ConnectorChoices({ agent, settings, onSettingsChange, sources, onAddConnection, onConnect, limit, close }) {
150
+ const tools = agent?.connectors ?? [];
151
+ const on = onIds(agent, settings).connectors;
152
+ return (_jsxs(_Fragment, { children: [tools.length > 0 && (_jsx(MenuGroup, { label: "Tools", children: tools.slice(0, limit).map((c) => c.status === ConnectorStatus.CONNECTED ? (_jsx(MenuItem, { role: "checkbox", checked: on.includes(c.id), detail: [c.description, c.requiresApproval ? "Asks before it acts" : ""].filter(Boolean).join(" · "), onSelect: () => onSettingsChange(toggle(agent, settings, "connectors", c.id)), children: c.name }, c.id)) : (_jsx(MenuItem, { detail: [STATUS_WORD[c.status], c.reason].filter(Boolean).join(": "), disabled: c.status !== ConnectorStatus.NEEDS_SIGN_IN || !onConnect || !safeUrl(c.signInUrl), onSelect: () => {
153
+ close();
154
+ onConnect?.(c);
155
+ }, children: c.status === ConnectorStatus.NEEDS_SIGN_IN ? `Sign in to ${c.name}` : c.name }, c.id))) })), (sources.length > 0 || onAddConnection) && (_jsxs(MenuGroup, { label: "Data", children: [sources.slice(0, limit).map((s) => (_jsx(MenuItem, { role: "radio", checked: s.id === settings.sourceId, onSelect: () => onSettingsChange({ ...settings, sourceId: s.id }), children: s.label }, s.id))), onAddConnection && (_jsx(MenuItem, { onSelect: () => {
156
+ close();
157
+ onAddConnection();
158
+ }, children: "Add a connection\u2026" }))] }))] }));
159
+ }
160
+ //# sourceMappingURL=selectors.js.map