Skip to content

Commit cba3e16

Browse files
committed
refactor: modify details for teaching
1 parent f9bdbe6 commit cba3e16

14 files changed

Lines changed: 82 additions & 83 deletions

File tree

libs/ag-ui-client/a2ui-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { BoundProperty } from '@a2ui/angular/v0_9';
22
import type { Signal, Type } from '@angular/core';
3-
import { z } from 'zod';
3+
import { z } from 'zod/v3';
44

55
type StripPathBinding<T> = T extends { path: string } ? never : T;
66

libs/ag-ui-client/ag-ui-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type InputContentPart, type UserMessage } from '@ag-ui/core';
22
import { ResourceRef, Type } from '@angular/core';
33
import { z } from 'zod';
4+
import type { z as z3 } from 'zod/v3';
45

56
import { type A2uiCustomCatalogFunction } from './a2ui-schema';
67

@@ -109,7 +110,7 @@ export interface A2uiCustomCatalogComponent {
109110
name: string;
110111
description: string;
111112
component: Type<unknown>;
112-
schema: z.ZodTypeAny;
113+
schema: z3.ZodTypeAny;
113114
}
114115

115116
export interface A2uiCustomCatalog {

libs/ag-ui-client/ag-ui-utils/agents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type ResourceStreamItem,
1010
type WritableSignal,
1111
} from '@angular/core';
12-
import { z } from 'zod';
12+
import { zodToJsonSchema } from 'zod-to-json-schema';
1313

1414
import {
1515
type A2uiCustomCatalog,
@@ -75,7 +75,7 @@ function buildCatalogContext(
7575
>((acc, entry) => {
7676
acc[entry.name] = {
7777
description: entry.description,
78-
schema: z.toJSONSchema(entry.schema),
78+
schema: zodToJsonSchema(entry.schema),
7979
};
8080
return acc;
8181
}, {});
@@ -86,7 +86,7 @@ function buildCatalogContext(
8686
acc[fn.name] = {
8787
description: fn.description,
8888
returnType: fn.returnType,
89-
schema: z.toJSONSchema(fn.schema),
89+
schema: zodToJsonSchema(fn.schema),
9090
};
9191
return acc;
9292
}, {});

libs/ag-ui-client/provide-a2ui-catalog.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
InjectionToken,
1515
makeEnvironmentProviders,
1616
} from '@angular/core';
17-
import type { ZodTypeAny } from 'zod';
1817

1918
import { type A2uiCustomCatalogFunction } from './a2ui-schema';
2019
import {
@@ -49,20 +48,21 @@ function toAngularComponentImplementation(
4948
return {
5049
name: entry.name,
5150
component: entry.component,
52-
schema: entry.schema,
53-
} as unknown as AngularComponentImplementation;
51+
schema: entry.schema as unknown,
52+
} as AngularComponentImplementation;
5453
}
5554

5655
function toFunctionImplementation(
5756
fn: A2uiCustomCatalogFunction,
5857
): FunctionImplementation {
59-
return {
58+
const implementation: FunctionImplementation = {
6059
name: fn.name,
6160
returnType: fn.returnType,
62-
schema: fn.schema as unknown as ZodTypeAny,
61+
schema: fn.schema as unknown as FunctionImplementation['schema'],
6362
execute: (args: Record<string, unknown>) =>
6463
fn.execute(fn.schema.parse(args)),
65-
} as unknown as FunctionImplementation;
64+
};
65+
return implementation;
6666
}
6767

6868
/**

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@
9494
"quickjs-emscripten": "^0.32.0",
9595
"rxjs": "~7.8.0",
9696
"tslib": "^2.3.0",
97-
"zod": "^4.3.5"
97+
"zod": "^4.3.5",
98+
"zod-to-json-schema": "^3.25.1"
9899
},
99100
"devDependencies": {
100101
"@angular/build": "^21.2.16",

projects/a2ui-demo/src/app/app.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import {
77
inject,
88
} from '@angular/core';
99

10-
import {
11-
createSurfaceMessages,
12-
type Passenger,
13-
toUpdateMessage,
14-
} from './passenger-card';
10+
import { createSurfaceMessages, type Passenger } from './passenger-card';
1511

1612
@Component({
1713
selector: 'app-root',
@@ -52,12 +48,19 @@ export class App {
5248
return;
5349
}
5450

55-
const passenger = action.context as unknown as Passenger;
51+
const passenger = action.context as Passenger;
5652
this.renderer.processMessages([
57-
toUpdateMessage(this.surfaceId, {
58-
...passenger,
59-
bonusMiles: passenger.bonusMiles + 300,
60-
}),
53+
{
54+
version: 'v0.9',
55+
updateDataModel: {
56+
surfaceId: this.surfaceId,
57+
path: '/passenger',
58+
value: {
59+
...passenger,
60+
bonusMiles: passenger.bonusMiles + 300,
61+
},
62+
},
63+
},
6164
]);
6265
},
6366
);

projects/a2ui-demo/src/app/custom-catalog/custom-catalog.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
1-
import {
2-
type AngularComponentImplementation,
3-
BASIC_FUNCTIONS,
4-
BasicCatalogBase,
5-
} from '@a2ui/angular/v0_9';
6-
import { z } from 'zod/v3';
1+
import { BASIC_FUNCTIONS, BasicCatalogBase } from '@a2ui/angular/v0_9';
72

83
import { formatIdImplementation } from './format-id';
9-
import { MilesProgress } from './miles-progress';
10-
import { binding } from './utils';
11-
12-
const passengerSchema = z.object({
13-
id: z.number(),
14-
firstName: z.string(),
15-
lastName: z.string(),
16-
bonusMiles: z.number(),
17-
});
18-
19-
const milesProgressSchema = z
20-
.object({
21-
passenger: binding(passengerSchema).optional(),
22-
weight: z.number().optional(),
23-
})
24-
.strict();
25-
26-
const milesProgressEntry = {
27-
name: 'MilesProgress',
28-
component: MilesProgress,
29-
schema: milesProgressSchema,
30-
} as unknown as AngularComponentImplementation;
4+
import { milesProgressEntry } from './miles-progress';
315

326
export const customCatalog = new BasicCatalogBase({
337
id: 'https://example.com/catalogs/flights42-a2ui-demo',
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
import type { FunctionImplementation } from '@a2ui/web_core/v0_9';
2-
import { z, type ZodTypeAny } from 'zod/v3';
1+
import {
2+
createFunctionImplementation,
3+
type FunctionImplementation,
4+
} from '@a2ui/web_core/v0_9';
5+
import { z } from 'zod/v3';
36

47
const formatIdSchema = z
58
.object({
69
value: z.number(),
710
})
811
.strict();
912

10-
export const formatIdImplementation = {
11-
name: 'formatId',
12-
returnType: 'string',
13-
schema: formatIdSchema as unknown as ZodTypeAny,
14-
execute: (args: Record<string, unknown>) => {
15-
const { value } = formatIdSchema.parse(args);
13+
export const formatIdImplementation = createFunctionImplementation(
14+
{
15+
name: 'formatId',
16+
returnType: 'string',
17+
schema: formatIdSchema as unknown as FunctionImplementation['schema'],
18+
},
19+
({ value }) => {
1620
const normalizedValue = Math.max(0, Math.trunc(value));
1721

1822
return `P-${String(normalizedValue).padStart(4, '0')}`;
1923
},
20-
} as unknown as FunctionImplementation;
24+
);

projects/a2ui-demo/src/app/custom-catalog/miles-progress.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import type { AngularComponentImplementation } from '@a2ui/angular/v0_9';
12
import { DecimalPipe } from '@angular/common';
23
import {
34
ChangeDetectionStrategy,
45
Component,
56
computed,
67
input,
78
} from '@angular/core';
9+
import { z } from 'zod/v3';
810

911
import {
1012
calcNextThreshold,
1113
calcProgressPercent,
1214
calcRemainingMiles,
1315
} from './miles-calc';
1416
import { initialContext, MilesProgressContext } from './miles-progress-context';
17+
import { binding } from './utils';
1518

1619
@Component({
1720
selector: 'app-miles-progress',
@@ -49,3 +52,23 @@ export class MilesProgress {
4952
calcProgressPercent(this.nextThreshold(), this.passenger().bonusMiles),
5053
);
5154
}
55+
56+
const passengerSchema = z.object({
57+
id: z.number(),
58+
firstName: z.string(),
59+
lastName: z.string(),
60+
bonusMiles: z.number(),
61+
});
62+
63+
const milesProgressSchema = z
64+
.object({
65+
passenger: binding(passengerSchema).optional(),
66+
weight: z.number().optional(),
67+
})
68+
.strict();
69+
70+
export const milesProgressEntry = {
71+
name: 'MilesProgress',
72+
component: MilesProgress,
73+
schema: milesProgressSchema as unknown,
74+
} as AngularComponentImplementation;

0 commit comments

Comments
 (0)