@takeshape/schema 11.98.6 → 11.99.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 (44) hide show
  1. package/dist/agents.d.ts +13 -7
  2. package/dist/agents.js +78 -65
  3. package/dist/builtin-schema.js +12 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.js +1 -0
  6. package/dist/models/agent.d.ts +3 -0
  7. package/dist/models/agent.js +29 -0
  8. package/dist/models/project-schema.d.ts +17 -0
  9. package/dist/models/project-schema.js +112 -0
  10. package/dist/models/query.d.ts +2 -8
  11. package/dist/models/query.js +16 -4
  12. package/dist/models/runtime-schema.d.ts +3 -7
  13. package/dist/models/runtime-schema.js +9 -86
  14. package/dist/models/shape.d.ts +2 -10
  15. package/dist/models/shape.js +7 -4
  16. package/dist/models/types.d.ts +28 -4
  17. package/dist/models/user-schema.d.ts +7 -0
  18. package/dist/models/user-schema.js +9 -0
  19. package/dist/project-schema/latest.d.ts +33 -38
  20. package/dist/project-schema/v3.48.0.d.ts +33 -38
  21. package/dist/project-schema/v3.49.0.d.ts +33 -38
  22. package/dist/project-schema/v3.50.0.d.ts +33 -38
  23. package/dist/project-schema/v3.51.0.d.ts +33 -38
  24. package/dist/project-schema/v3.52.0.d.ts +33 -38
  25. package/dist/project-schema/v3.53.0.d.ts +33 -38
  26. package/dist/project-schema/v3.54.0.d.ts +33 -38
  27. package/dist/project-schema/v3.55.0.d.ts +33 -38
  28. package/dist/resolvers/takeshape/assets/asset-image-params.d.ts +1 -1
  29. package/dist/schema-util.d.ts +5 -4
  30. package/dist/schema-util.js +43 -3
  31. package/dist/schemas/project-schema/experimental.json +101 -131
  32. package/dist/util/mcp.d.ts +1 -0
  33. package/dist/util/mcp.js +1 -1
  34. package/dist/validate/ai.d.ts +6 -0
  35. package/dist/validate/ai.js +183 -0
  36. package/dist/validate/types.d.ts +1 -0
  37. package/dist/validate/types.js +1 -0
  38. package/dist/validate/util.d.ts +3 -0
  39. package/dist/validate/util.js +28 -0
  40. package/dist/validate/validate.d.ts +1 -0
  41. package/dist/validate/validate.js +14 -164
  42. package/examples/latest/agent-schema.json +24 -10
  43. package/examples/source/agent-schema.json +23 -9
  44. package/package.json +5 -5
package/dist/agents.d.ts CHANGED
@@ -1,14 +1,13 @@
1
- import type { AgentAPI, AgentAPIArgument, AgentJSON, Args, GuardJSON, ProjectSchemaJSON } from './project-schema/index.ts';
1
+ import type { AgentJSON, AgentMap, GuardJSON, ProjectSchemaJSON, QueryJSON } from './project-schema/index.ts';
2
+ export declare const START_AGENT_EXECUTION = "START";
2
3
  /**
3
4
  * Special transition destination ID for agent transitions that end the agent execution.
4
5
  */
5
- export declare const END_AGENT_EXECUTION = "endAgentExecution";
6
+ export declare const END_AGENT_EXECUTION = "END";
6
7
  /**
7
8
  * Special history strategy ID that indicates the history should be cleared.
8
9
  */
9
10
  export declare const CLEAR_HISTORY_STRATEGY_ID = "clearHistory";
10
- export declare const BUILT_IN_CHAT_ARGS: AgentAPIArgument[];
11
- export declare const BUILT_IN_CHAT_ARG_NAMES: string[];
12
11
  export type AgentEndTransition = {
13
12
  type: 'done' | 'progress';
14
13
  originStateId: string;
@@ -16,18 +15,21 @@ export type AgentEndTransition = {
16
15
  export declare const getAgentEndTransitions: (agent: AgentJSON, includeSuspend?: boolean) => AgentEndTransition[];
17
16
  export declare const getAgentEndStates: (agent: AgentJSON, includeSuspend?: boolean) => Set<string>;
18
17
  export declare const getInspectAgentSessionQueryName: (agentName: string) => string;
19
- export declare const removeBuiltInArgs: (args: AgentAPIArgument[]) => AgentAPIArgument[];
20
- export declare function getAgentApiArgs(api: AgentAPI): AgentAPIArgument[];
21
- export declare const createArgs: (agent: AgentJSON) => Args | undefined;
18
+ export declare function getAgentQueries(agents: AgentMap): Pick<ProjectSchemaJSON, 'queries' | 'mutations'>;
19
+ export declare function isValidAgentMutation(projectSchema: ProjectSchemaJSON, name: string): boolean;
22
20
  export declare function addAiQueries(projectSchema: ProjectSchemaJSON): ProjectSchemaJSON;
23
21
  /**
24
22
  * Where guards live in the schema.
25
23
  */
26
24
  export declare const GUARDS_SCHEMA_PATH: readonly ["ai-experimental", "guards"];
25
+ export declare const AGENTS_SCHEMA_PATH: readonly ["ai-experimental", "agents"];
27
26
  /**
28
27
  * Check if a guard is enabled.
29
28
  */
30
29
  export declare const isGuardEnabled: (guard: GuardJSON | undefined) => boolean;
30
+ export declare function getGuards(schema: ProjectSchemaJSON): (GuardJSON & {
31
+ id: string;
32
+ })[];
31
33
  /**
32
34
  * Get a guard configuration from the project schema.
33
35
  */
@@ -36,3 +38,7 @@ export declare function getGuardConfig(schema: ProjectSchemaJSON, guardId: strin
36
38
  * Get a guard configuration from the project schema, but only if it is enabled.
37
39
  */
38
40
  export declare function getEnabledGuardConfig(schema: ProjectSchemaJSON, guardId: string): GuardJSON | undefined;
41
+ export declare function getAgentMap(projectSchema: ProjectSchemaJSON): AgentMap | undefined;
42
+ export declare function getAgent(projectSchema: ProjectSchemaJSON, name: string): AgentJSON | undefined;
43
+ export declare function ensureAgent(projectSchema: ProjectSchemaJSON, name: string): AgentJSON;
44
+ export declare function isAgentQuery(query: QueryJSON, agentName: string): boolean;
package/dist/agents.js CHANGED
@@ -1,35 +1,21 @@
1
1
  import get from 'lodash/get.js';
2
2
  import uniq from 'lodash/uniq.js';
3
- import uniqBy from 'lodash/uniqBy.js';
4
3
  import upperFirst from 'lodash/upperFirst.js';
4
+ export const START_AGENT_EXECUTION = 'START';
5
5
  /**
6
6
  * Special transition destination ID for agent transitions that end the agent execution.
7
7
  */
8
- export const END_AGENT_EXECUTION = 'endAgentExecution';
8
+ export const END_AGENT_EXECUTION = 'END';
9
9
  /**
10
10
  * Special history strategy ID that indicates the history should be cleared.
11
11
  */
12
12
  export const CLEAR_HISTORY_STRATEGY_ID = 'clearHistory';
13
- export const BUILT_IN_CHAT_ARGS = [
14
- {
15
- argName: 'input',
16
- argDescription: 'Input text for the agent',
17
- argType: 'string',
18
- required: true
19
- },
20
- {
21
- argName: 'sessionId',
22
- argDescription: 'Session ID to continue existing session with this agent',
23
- argType: 'sessionId'
24
- }
25
- ];
26
- export const BUILT_IN_CHAT_ARG_NAMES = BUILT_IN_CHAT_ARGS.map((arg) => arg.argName);
27
13
  const transitionCanEnd = (transition, includeSuspend = false) => {
28
14
  if (!transition || transition.length === 0) {
29
15
  return true;
30
16
  }
31
17
  for (const step of transition) {
32
- if (includeSuspend && step.suspend) {
18
+ if (includeSuspend && step.type === 'suspend') {
33
19
  return true;
34
20
  }
35
21
  if (step.destination === END_AGENT_EXECUTION) {
@@ -59,41 +45,10 @@ export const getAgentEndStates = (agent, includeSuspend = false) => {
59
45
  export const getInspectAgentSessionQueryName = (agentName) => {
60
46
  return `inspect${upperFirst(agentName)}`;
61
47
  };
62
- export const removeBuiltInArgs = (args) => {
63
- return args.filter((arg) => !BUILT_IN_CHAT_ARG_NAMES.includes(arg.argName));
64
- };
65
- export function getAgentApiArgs(api) {
66
- let apiArguments = uniqBy(api.arguments ?? [], (arg) => arg.argName);
67
- if (api.type === 'chat') {
68
- apiArguments = removeBuiltInArgs(apiArguments).concat(BUILT_IN_CHAT_ARGS);
69
- }
70
- return apiArguments;
71
- }
72
- export const createArgs = (agent) => {
73
- const apiArguments = getAgentApiArgs(agent.api);
74
- return apiArguments.length > 0
75
- ? {
76
- type: 'object',
77
- properties: apiArguments.reduce((acc, argument) => {
78
- acc[argument.argName] = {
79
- type: argument.argType === 'sessionId' ? 'string' : argument.argType,
80
- description: argument.argDescription
81
- };
82
- return acc;
83
- }, {}),
84
- required: apiArguments.filter((arg) => arg.required).map((arg) => arg.argName)
85
- }
86
- : undefined;
87
- };
88
- export function addAiQueries(projectSchema) {
89
- const agents = projectSchema['ai-experimental']?.agents;
90
- if (!agents) {
91
- return projectSchema;
92
- }
93
- const newSchema = {
94
- ...projectSchema,
95
- queries: { ...projectSchema.queries },
96
- mutations: { ...projectSchema.mutations }
48
+ export function getAgentQueries(agents) {
49
+ const result = {
50
+ queries: {},
51
+ mutations: {}
97
52
  };
98
53
  for (const [agentName, agent] of Object.entries(agents)) {
99
54
  // Get valid return types based on states that could possibly be the end state
@@ -111,20 +66,25 @@ export function addAiQueries(projectSchema) {
111
66
  return 'JSON';
112
67
  }));
113
68
  const shape = returnTypes.length === 0 ? 'string' : returnTypes.length === 1 ? returnTypes[0] : 'JSON';
114
- if (newSchema.mutations[agentName] !== undefined) {
115
- throw new Error(`Schema already has a mutation with the name ${agentName}`);
116
- }
117
- newSchema.mutations[agentName] = {
118
- description: agent.description,
119
- args: createArgs(agent),
120
- shape,
121
- resolver: {
122
- name: 'ai:runAgent',
123
- agentName
69
+ for (const inputConfig of agent.api.inputs) {
70
+ if (inputConfig.type === 'mutation') {
71
+ const mutationName = inputConfig.name;
72
+ if (result.mutations[mutationName] !== undefined) {
73
+ throw new Error(`Schema already has a mutation with the name "${mutationName}"`);
74
+ }
75
+ result.mutations[mutationName] = {
76
+ description: agent.description,
77
+ args: inputConfig.args,
78
+ shape,
79
+ resolver: {
80
+ name: 'ai:runAgent',
81
+ agentName
82
+ }
83
+ };
124
84
  }
125
- };
85
+ }
126
86
  if (shape === 'TSChatResponse') {
127
- newSchema.queries[getInspectAgentSessionQueryName(agentName)] ||= {
87
+ result.queries[getInspectAgentSessionQueryName(agentName)] ||= {
128
88
  description: `Inspect a session for the ${agentName} agent`,
129
89
  args: {
130
90
  type: 'object',
@@ -143,18 +103,51 @@ export function addAiQueries(projectSchema) {
143
103
  };
144
104
  }
145
105
  }
146
- return newSchema;
106
+ return result;
107
+ }
108
+ export function isValidAgentMutation(projectSchema, name) {
109
+ for (const agent of Object.values(getAgentMap(projectSchema) ?? {})) {
110
+ for (const input of agent.api.inputs) {
111
+ if (input.type === 'mutation' && input.name === name) {
112
+ return true;
113
+ }
114
+ }
115
+ }
116
+ return false;
117
+ }
118
+ export function addAiQueries(projectSchema) {
119
+ const agents = getAgentMap(projectSchema);
120
+ if (!agents) {
121
+ return projectSchema;
122
+ }
123
+ const aiQueries = getAgentQueries(agents);
124
+ return {
125
+ ...projectSchema,
126
+ queries: {
127
+ ...projectSchema.queries,
128
+ ...aiQueries.queries
129
+ },
130
+ mutations: {
131
+ ...projectSchema.mutations,
132
+ ...aiQueries.mutations
133
+ }
134
+ };
147
135
  }
148
136
  /**
149
137
  * Where guards live in the schema.
150
138
  */
151
139
  export const GUARDS_SCHEMA_PATH = ['ai-experimental', 'guards'];
140
+ export const AGENTS_SCHEMA_PATH = ['ai-experimental', 'agents'];
152
141
  /**
153
142
  * Check if a guard is enabled.
154
143
  */
155
144
  export const isGuardEnabled = (guard) => {
156
145
  return Boolean(guard && guard.enabled !== false);
157
146
  };
147
+ export function getGuards(schema) {
148
+ const guards = schema['ai-experimental']?.guards;
149
+ return guards ? Object.entries(guards).map(([id, guard]) => ({ id, ...guard })) : [];
150
+ }
158
151
  /**
159
152
  * Get a guard configuration from the project schema.
160
153
  */
@@ -170,3 +163,23 @@ export function getEnabledGuardConfig(schema, guardId) {
170
163
  return config;
171
164
  }
172
165
  }
166
+ export function getAgentMap(projectSchema) {
167
+ return projectSchema['ai-experimental']?.agents;
168
+ }
169
+ export function getAgent(projectSchema, name) {
170
+ return getAgentMap(projectSchema)?.[name];
171
+ }
172
+ export function ensureAgent(projectSchema, name) {
173
+ const agent = getAgent(projectSchema, name);
174
+ if (!agent) {
175
+ throw new Error(`Agent "${name}" not found`);
176
+ }
177
+ return agent;
178
+ }
179
+ export function isAgentQuery(query, agentName) {
180
+ const resolver = query.resolver;
181
+ if ('agentName' in resolver) {
182
+ return resolver.agentName === agentName;
183
+ }
184
+ return false;
185
+ }
@@ -310,6 +310,18 @@ export const builtInShapes = {
310
310
  }
311
311
  }
312
312
  },
313
+ TSGenerateArgs: {
314
+ id: 'TSGenerateArgs',
315
+ name: 'TSGenerateArgs',
316
+ title: 'Generate Args',
317
+ schema: {
318
+ type: 'object',
319
+ properties: {
320
+ input: { type: 'string' }
321
+ },
322
+ required: ['input']
323
+ }
324
+ },
313
325
  TSChatArgs: {
314
326
  id: 'TSChatArgs',
315
327
  name: 'TSChatArgs',
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export * from './migration/index.ts';
13
13
  export type { ProjectSchemaUpdate } from './migration/types.ts';
14
14
  export * from './mocks.ts';
15
15
  export * from './models/runtime-schema.ts';
16
+ export * from './models/user-schema.ts';
16
17
  export * from './patterns.ts';
17
18
  export * from './project-schema/index.ts';
18
19
  export * from './project-schema/migrate.ts';
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ export * from "./interfaces.js";
11
11
  export * from "./migration/index.js";
12
12
  export * from "./mocks.js";
13
13
  export * from "./models/runtime-schema.js";
14
+ export * from "./models/user-schema.js";
14
15
  export * from "./patterns.js";
15
16
  export * from "./project-schema/index.js";
16
17
  export * from "./project-schema/migrate.js";
@@ -0,0 +1,3 @@
1
+ import type { AgentJSON } from '../project-schema/latest.ts';
2
+ import type { Agent, ProjectSchema } from './types.ts';
3
+ export declare function createAgent(parent: ProjectSchema, name: string, agent: AgentJSON): Agent;
@@ -0,0 +1,29 @@
1
+ import { isAgentQuery } from "../agents.js";
2
+ function queryPredicate(agentName) {
3
+ return (query) => isAgentQuery(query.json, agentName);
4
+ }
5
+ class AgentImpl {
6
+ #schema;
7
+ #agentName;
8
+ #agent;
9
+ constructor(parent, name, agent) {
10
+ this.#schema = parent;
11
+ this.#agentName = name;
12
+ this.#agent = agent;
13
+ }
14
+ get name() {
15
+ return this.#agentName;
16
+ }
17
+ getMutations() {
18
+ return this.#schema.getLocalMutations().filter(queryPredicate(this.name));
19
+ }
20
+ getQueries() {
21
+ return this.#schema.getLocalQueries().filter(queryPredicate(this.name));
22
+ }
23
+ get json() {
24
+ return this.#agent;
25
+ }
26
+ }
27
+ export function createAgent(parent, name, agent) {
28
+ return new AgentImpl(parent, name, agent);
29
+ }
@@ -0,0 +1,17 @@
1
+ import type { ProjectSchemaJSON, PropertySchema } from '../project-schema/index.ts';
2
+ import type { ServiceLayers } from '../types/types.ts';
3
+ import type { Agent, ProjectSchema, Query, Shape } from './types.ts';
4
+ export declare class ProjectSchemaImpl implements ProjectSchema {
5
+ #private;
6
+ constructor(projectSchemaJson: ProjectSchemaJSON, layers: ServiceLayers);
7
+ getShape(rawShapeRef: string): Shape | undefined;
8
+ getQuery(rawQueryRef: string): Query | undefined;
9
+ ensureQuery(ref: string): Query;
10
+ getLocalQueries(): Query[];
11
+ getLocalMutations(): Query[];
12
+ getAgents(): Agent[];
13
+ ensureAgent(name: string): Agent;
14
+ getAgent(name: string): Agent | undefined;
15
+ dereferenceSchema(propertySchema: PropertySchema): PropertySchema;
16
+ get json(): ProjectSchemaJSON;
17
+ }
@@ -0,0 +1,112 @@
1
+ import { getAgent, getAgentMap } from "../agents.js";
2
+ import { dereferenceSchema, getQuery, normalizePropertyRef, normalizeRefExpression, parsePropertyRef, parseRef, refItemToNamespacedShapeName } from "../refs.js";
3
+ import { getShape } from "../util/shapes.js";
4
+ import { createAgent } from "./agent.js";
5
+ import { createQuery } from './query.js';
6
+ import { createShape } from './shape.js';
7
+ export class ProjectSchemaImpl {
8
+ #projectSchemaJson;
9
+ #layers;
10
+ #shapeCache;
11
+ #queryCache;
12
+ #agentCache;
13
+ constructor(projectSchemaJson, layers) {
14
+ this.#projectSchemaJson = projectSchemaJson;
15
+ this.#layers = layers;
16
+ this.#shapeCache = new Map();
17
+ this.#queryCache = new Map();
18
+ this.#agentCache = new Map();
19
+ }
20
+ getShape(rawShapeRef) {
21
+ const shapeRef = normalizeRefExpression(this.#projectSchemaJson, rawShapeRef);
22
+ let shape = this.#shapeCache.get(shapeRef);
23
+ if (!shape) {
24
+ const refItem = parseRef(this.#projectSchemaJson, rawShapeRef);
25
+ if (refItem) {
26
+ const namespacedName = refItemToNamespacedShapeName(this.#projectSchemaJson, refItem);
27
+ const shapeJson = getShape(this.#projectSchemaJson, namespacedName);
28
+ if (shapeJson) {
29
+ shape = createShape(this, refItem, shapeJson);
30
+ this.#shapeCache.set(shapeRef, shape);
31
+ }
32
+ else if (refItem.layerId !== 'local') {
33
+ const layerSchema = this.#layers[refItem.layerId]?.schema;
34
+ if (layerSchema) {
35
+ const shapeJson = getShape(layerSchema, namespacedName);
36
+ if (shapeJson) {
37
+ shape = createShape(this, refItem, shapeJson);
38
+ this.#shapeCache.set(shapeRef, shape);
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+ return shape;
45
+ }
46
+ getQuery(rawQueryRef) {
47
+ const queryRef = normalizePropertyRef(rawQueryRef);
48
+ let query = this.#queryCache.get(queryRef);
49
+ if (!query) {
50
+ const queryEntry = getQuery(this.#projectSchemaJson, queryRef);
51
+ if (queryEntry) {
52
+ query = createQuery(this, queryEntry);
53
+ this.#queryCache.set(queryRef, query);
54
+ }
55
+ else {
56
+ const parsedRef = parsePropertyRef(queryRef);
57
+ if (parsedRef && parsedRef.layerId !== 'local') {
58
+ const layerSchema = this.#layers[parsedRef.layerId]?.schema;
59
+ if (layerSchema) {
60
+ const queryEntry = getQuery(layerSchema, parsedRef.propertyName);
61
+ if (queryEntry) {
62
+ query = createQuery(this, { ...queryEntry, ref: parsedRef });
63
+ this.#queryCache.set(queryRef, query);
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
69
+ return query;
70
+ }
71
+ ensureQuery(ref) {
72
+ const query = this.getQuery(ref);
73
+ if (!query) {
74
+ throw new Error(`Query "${ref}" not found`);
75
+ }
76
+ return query;
77
+ }
78
+ getLocalQueries() {
79
+ return Object.keys(this.#projectSchemaJson.queries).map((queryName) => this.ensureQuery(`Query.${queryName}`));
80
+ }
81
+ getLocalMutations() {
82
+ return Object.keys(this.#projectSchemaJson.mutations).map((queryName) => this.ensureQuery(`Mutation.${queryName}`));
83
+ }
84
+ getAgents() {
85
+ const agents = getAgentMap(this.#projectSchemaJson);
86
+ return agents ? Object.keys(agents).map((name) => this.ensureAgent(name)) : [];
87
+ }
88
+ ensureAgent(name) {
89
+ const agent = this.getAgent(name);
90
+ if (!agent) {
91
+ throw new Error(`Agent "${name}" not found`);
92
+ }
93
+ return agent;
94
+ }
95
+ getAgent(name) {
96
+ let agent = this.#agentCache.get(name);
97
+ if (!agent) {
98
+ const agentJson = getAgent(this.#projectSchemaJson, name);
99
+ if (agentJson) {
100
+ agent = createAgent(this, name, agentJson);
101
+ this.#agentCache.set(name, agent);
102
+ }
103
+ }
104
+ return agent;
105
+ }
106
+ dereferenceSchema(propertySchema) {
107
+ return dereferenceSchema(this.#projectSchemaJson, propertySchema);
108
+ }
109
+ get json() {
110
+ return this.#projectSchemaJson;
111
+ }
112
+ }
@@ -1,9 +1,3 @@
1
- import type { ObjectSchema } from '../project-schema/latest.ts';
2
1
  import { type QueryEntry } from '../refs.ts';
3
- import type { ReferenceResolver } from './types.ts';
4
- export declare class Query {
5
- #private;
6
- constructor(parent: ReferenceResolver, entry: QueryEntry);
7
- get ref(): string;
8
- get argsSchema(): ObjectSchema | undefined;
9
- }
2
+ import type { ProjectSchema, Query } from './types.ts';
3
+ export declare function createQuery(parent: ProjectSchema, entry: QueryEntry): Query;
@@ -1,11 +1,11 @@
1
1
  import { serializePropertyRef } from "../refs.js";
2
- export class Query {
3
- #refResolver;
2
+ class QueryImpl {
3
+ #schema;
4
4
  #name;
5
5
  #query;
6
6
  #ref;
7
7
  constructor(parent, entry) {
8
- this.#refResolver = parent;
8
+ this.#schema = parent;
9
9
  this.#name = entry.name;
10
10
  this.#query = entry.query;
11
11
  this.#ref = entry.ref;
@@ -19,8 +19,20 @@ export class Query {
19
19
  if (typeof args === 'object') {
20
20
  return args;
21
21
  }
22
- return this.#refResolver.getShape(args)?.getObjectSchema();
22
+ return this.#schema.getShape(args)?.getObjectSchema();
23
23
  }
24
24
  return undefined;
25
25
  }
26
+ get type() {
27
+ return this.#ref.shapeName === 'Mutation' ? 'mutation' : 'query';
28
+ }
29
+ get name() {
30
+ return this.#name;
31
+ }
32
+ get json() {
33
+ return this.#query;
34
+ }
35
+ }
36
+ export function createQuery(parent, entry) {
37
+ return new QueryImpl(parent, entry);
26
38
  }
@@ -1,13 +1,9 @@
1
1
  import type { ProjectSchemaJSON } from '../project-schema/index.ts';
2
2
  import type { ServiceLayers } from '../types/types.ts';
3
- import { Query } from './query.js';
4
- import { Shape } from './shape.js';
5
- export declare class RuntimeSchema {
6
- #private;
3
+ import { ProjectSchemaImpl } from './project-schema.ts';
4
+ export declare class RuntimeSchema extends ProjectSchemaImpl {
7
5
  private constructor();
8
6
  static buildSchema(userSchema: ProjectSchemaJSON, layers: ServiceLayers): RuntimeSchema;
7
+ static safeBuildSchema(userSchema: ProjectSchemaJSON, layers: ServiceLayers): RuntimeSchema | undefined;
9
8
  static fromJson(runtimeSchema: ProjectSchemaJSON, layers: ServiceLayers): RuntimeSchema;
10
- getQuery(ref: string): Query | undefined;
11
- getShape(ref: string): Shape | undefined;
12
- get json(): ProjectSchemaJSON;
13
9
  }
@@ -1,98 +1,21 @@
1
- import { dereferenceSchema, getQuery, normalizePropertyRef, normalizeRefExpression, parsePropertyRef, parseRef, refItemToNamespacedShapeName } from "../refs.js";
2
1
  import { buildRuntimeSchema } from "../runtime-schema.js";
3
- import { getShape } from "../util/shapes.js";
4
- import { Query } from './query.js';
5
- import { Shape } from './shape.js';
6
- class ProjectSchemaRefResolver {
7
- #runtimeSchemaJson;
8
- #layers;
9
- #shapeCache;
10
- #queryCache;
2
+ import { ProjectSchemaImpl } from "./project-schema.js";
3
+ export class RuntimeSchema extends ProjectSchemaImpl {
11
4
  constructor(runtimeSchema, layers) {
12
- this.#runtimeSchemaJson = runtimeSchema;
13
- this.#layers = layers;
14
- this.#shapeCache = new Map();
15
- this.#queryCache = new Map();
16
- }
17
- getShape(rawShapeRef) {
18
- const shapeRef = normalizeRefExpression(this.#runtimeSchemaJson, rawShapeRef);
19
- let shape = this.#shapeCache.get(shapeRef);
20
- if (!shape) {
21
- const refItem = parseRef(this.#runtimeSchemaJson, rawShapeRef);
22
- if (refItem) {
23
- const namespacedName = refItemToNamespacedShapeName(this.#runtimeSchemaJson, refItem);
24
- const shapeJson = getShape(this.#runtimeSchemaJson, namespacedName);
25
- if (shapeJson) {
26
- shape = new Shape(this, refItem, shapeJson);
27
- this.#shapeCache.set(shapeRef, shape);
28
- }
29
- else if (refItem.layerId !== 'local') {
30
- const layerSchema = this.#layers[refItem.layerId]?.schema;
31
- if (layerSchema) {
32
- const shapeJson = getShape(layerSchema, namespacedName);
33
- if (shapeJson) {
34
- shape = new Shape(this, refItem, shapeJson);
35
- this.#shapeCache.set(shapeRef, shape);
36
- }
37
- }
38
- }
39
- }
40
- }
41
- return shape;
42
- }
43
- getQuery(rawQueryRef) {
44
- const queryRef = normalizePropertyRef(rawQueryRef);
45
- let query = this.#queryCache.get(queryRef);
46
- if (!query) {
47
- const queryEntry = getQuery(this.#runtimeSchemaJson, queryRef);
48
- if (queryEntry) {
49
- query = new Query(this, queryEntry);
50
- this.#queryCache.set(queryRef, query);
51
- }
52
- else {
53
- const parsedRef = parsePropertyRef(queryRef);
54
- if (parsedRef && parsedRef.layerId !== 'local') {
55
- const layerSchema = this.#layers[parsedRef.layerId]?.schema;
56
- if (layerSchema) {
57
- const queryEntry = getQuery(layerSchema, parsedRef.propertyName);
58
- if (queryEntry) {
59
- query = new Query(this, { ...queryEntry, ref: parsedRef });
60
- this.#queryCache.set(queryRef, query);
61
- }
62
- }
63
- }
64
- }
65
- }
66
- return query;
67
- }
68
- dereferenceSchema(propertySchema) {
69
- return dereferenceSchema(this.#runtimeSchemaJson, propertySchema);
70
- }
71
- }
72
- export class RuntimeSchema {
73
- #runtimeSchemaJson;
74
- #layers;
75
- #refResolver;
76
- constructor(runtimeSchema, layers) {
77
- this.#runtimeSchemaJson = runtimeSchema;
78
- this.#layers = layers;
79
- this.#refResolver = new ProjectSchemaRefResolver(runtimeSchema, layers);
5
+ super(runtimeSchema, layers);
80
6
  }
81
7
  static buildSchema(userSchema, layers) {
82
8
  // biome-ignore lint/suspicious/noConsole: ignore
83
9
  const runtimeSchema = buildRuntimeSchema(userSchema, layers, console.log);
84
10
  return new RuntimeSchema(runtimeSchema, layers);
85
11
  }
12
+ static safeBuildSchema(userSchema, layers) {
13
+ try {
14
+ return RuntimeSchema.buildSchema(userSchema, layers);
15
+ }
16
+ catch { }
17
+ }
86
18
  static fromJson(runtimeSchema, layers) {
87
19
  return new RuntimeSchema(runtimeSchema, layers);
88
20
  }
89
- getQuery(ref) {
90
- return this.#refResolver.getQuery(ref);
91
- }
92
- getShape(ref) {
93
- return this.#refResolver.getShape(ref);
94
- }
95
- get json() {
96
- return this.#runtimeSchemaJson;
97
- }
98
21
  }
@@ -1,12 +1,4 @@
1
1
  import type { ShapeJSON } from '../project-schema/latest.ts';
2
2
  import { type RefItem } from '../refs.ts';
3
- import type { ReferenceResolver } from './types.ts';
4
- export declare class Shape {
5
- #private;
6
- constructor(parent: ReferenceResolver, ref: RefItem, shape: ShapeJSON);
7
- get name(): string;
8
- get ref(): string;
9
- get refItem(): RefItem;
10
- get json(): ShapeJSON;
11
- getObjectSchema(): import("../project-schema/latest.ts").ObjectSchema | undefined;
12
- }
3
+ import type { ProjectSchema, Shape } from './types.ts';
4
+ export declare function createShape(parent: ProjectSchema, ref: RefItem, shape: ShapeJSON): Shape;
@@ -1,12 +1,12 @@
1
1
  import { serializeRef } from "../refs.js";
2
2
  import { isObjectSchema } from "../types/utils.js";
3
- export class Shape {
4
- #refResolver;
3
+ class ShapeImpl {
4
+ #schema;
5
5
  #ref;
6
6
  #shape;
7
7
  #dereferencedSchema;
8
8
  constructor(parent, ref, shape) {
9
- this.#refResolver = parent;
9
+ this.#schema = parent;
10
10
  this.#ref = ref;
11
11
  this.#shape = shape;
12
12
  }
@@ -24,10 +24,13 @@ export class Shape {
24
24
  }
25
25
  getObjectSchema() {
26
26
  if (!this.#dereferencedSchema) {
27
- this.#dereferencedSchema = this.#refResolver.dereferenceSchema(this.#shape.schema);
27
+ this.#dereferencedSchema = this.#schema.dereferenceSchema(this.#shape.schema);
28
28
  }
29
29
  if (isObjectSchema(this.#dereferencedSchema)) {
30
30
  return this.#dereferencedSchema;
31
31
  }
32
32
  }
33
33
  }
34
+ export function createShape(parent, ref, shape) {
35
+ return new ShapeImpl(parent, ref, shape);
36
+ }