@asterism-app/builtin-api 1.1.3 → 1.1.5

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.
@@ -4,7 +4,7 @@ A plugin can make its resource available through builtin's model-facing creation
4
4
 
5
5
  ## Reuse creation meaning
6
6
 
7
- Build on the original public creation contract rather than creating a parallel initialization path. A straightforward entry can expose that contract directly. A specialized entry is useful when a model can supply a smaller meaningful input, such as a short note from which the plugin derives a titled document.
7
+ Build on the original public creation contract rather than creating a parallel initialization path. Agent entries receive a parameter object even when there are no fields to supply. Reuse the creation schema directly when it describes an object, or use `inputSchema` and `toInput` to adapt that object to the underlying contract. This lets the domain contract keep inputs like `null` or a number without exposing them as the entire Agent input. An adapter can also expose a smaller meaningful input, such as a short note from which the plugin derives a titled document.
8
8
 
9
9
  Keep that adaptation explainable. It should translate intent into the same domain creation operation, not bypass validation or hide unrelated effects. Descriptions and schemas should make the resulting object's purpose understandable without a manual recipe.
10
10
 
@@ -15,8 +15,8 @@
15
15
  "react-intl": "^12.1.4"
16
16
  },
17
17
  "devDependencies": {
18
- "@asterism-app/sdk": "2.2.1",
19
- "@asterism-app/builtin-api": "1.1.3",
18
+ "@asterism-app/sdk": "2.3.0",
19
+ "@asterism-app/builtin-api": "1.1.5",
20
20
  "@asterism-app/plugin-build": "1.0.1",
21
21
  "typescript": "^7.0.2",
22
22
  "@types/node": "^26.6.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asterism-app/builtin-api",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Public domain contracts and extensions for Asterism built-in features",
5
5
  "license": "MIT",
6
6
  "author": "Chien Zhang",
package/src/runtime.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { belongsTo, jsonSchema, type CreationContract, type Json, type SessionDirectory } from "@asterism-app/sdk/contracts";
2
+ import { belongsTo, jsonSchema, type CreationContract, type Json, type JsonObject, type SessionDirectory } from "@asterism-app/sdk/contracts";
3
3
  import { defineModule, PluginApiError, type PluginContext, type PluginModule, type ObjectReader, type TopLevelToolContext } from "@asterism-app/sdk/runtime";
4
4
 
5
5
  export { agentServices, registerAgentServices, type AgentServices } from "./agent-services.ts";
@@ -7,6 +7,9 @@ export { defineAgentPreparer, captureAgentPreparers, type AgentPreparation, type
7
7
  export { defineAgentContextTransform, captureAgentContextTransforms,
8
8
  type AgentContextUnit, type AgentContextItem, type AgentContextRequest } from "./context-transform.ts";
9
9
 
10
+ /** The Agent's argument envelope; each entry defines the object's fields. */
11
+ export const agentCreationInputSchema = z.object({}).catchall(jsonSchema);
12
+
10
13
  export interface AgentCreationDescription {
11
14
  readonly id: string;
12
15
  readonly resourceKind: string;
@@ -16,7 +19,7 @@ export interface AgentCreationDescription {
16
19
  }
17
20
  interface Entry {
18
21
  readonly description: AgentCreationDescription;
19
- create(input: Json, context: TopLevelToolContext): ReturnType<TopLevelToolContext["session"]["create"]>;
22
+ create(input: JsonObject, context: TopLevelToolContext): ReturnType<TopLevelToolContext["session"]["create"]>;
20
23
  }
21
24
  type CreationOptions = Parameters<TopLevelToolContext["session"]["create"]>[2];
22
25
  /** Catalogs are isolated by host runtime instance; shared public exports do not mix plugin registrations from different hosts. */
@@ -33,25 +36,40 @@ function entry(plugin: PluginContext, id: string): Entry {
33
36
  return value;
34
37
  }
35
38
 
39
+ /** Zod can project object variants as unions and named schemas as local definitions. */
40
+ function describesObject(root: z.core.JSONSchema.BaseSchema): boolean {
41
+ function check(schema: z.core.JSONSchema.BaseSchema, refs: readonly string[] = []): boolean {
42
+ if (schema.type === "object" || schema.allOf?.some(branch => check(branch, refs))) return true;
43
+ const variants = schema.anyOf ?? schema.oneOf;
44
+ if (variants?.length && variants.every(branch => check(branch, refs))) return true;
45
+ const ref = schema.$ref;
46
+ if (!ref?.startsWith("#/$defs/") || refs.includes(ref)) return false;
47
+ const name = ref.slice("#/$defs/".length).replaceAll("~1", "/").replaceAll("~0", "~");
48
+ const definition = root.$defs && Object.hasOwn(root.$defs, name) ? root.$defs[name] : undefined;
49
+ return definition !== undefined && check(definition, [...refs, ref]);
50
+ }
51
+ return check(root);
52
+ }
53
+
36
54
  /** The domain service shared by built-in creation tools and plugins; querying is not a prerequisite for creation. */
37
55
  export const agentCreations = {
38
56
  list(plugin: PluginContext) {
39
57
  return [...directory(plugin).values()].map(({ description: { id, resourceKind, description } }) => ({ id, resourceKind, description }));
40
58
  },
41
59
  inspect(plugin: PluginContext, id: string): AgentCreationDescription { return structuredClone(entry(plugin, id).description); },
42
- create(plugin: PluginContext, id: string, input: Json, context: TopLevelToolContext) {
60
+ create(plugin: PluginContext, id: string, input: JsonObject, context: TopLevelToolContext) {
43
61
  return entry(plugin, id).create(input, context);
44
62
  },
45
63
  };
46
64
 
47
- /** Agent creation entry points are independent domain contributions; they reuse creation input directly or explicitly adapt model input. */
65
+ /** Agent creation entries accept objects. Use an adapter if the underlying creation takes a different input shape. */
48
66
  export function defineAgentCreation<I, S extends Json, O extends Json, A extends Json = Json>(definition: {
49
67
  id: string;
50
68
  description: string;
51
69
  guidelines?: readonly string[];
70
+ } & ({ creation: CreationContract<I & JsonObject, S, O>; inputSchema?: never; toInput?: never; toOptions?: never } | {
52
71
  creation: CreationContract<I, S, O>;
53
- } & ({ inputSchema?: never; toInput?: never; toOptions?: never } | {
54
- inputSchema: z.ZodType<A>;
72
+ inputSchema: z.ZodType<A, JsonObject>;
55
73
  toInput(input: A, context: { object: ObjectReader }): I & Json;
56
74
  toOptions?(input: A, context: { object: ObjectReader }): CreationOptions;
57
75
  })): PluginModule {
@@ -60,7 +78,10 @@ export function defineAgentCreation<I, S extends Json, O extends Json, A extends
60
78
  if (!definition.description.trim()) throw new PluginApiError("invalid_argument", "An Agent creation entry needs a description.");
61
79
  const entries = directory(plugin);
62
80
  if (entries.has(definition.id)) throw new PluginApiError("conflict", `Duplicate Agent creation entry: ${definition.id}`);
63
- const input = jsonSchema.parse(z.toJSONSchema(definition.inputSchema ?? definition.creation.schema, { io: "input" }));
81
+ const projected = z.toJSONSchema(definition.inputSchema ?? definition.creation.schema, { io: "input" });
82
+ if (!describesObject(projected)) throw new PluginApiError("invalid_argument",
83
+ `Agent creation entry ${definition.id} needs an object input schema. Use inputSchema and toInput to adapt other creation inputs.`);
84
+ const input = jsonSchema.parse(projected);
64
85
  const value: Entry = {
65
86
  description: { id: definition.id, resourceKind: definition.creation.resource.kind,
66
87
  description: definition.description, guidelines: [...definition.guidelines ?? []], input },
package/src/text.ts CHANGED
@@ -45,7 +45,7 @@ export const textChangeSchema = z.strictObject({
45
45
  diff: z.string().describe("The actual unified diff, preserving final-newline and CRLF differences."),
46
46
  added: z.number().int().nonnegative().describe("Number of added lines in the actual diff."),
47
47
  removed: z.number().int().nonnegative().describe("Number of removed lines in the actual diff."),
48
- anchors: z.array(textRowSchema).describe("Fresh anchors for lines produced by edit or replace, including a line shifted into a deletion gap. Always empty for write."),
48
+ anchors: z.array(lineAnchorSchema).describe("Fresh anchors for lines produced by edit or replace, including a line shifted into a deletion gap. Always empty for write."),
49
49
  anchorsTruncated: z.boolean().describe("True when the fresh anchors cover a subset of the changed lines."),
50
50
  }).describe("The committed content change with an actual diff and bounded fresh edit anchors. write returns an empty anchors array.");
51
51
  export type TextRead = z.infer<typeof textReadSchema>;
package/src/tools.ts CHANGED
@@ -52,7 +52,8 @@ export const Edit = defineTool({
52
52
  "append/prepend take body. Each body item is one text line; an empty string represents a blank line.",
53
53
  "All operations refer to the same pre-edit content and commit as one batch. Anchor or overlap failures preserve the original text.",
54
54
  "Replace/delete ranges conflict when they share original lines. Insertions can share range boundaries; an insertion strictly inside a replaced or deleted range conflicts. Insertions at the same original gap appear in edits array order.",
55
- "Anchor mismatch results include current-line or shifted candidates. Successful results include the actual diff and fresh anchors.",
55
+ "A successful edit reports the line counts and fresh LINE#HASH anchors for subsequent edits. The user-facing result contains the diff.",
56
+ "Anchor mismatch results include current-line or shifted candidates.",
56
57
  ],
57
58
  argsSchema: z.strictObject({
58
59
  edits: z.array(editOperationSchema).min(1).max(500).describe("One to 500 hashline operations referring to the same current text. Replace/delete ranges have no shared lines; insertions may share boundaries and use array order at the same gap."),
@@ -89,7 +90,7 @@ export const Replace = defineTool({
89
90
  "Literal mode matches find as text and inserts replace verbatim. Regex mode uses JavaScript regular expressions and replacement syntax.",
90
91
  "All non-overlapping matches are replaced. flags applies in both modes; global matching is always enabled.",
91
92
  "No matches returns an error without changing the target. Matches that produce identical content succeed without writing.",
92
- "The result includes the match count, actual diff, and fresh anchors for changed lines. These anchors can be used by edit.",
93
+ "The receipt reports the match count and fresh anchors for edit. The user-facing result contains the diff.",
93
94
  ],
94
95
  argsSchema: z.strictObject({
95
96
  find: z.string().min(1).describe("Non-empty text to find, or a JavaScript regular expression source when regex is true."),