@cadraos/sdk 0.3.15 → 0.3.16-dev.1

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.
@@ -1830,13 +1830,69 @@ function executionCancelPath(executionId) {
1830
1830
  return `${EXECUTION_PATH}/${executionId}/cancel`;
1831
1831
  }
1832
1832
 
1833
+ // src/utils/message-key.ts
1834
+ var MESSAGE_KEY_PATTERN = /^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$/;
1835
+ function isValidMessageKey(key) {
1836
+ return MESSAGE_KEY_PATTERN.test(key);
1837
+ }
1838
+
1839
+ // src/resources/agent-messages.ts
1840
+ var AgentMessagesAPI = class extends BaseResourceAPI {
1841
+ constructor(http) {
1842
+ super(http, "/api/v1/agents");
1843
+ }
1844
+ /**
1845
+ * List an agent's saved messages and the known keys
1846
+ *
1847
+ * @param agentUuid - Agent UUID (a team UUID resolves to its lead agent)
1848
+ *
1849
+ * @example
1850
+ * ```typescript
1851
+ * const { messages, knownKeys, languages, defaultLanguage } = await client.agents.messages.list('agent-uuid');
1852
+ * ```
1853
+ */
1854
+ async list(agentUuid) {
1855
+ return this.httpGet(`/${agentUuid}/messages`);
1856
+ }
1857
+ /**
1858
+ * Render one message with placeholders filled
1859
+ *
1860
+ * Resolves with `text: null` when the agent has no message for the key.
1861
+ * Throws `ValidationError` before any request when the key is not a
1862
+ * lowercase dotted key (see `MESSAGE_KEY_PATTERN`).
1863
+ *
1864
+ * @example
1865
+ * ```typescript
1866
+ * const { text } = await client.agents.messages.render('agent-uuid', 'handoff.requested', {
1867
+ * lang: 'es',
1868
+ * vars: { agentName: 'Ana' },
1869
+ * });
1870
+ * ```
1871
+ */
1872
+ async render(agentUuid, key, options = {}) {
1873
+ if (!isValidMessageKey(key)) {
1874
+ throw new ValidationError(`Invalid message key "${key}"`, [
1875
+ { field: "key", message: "Must be a lowercase dotted key, e.g. chat.welcome" }
1876
+ ]);
1877
+ }
1878
+ return this.httpPost(`/${agentUuid}/messages/render`, {
1879
+ key,
1880
+ ...options.lang ? { lang: options.lang } : {},
1881
+ ...options.vars ? { vars: options.vars } : {}
1882
+ });
1883
+ }
1884
+ };
1885
+
1833
1886
  // src/resources/agents.ts
1834
1887
  var AgentsAPI = class extends BaseResourceAPI {
1835
1888
  constructor(http, getStreamConfig) {
1836
1889
  super(http, "/api/v1/agents");
1837
1890
  this.getStreamConfig = getStreamConfig;
1891
+ this.messages = new AgentMessagesAPI(http);
1838
1892
  }
1839
1893
  getStreamConfig;
1894
+ /** Saved agent messages (p13): `list()` and `render()` */
1895
+ messages;
1840
1896
  // ===== LIST Operations =====
1841
1897
  /**
1842
1898
  * List all agents in the organization
@@ -9610,6 +9666,147 @@ function useImageStream(options) {
9610
9666
  }, [client, providedExecutionId, agentUuid, inputKey, sessionId, shouldConnect]);
9611
9667
  return { images, status, error, executionId, output, cancel };
9612
9668
  }
9669
+ var DEFAULT_LABELS = {
9670
+ search: "Search messages",
9671
+ loading: "Loading messages",
9672
+ loadError: "Could not load messages",
9673
+ empty: "No matching messages",
9674
+ notSet: "not set",
9675
+ useCustom: (key) => `Use "${key}"`,
9676
+ invalidCustom: "Keys are lowercase and dotted, like chat.welcome",
9677
+ language: "Preview language"
9678
+ };
9679
+ function previewOf(message, lang, defaultLanguage = "en") {
9680
+ const base = lang.split("-")[0] ?? lang;
9681
+ for (const candidate of [lang, base, defaultLanguage, "en"]) {
9682
+ const text = message.text[candidate];
9683
+ if (text) return text;
9684
+ }
9685
+ return Object.values(message.text).find((t) => Boolean(t)) ?? null;
9686
+ }
9687
+ function languagesOf(list) {
9688
+ if (list.languages && list.languages.length > 0) return list.languages;
9689
+ const seen = /* @__PURE__ */ new Set([list.defaultLanguage ?? "en"]);
9690
+ for (const m of list.messages) {
9691
+ for (const [code, text] of Object.entries(m.text)) if (text) seen.add(code);
9692
+ }
9693
+ return [...seen];
9694
+ }
9695
+ function AgentMessagePicker({
9696
+ client,
9697
+ agentUuid,
9698
+ value,
9699
+ onChange,
9700
+ allowCustom = false,
9701
+ lang,
9702
+ onLangChange,
9703
+ labels: labelOverrides,
9704
+ className = ""
9705
+ }) {
9706
+ const labels = { ...DEFAULT_LABELS, ...labelOverrides };
9707
+ const [data, setData] = react.useState(null);
9708
+ const [error, setError] = react.useState(false);
9709
+ const [query, setQuery] = react.useState("");
9710
+ const [pickedLang, setPickedLang] = react.useState(null);
9711
+ react.useEffect(() => {
9712
+ setPickedLang(null);
9713
+ }, [lang, agentUuid]);
9714
+ const defaultLanguage = data?.defaultLanguage ?? "en";
9715
+ const languages = react.useMemo(() => data ? languagesOf(data) : [], [data]);
9716
+ const activeLang = pickedLang ?? lang ?? defaultLanguage;
9717
+ react.useEffect(() => {
9718
+ let cancelled = false;
9719
+ setData(null);
9720
+ setError(false);
9721
+ client.agents.messages.list(agentUuid).then((result) => {
9722
+ if (!cancelled) setData(result);
9723
+ }).catch(() => {
9724
+ if (!cancelled) setError(true);
9725
+ });
9726
+ return () => {
9727
+ cancelled = true;
9728
+ };
9729
+ }, [client, agentUuid]);
9730
+ const items = react.useMemo(() => {
9731
+ if (!data) return [];
9732
+ const saved = data.messages.map((m) => ({
9733
+ key: m.key,
9734
+ preview: previewOf(m, activeLang, defaultLanguage)
9735
+ }));
9736
+ const savedKeys = new Set(saved.map((i) => i.key));
9737
+ const unsaved = data.knownKeys.filter((k) => !savedKeys.has(k)).sort().map((key) => ({ key, preview: null }));
9738
+ return [...saved, ...unsaved];
9739
+ }, [data, activeLang, defaultLanguage]);
9740
+ const needle = query.trim().toLowerCase();
9741
+ const visible = needle ? items.filter(
9742
+ (i) => i.key.includes(needle) || (i.preview?.toLowerCase().includes(needle) ?? false)
9743
+ ) : items;
9744
+ const custom = query.trim();
9745
+ const showCustom = allowCustom && custom.length > 0 && !items.some((i) => i.key === custom);
9746
+ const customValid = showCustom && isValidMessageKey(custom);
9747
+ const languageChoices = languages.includes(activeLang) ? languages : [...languages, activeLang];
9748
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `flex flex-col gap-2 text-sm ${className}`, children: [
9749
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
9750
+ /* @__PURE__ */ jsxRuntime.jsx(
9751
+ "input",
9752
+ {
9753
+ type: "search",
9754
+ value: query,
9755
+ onChange: (e) => setQuery(e.target.value),
9756
+ placeholder: labels.search,
9757
+ "aria-label": labels.search,
9758
+ className: "w-full min-w-0 flex-1 rounded-lg bg-gray-100 dark:bg-gray-800 px-3 py-2 text-gray-900 dark:text-gray-100 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500"
9759
+ }
9760
+ ),
9761
+ data && languages.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(
9762
+ "select",
9763
+ {
9764
+ value: activeLang,
9765
+ onChange: (e) => {
9766
+ setPickedLang(e.target.value);
9767
+ onLangChange?.(e.target.value);
9768
+ },
9769
+ "aria-label": labels.language,
9770
+ className: "rounded-lg bg-gray-100 dark:bg-gray-800 px-2 py-2 font-mono text-xs text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500",
9771
+ children: languageChoices.map((code) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: code, children: code }, code))
9772
+ }
9773
+ )
9774
+ ] }),
9775
+ error ? /* @__PURE__ */ jsxRuntime.jsx("p", { role: "alert", className: "px-1 text-red-500", children: labels.loadError }) : !data ? /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "flex items-center gap-2 px-1 text-gray-500 dark:text-gray-400", children: [
9776
+ /* @__PURE__ */ jsxRuntime.jsx(LoadingSpinner, { className: "h-4 w-4" }),
9777
+ labels.loading
9778
+ ] }) : /* @__PURE__ */ jsxRuntime.jsxs("ul", { role: "listbox", "aria-label": labels.search, className: "flex max-h-72 flex-col gap-0.5 overflow-y-auto", children: [
9779
+ visible.map((item) => {
9780
+ const selected = item.key === value;
9781
+ return /* @__PURE__ */ jsxRuntime.jsx("li", { role: "option", "aria-selected": selected, children: /* @__PURE__ */ jsxRuntime.jsxs(
9782
+ "button",
9783
+ {
9784
+ type: "button",
9785
+ onClick: () => onChange(item.key),
9786
+ className: `flex w-full items-start gap-2 rounded-lg px-3 py-2 text-left transition-colors ${selected ? "bg-blue-50 dark:bg-blue-900/30" : "hover:bg-gray-100 dark:hover:bg-gray-800"}`,
9787
+ children: [
9788
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [
9789
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "font-mono text-xs text-gray-900 dark:text-gray-100", children: item.key }),
9790
+ item.preview !== null ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate text-gray-500 dark:text-gray-400", children: item.preview }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "italic text-gray-400 dark:text-gray-500", children: labels.notSet })
9791
+ ] }),
9792
+ selected && /* @__PURE__ */ jsxRuntime.jsx(CheckIcon, { className: "h-4 w-4 flex-shrink-0 text-blue-500" })
9793
+ ]
9794
+ }
9795
+ ) }, item.key);
9796
+ }),
9797
+ visible.length === 0 && !showCustom && /* @__PURE__ */ jsxRuntime.jsx("li", { className: "px-3 py-2 text-gray-500 dark:text-gray-400", children: labels.empty })
9798
+ ] }),
9799
+ data && showCustom && (customValid ? /* @__PURE__ */ jsxRuntime.jsx(
9800
+ "button",
9801
+ {
9802
+ type: "button",
9803
+ onClick: () => onChange(custom),
9804
+ className: "rounded-lg bg-gray-100 dark:bg-gray-800 px-3 py-2 text-left font-mono text-xs text-blue-600 dark:text-blue-400 hover:bg-gray-200 dark:hover:bg-gray-700",
9805
+ children: labels.useCustom(custom)
9806
+ }
9807
+ ) : /* @__PURE__ */ jsxRuntime.jsx("p", { className: "px-1 text-xs text-gray-500 dark:text-gray-400", children: labels.invalidCustom }))
9808
+ ] });
9809
+ }
9613
9810
  function cn13(...classes) {
9614
9811
  return classes.filter(Boolean).join(" ");
9615
9812
  }
@@ -9706,6 +9903,7 @@ function TodoList({
9706
9903
  }
9707
9904
 
9708
9905
  exports.AgentChat = AgentChat;
9906
+ exports.AgentMessagePicker = AgentMessagePicker;
9709
9907
  exports.AlertCircleIcon = AlertCircleIcon;
9710
9908
  exports.AskUserPrompt = AskUserPrompt;
9711
9909
  exports.BotIcon = BotIcon;
@@ -9738,6 +9936,7 @@ exports.ImageLightbox = ImageLightbox;
9738
9936
  exports.ImagesIcon = ImagesIcon;
9739
9937
  exports.ListTodoIcon = ListTodoIcon;
9740
9938
  exports.LoadingSpinner = LoadingSpinner;
9939
+ exports.MESSAGE_KEY_PATTERN = MESSAGE_KEY_PATTERN;
9741
9940
  exports.MarkdownContent = MarkdownContent;
9742
9941
  exports.Maximize2Icon = Maximize2Icon;
9743
9942
  exports.MessageCircleIcon = MessageCircleIcon;
@@ -9758,6 +9957,7 @@ exports.ZoomOutIcon = ZoomOutIcon;
9758
9957
  exports.compressImage = compressImage;
9759
9958
  exports.createDefaultAdapter = createDefaultAdapter;
9760
9959
  exports.isInternalTool = isInternalTool;
9960
+ exports.isValidMessageKey = isValidMessageKey;
9761
9961
  exports.useAgentChat = useAgentChat;
9762
9962
  exports.useChatConfig = useChatConfig;
9763
9963
  exports.useChatConfigOptional = useChatConfigOptional;