akanjs 3.0.0-alpha.32 → 3.0.0-alpha.34

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.0.0-alpha.32",
3
+ "version": "3.0.0-alpha.34",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -82,9 +82,9 @@ export class StToolBuilder<Args extends unknown[] = []> {
82
82
  type: T,
83
83
  option: StToolArgOption<ArgValue<T>> = {},
84
84
  ): StToolBuilder<[...Args, ArgValue<T> | null]> {
85
-
86
- StToolBuilder.schemaOf(type);
87
- return new StToolBuilder(this.#name, this.#meta, [
85
+
86
+ const published = this.#name && StToolBuilder.#describable(this.#name, name, type) ? this.#name : null;
87
+ return new StToolBuilder(published, this.#meta, [
88
88
  ...this.#args,
89
89
  { name, type, optional: !!option.optional, ...(option.oneOf ? { oneOf: option.oneOf } : {}) },
90
90
  ]);
@@ -150,6 +150,20 @@ export class StToolBuilder<Args extends unknown[] = []> {
150
150
  return arg.oneOf ? { ...schema, enum: [...arg.oneOf] } : schema;
151
151
  }
152
152
 
153
+ static #describable(toolName: string, argName: string, type: ParamFieldType): boolean {
154
+ try {
155
+ StToolBuilder.schemaOf(type);
156
+ return true;
157
+ } catch (error) {
158
+ console.error(
159
+ `st.tool("${toolName}") is not published: its "${argName}" argument is ${
160
+ error instanceof Error ? error.message : String(error)
161
+ }`,
162
+ );
163
+ return false;
164
+ }
165
+ }
166
+
153
167
  /** Scalars and enums only — the value arrives as JSON from a model, so a class instance has no way in. */
154
168
  static schemaOf(type: ParamFieldType): JsonSchema {
155
169
  if (isEnum(type as Cls)) {
@@ -159,7 +173,7 @@ export class StToolBuilder<Args extends unknown[] = []> {
159
173
  }
160
174
  const scalar = type as typeof PrimitiveScalar;
161
175
  if (!PrimitiveRegistry.has(scalar as unknown as Cls))
162
- throw new Error("st.tool takes scalar and enum arguments only.");
176
+ throw new Error(`${StToolBuilder.#typeName(type)}, and st.tool takes scalar and enum arguments only.`);
163
177
  switch (PrimitiveRegistry.getName(scalar)) {
164
178
  case "ID":
165
179
  case "String":
@@ -173,10 +187,15 @@ export class StToolBuilder<Args extends unknown[] = []> {
173
187
  case "Date":
174
188
  return { type: "string", format: "date-time" };
175
189
  default:
176
- throw new Error(`st.tool cannot describe the scalar ${PrimitiveRegistry.getName(scalar)}.`);
190
+ throw new Error(`the scalar ${PrimitiveRegistry.getName(scalar)}, which st.tool cannot describe.`);
177
191
  }
178
192
  }
179
193
 
194
+ static #typeName(type: ParamFieldType): string {
195
+ const named = type as { name?: string } | null | undefined;
196
+ return named?.name ? `the type ${named.name}` : `${String(type)}`;
197
+ }
198
+
180
199
  static positionalOf(toolName: string, args: StToolArg[], named: Record<string, unknown>): unknown[] {
181
200
  return args.map((arg) => {
182
201
  const value = named[arg.name];
@@ -20,16 +20,30 @@ export const useStState = <T>(
20
20
  initial: T | (() => T),
21
21
  meta: StStateMeta<T> = {},
22
22
  ): [T, Dispatch<SetStateAction<T>>] => {
23
- const set = meta.set;
23
+
24
+ const set = name && meta.set ? writable(name, meta.set) : null;
24
25
  return useAgentState<T>(name, initial, {
25
26
  description: meta.desc,
26
27
  report: meta.report,
27
28
  serialize: meta.serialize ?? ((value) => readableValue(name ?? "", value, meta.mask)),
28
29
  ...(set
29
30
  ? {
30
- set: StToolBuilder.schemaOf(set),
31
- parse: (value) => StToolBuilder.checkedValue(`set${capitalize(name ?? "")}`, "value", set, value) as T,
31
+ set: set.schema,
32
+ parse: (value) => StToolBuilder.checkedValue(`set${capitalize(name ?? "")}`, "value", set.type, value) as T,
32
33
  }
33
34
  : {}),
34
35
  });
35
36
  };
37
+
38
+ const writable = (name: string, type: ParamFieldType) => {
39
+ try {
40
+ return { schema: StToolBuilder.schemaOf(type), type };
41
+ } catch (error) {
42
+ console.error(
43
+ `st.useState("${name}") stays read-only: its "set" type is ${
44
+ error instanceof Error ? error.message : String(error)
45
+ }`,
46
+ );
47
+ return null;
48
+ }
49
+ };