@cognizant-ai-lab/ui-common 1.11.0 → 1.12.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.
@@ -0,0 +1,31 @@
1
+ import { SlyData } from "./SlyData.js";
2
+ /**
3
+ * A top-level sly_data key advertised by the network, in a shape ready for display.
4
+ */
5
+ export interface SlyDataSchemaEntry {
6
+ /** What the key is for, verbatim from the schema */
7
+ readonly description?: string;
8
+ /** True if the key appears in the schema's root `required` list */
9
+ readonly isRequired: boolean;
10
+ /** The property name, verbatim. Any string is legal here -- some networks use URLs as keys. */
11
+ readonly key: string;
12
+ /** The declared type name (neuro-san writes "int"/"float"/"string"/"bool"), or "object" when only inferable */
13
+ readonly type?: string;
14
+ }
15
+ /**
16
+ * Lists the top-level sly_data keys a network advertises, for display to the user.
17
+ * @param schema The network's sly_data_schema, as returned by the function endpoint. May be anything.
18
+ * @param excludeKeys Keys to leave out: the ones the UI itself supplies at send time (API keys, login...), which
19
+ * the user should not be prompted for.
20
+ * @returns One entry per declared key in declaration order, or an empty array when the schema is unusable.
21
+ */
22
+ export declare const describeSlyDataSchema: (schema: unknown, excludeKeys?: readonly string[]) => readonly SlyDataSchemaEntry[];
23
+ /**
24
+ * Builds a sly_data skeleton from the network's schema, layered under the user's existing values.
25
+ * @param schema The network's sly_data_schema. May be anything; an unusable schema returns `existing` untouched.
26
+ * @param existing The current sly_data. Every existing key survives verbatim; the template only adds what is missing.
27
+ * @param excludeKeys Top-level keys never to seed: the ones the UI supplies at send time. Keeping these out matters
28
+ * for more than tidiness -- `llm_config` holds API keys, which must never be led into the persistent store.
29
+ * @returns A new object; the input is not mutated.
30
+ */
31
+ export declare const buildSlyDataTemplate: (schema: unknown, existing: SlyData, excludeKeys?: readonly string[]) => SlyData;
@@ -0,0 +1,149 @@
1
+ /*
2
+ Copyright 2025 Cognizant Technology Solutions Corp, www.cognizant.com.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ import * as z from "zod";
17
+ /**
18
+ * Helpers for reading an agent network's `sly_data_schema`.
19
+ *
20
+ * A network's front man may advertise the sly_data it expects via `sly_data_schema` (see the neuro-san agent hocon
21
+ * reference). The document is OpenAI-tool-style JSON Schema -- `type`/`properties`/`required`/`description` -- with
22
+ * one quirk: scalar types use neuro-san's names `"int"`, `"float"`, `"string"`, `"bool"` rather than the JSON Schema
23
+ * names. Everything here is defensive: the schema is server-supplied data, and a malformed one must degrade to
24
+ * "no schema" rather than break the sly_data editor.
25
+ */
26
+ // How many levels of nested objects a template will materialize. Deep enough for every published schema
27
+ // (mcp-style http_headers nest three levels), shallow enough that a pathological schema cannot recurse away.
28
+ const MAX_TEMPLATE_DEPTH = 5;
29
+ // Default template value for each recognized scalar type name. Neuro-san's names are canonical, but the
30
+ // JSON Schema spellings cost nothing to accept and schema authors do write them.
31
+ const SCALAR_TYPE_DEFAULTS = {
32
+ bool: false,
33
+ boolean: false,
34
+ double: 0,
35
+ float: 0,
36
+ int: 0,
37
+ integer: 0,
38
+ number: 0,
39
+ string: "",
40
+ };
41
+ /**
42
+ * One node of a sly_data_schema document. Loose on purpose: extra keys are the schema author's business.
43
+ * The recursion mirrors JSON Schema's -- an object node describes its keys under `properties`.
44
+ */
45
+ const SchemaNode = z.looseObject({
46
+ description: z.string().optional(),
47
+ get properties() {
48
+ return z.record(z.string(), SchemaNode).optional();
49
+ },
50
+ required: z.array(z.string()).optional(),
51
+ type: z.string().optional(),
52
+ });
53
+ /**
54
+ * Whether a schema node describes an object. An explicit `type: "object"` counts, and so does an untyped node
55
+ * that declares `properties` -- schema authors routinely omit the redundant `type`.
56
+ * @param node The schema node to examine.
57
+ * @returns True if the node describes an object value.
58
+ */
59
+ const isObjectNode = (node) => node.type === "object" || (node.type === undefined && node.properties !== undefined);
60
+ /**
61
+ * Validates a server-supplied sly_data_schema and returns its root node, or null if there is nothing usable.
62
+ * A root is usable when it is object-shaped and declares at least one property.
63
+ * @param schema The sly_data_schema as received from the server. May be anything.
64
+ * @returns The validated root node, or null when the schema is absent or unusable.
65
+ */
66
+ const parseSchemaRoot = (schema) => {
67
+ if (schema === undefined || schema === null) {
68
+ return null;
69
+ }
70
+ const parsed = SchemaNode.safeParse(schema);
71
+ if (!parsed.success || !isObjectNode(parsed.data) || Object.keys(parsed.data.properties ?? {}).length === 0) {
72
+ // A bad schema is the network author's bug, not the user's: degrade to the schema-less experience.
73
+ console.warn("Ignoring unusable sly_data_schema:", schema);
74
+ return null;
75
+ }
76
+ return parsed.data;
77
+ };
78
+ /**
79
+ * Lists the top-level sly_data keys a network advertises, for display to the user.
80
+ * @param schema The network's sly_data_schema, as returned by the function endpoint. May be anything.
81
+ * @param excludeKeys Keys to leave out: the ones the UI itself supplies at send time (API keys, login...), which
82
+ * the user should not be prompted for.
83
+ * @returns One entry per declared key in declaration order, or an empty array when the schema is unusable.
84
+ */
85
+ export const describeSlyDataSchema = (schema, excludeKeys = []) => {
86
+ const root = parseSchemaRoot(schema);
87
+ if (root === null) {
88
+ return [];
89
+ }
90
+ const required = new Set(root.required);
91
+ return Object.entries(root.properties)
92
+ .filter(([key]) => !excludeKeys.includes(key))
93
+ .map(([key, node]) => ({
94
+ description: node.description,
95
+ isRequired: required.has(key),
96
+ key,
97
+ type: node.type ?? (isObjectNode(node) ? "object" : undefined),
98
+ }));
99
+ };
100
+ /**
101
+ * Produces the template value for one schema node, folding in whatever the user already has.
102
+ * An existing value always wins -- even one that contradicts the schema -- because a template must never
103
+ * destroy user input. Objects are merged recursively so a partially-filled nested value gains its missing keys.
104
+ * @param node The schema node describing this value.
105
+ * @param existing The value currently at this position, or undefined if there is none.
106
+ * @param depth How many object levels deep this node is, for the recursion cap.
107
+ * @returns The existing value, or a default appropriate for the declared type when there is none.
108
+ */
109
+ const templateValue = (node, existing, depth) => {
110
+ if (isObjectNode(node)) {
111
+ if (existing !== undefined && (existing === null || typeof existing !== "object" || Array.isArray(existing))) {
112
+ return existing;
113
+ }
114
+ if (depth >= MAX_TEMPLATE_DEPTH) {
115
+ return existing ?? {};
116
+ }
117
+ const merged = { ...existing };
118
+ Object.entries(node.properties ?? {}).forEach(([key, child]) => {
119
+ merged[key] = templateValue(child, merged[key], depth + 1);
120
+ });
121
+ return merged;
122
+ }
123
+ if (existing !== undefined) {
124
+ return existing;
125
+ }
126
+ // A type we do not understand maps to null: "fill me in" without guessing a shape
127
+ return SCALAR_TYPE_DEFAULTS[node.type] ?? null;
128
+ };
129
+ /**
130
+ * Builds a sly_data skeleton from the network's schema, layered under the user's existing values.
131
+ * @param schema The network's sly_data_schema. May be anything; an unusable schema returns `existing` untouched.
132
+ * @param existing The current sly_data. Every existing key survives verbatim; the template only adds what is missing.
133
+ * @param excludeKeys Top-level keys never to seed: the ones the UI supplies at send time. Keeping these out matters
134
+ * for more than tidiness -- `llm_config` holds API keys, which must never be led into the persistent store.
135
+ * @returns A new object; the input is not mutated.
136
+ */
137
+ export const buildSlyDataTemplate = (schema, existing, excludeKeys = []) => {
138
+ const root = parseSchemaRoot(schema);
139
+ if (root === null) {
140
+ return existing;
141
+ }
142
+ const merged = { ...existing };
143
+ Object.entries(root.properties)
144
+ .filter(([key]) => !excludeKeys.includes(key))
145
+ .forEach(([key, node]) => {
146
+ merged[key] = templateValue(node, merged[key], 1);
147
+ });
148
+ return merged;
149
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cognizant-ai-lab/ui-common",
3
3
  "description": "A shared UI component library for Cognizant AI Labs projects",
4
- "version": "1.11.0",
4
+ "version": "1.12.0",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "yarn clean && yarn generate && yarn run --top-level tsc --project tsconfig.build.json && fix-esm-import-path dist > /dev/null",
@@ -43,11 +43,11 @@
43
43
  "fake-indexeddb": "6.2.5",
44
44
  "fix-esm-import-path": "1.10.3",
45
45
  "globals": "17.4.0",
46
- "openapi-typescript": "7.8.0",
46
+ "openapi-typescript": "7.13.0",
47
47
  "react": "19.2.4",
48
48
  "react-dom": "19.2.4",
49
49
  "typescript": "5.9.3",
50
- "vitest": "4.1.10"
50
+ "vitest": "4.1.11"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@emotion/react": "^11.14.0",