ai-props 2.1.3 → 2.4.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 (105) hide show
  1. package/.dev.vars +2 -0
  2. package/.turbo/turbo-build.log +1 -1
  3. package/CHANGELOG.md +20 -0
  4. package/README.md +2 -0
  5. package/dist/ai.d.ts.map +1 -1
  6. package/dist/ai.js +4 -4
  7. package/dist/ai.js.map +1 -1
  8. package/dist/cascade.d.ts +329 -0
  9. package/dist/cascade.d.ts.map +1 -0
  10. package/dist/cascade.js +522 -0
  11. package/dist/cascade.js.map +1 -0
  12. package/dist/client.d.ts +233 -0
  13. package/dist/client.d.ts.map +1 -0
  14. package/dist/client.js +191 -0
  15. package/dist/client.js.map +1 -0
  16. package/dist/durable-cascade.d.ts +280 -0
  17. package/dist/durable-cascade.d.ts.map +1 -0
  18. package/dist/durable-cascade.js +469 -0
  19. package/dist/durable-cascade.js.map +1 -0
  20. package/dist/event-bridge.d.ts +257 -0
  21. package/dist/event-bridge.d.ts.map +1 -0
  22. package/dist/event-bridge.js +317 -0
  23. package/dist/event-bridge.js.map +1 -0
  24. package/dist/generate.d.ts.map +1 -1
  25. package/dist/generate.js +12 -6
  26. package/dist/generate.js.map +1 -1
  27. package/dist/hoc.d.ts.map +1 -1
  28. package/dist/hoc.js +13 -13
  29. package/dist/hoc.js.map +1 -1
  30. package/dist/hono-jsx.d.ts +208 -0
  31. package/dist/hono-jsx.d.ts.map +1 -0
  32. package/dist/hono-jsx.js +459 -0
  33. package/dist/hono-jsx.js.map +1 -0
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +2 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/mdx-types.d.ts +152 -0
  39. package/dist/mdx-types.d.ts.map +1 -0
  40. package/dist/mdx-types.js +9 -0
  41. package/dist/mdx-types.js.map +1 -0
  42. package/dist/mdx-utils.d.ts +106 -0
  43. package/dist/mdx-utils.d.ts.map +1 -0
  44. package/dist/mdx-utils.js +384 -0
  45. package/dist/mdx-utils.js.map +1 -0
  46. package/dist/mdx.d.ts +230 -0
  47. package/dist/mdx.d.ts.map +1 -0
  48. package/dist/mdx.js +820 -0
  49. package/dist/mdx.js.map +1 -0
  50. package/dist/rpc.d.ts +313 -0
  51. package/dist/rpc.d.ts.map +1 -0
  52. package/dist/rpc.js +359 -0
  53. package/dist/rpc.js.map +1 -0
  54. package/dist/streaming.d.ts +199 -0
  55. package/dist/streaming.d.ts.map +1 -0
  56. package/dist/streaming.js +402 -0
  57. package/dist/streaming.js.map +1 -0
  58. package/dist/validate.d.ts.map +1 -1
  59. package/dist/validate.js +11 -13
  60. package/dist/validate.js.map +1 -1
  61. package/dist/worker.d.ts +270 -0
  62. package/dist/worker.d.ts.map +1 -0
  63. package/dist/worker.js +405 -0
  64. package/dist/worker.js.map +1 -0
  65. package/package.json +39 -13
  66. package/src/ai.ts +12 -31
  67. package/src/cascade.ts +795 -0
  68. package/src/client.ts +440 -0
  69. package/src/durable-cascade.ts +743 -0
  70. package/src/event-bridge.ts +478 -0
  71. package/src/generate.ts +14 -12
  72. package/src/hoc.ts +15 -19
  73. package/src/hono-jsx.ts +675 -0
  74. package/src/index.ts +30 -0
  75. package/src/mdx-types.ts +169 -0
  76. package/src/mdx-utils.ts +437 -0
  77. package/src/mdx.ts +1008 -0
  78. package/src/rpc.ts +614 -0
  79. package/src/streaming.ts +618 -0
  80. package/src/validate.ts +15 -29
  81. package/src/worker.ts +547 -0
  82. package/test/cascade.test.ts +338 -0
  83. package/test/durable-cascade.test.ts +319 -0
  84. package/test/event-bridge.test.ts +351 -0
  85. package/test/generate.test.ts +6 -16
  86. package/test/mdx.test.ts +817 -0
  87. package/test/worker/capnweb-rpc.test.ts +1084 -0
  88. package/test/worker/full-flow.integration.test.ts +1463 -0
  89. package/test/worker/hono-jsx.test.ts +1258 -0
  90. package/test/worker/mdx-parsing.test.ts +1148 -0
  91. package/test/worker/setup.ts +56 -0
  92. package/test/worker.test.ts +595 -0
  93. package/tsconfig.json +2 -1
  94. package/vitest.config.js +6 -0
  95. package/vitest.config.ts +15 -1
  96. package/vitest.workers.config.ts +58 -0
  97. package/wrangler.jsonc +27 -0
  98. package/LICENSE +0 -21
  99. package/src/ai.js +0 -198
  100. package/src/cache.js +0 -182
  101. package/src/generate.js +0 -220
  102. package/src/hoc.js +0 -235
  103. package/src/index.js +0 -20
  104. package/src/types.js +0 -6
  105. package/src/validate.js +0 -252
@@ -0,0 +1,233 @@
1
+ /**
2
+ * RPC Client for AI Props
3
+ *
4
+ * Provides a typed RPC client that connects to the deployed
5
+ * ai-props worker using rpc.do for remote procedure calls.
6
+ *
7
+ * This module is for client-side usage - it provides types and
8
+ * helpers for consuming the PropsService worker from other workers
9
+ * or client applications.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { createPropsClient } from 'ai-props/client'
14
+ *
15
+ * const client = createPropsClient('https://ai-props.workers.dev')
16
+ * const result = await client.generate({
17
+ * schema: { title: 'Page title', description: 'Page description' },
18
+ * context: { topic: 'AI' }
19
+ * })
20
+ * console.log(result.props)
21
+ * ```
22
+ *
23
+ * @packageDocumentation
24
+ */
25
+ export type { PropSchema, GeneratePropsOptions, GeneratePropsResult, AIPropsConfig, PropsCache, PropsCacheEntry, ValidationResult, ValidationError, UseAIPropsResult, UseAIPropsOptions, AIComponentOptions, AIComponent, InferProps, } from './types.js';
26
+ export type { PropsServiceCoreInterface, PropsClientOptions, PropsClientInstance, PropsRPCErrorCode, PropsRPCError, RetryConfig, RetryBackoff, PropsServiceBinding, ExtractService, } from './rpc.js';
27
+ export { isPropsRPCError, withRetry, calculateRetryDelay, DEFAULT_RETRY_CONFIG } from './rpc.js';
28
+ /**
29
+ * Configuration for the AI Props HTTP RPC client
30
+ */
31
+ export interface PropsHttpClientConfig {
32
+ /** RPC endpoint URL (default: https://ai-props.do) */
33
+ url?: string;
34
+ /** Authentication token */
35
+ token?: string;
36
+ /** Request timeout in milliseconds (default: 30000) */
37
+ timeout?: number;
38
+ /** Custom headers to include in requests */
39
+ headers?: Record<string, string>;
40
+ }
41
+ import type { PropSchema, GeneratePropsOptions, GeneratePropsResult, AIPropsConfig, PropsCacheEntry, ValidationResult } from './types.js';
42
+ /**
43
+ * PropsClientAPI - Type-safe interface for the AI Props RPC client
44
+ *
45
+ * This interface mirrors all public methods on PropsServiceCore so that
46
+ * the RPC client provides full type safety when calling remote methods.
47
+ */
48
+ export interface PropsClientAPI {
49
+ /**
50
+ * Generate props using AI
51
+ */
52
+ generate<T = Record<string, unknown>>(options: GeneratePropsOptions): Promise<GeneratePropsResult<T>>;
53
+ /**
54
+ * Generate props synchronously from cache
55
+ * @throws {Error} If props are not in cache
56
+ */
57
+ getSync<T = Record<string, unknown>>(schema: PropSchema, context?: Record<string, unknown>): Promise<T>;
58
+ /**
59
+ * Pre-generate props for warming the cache
60
+ */
61
+ prefetch(requests: GeneratePropsOptions[]): Promise<void>;
62
+ /**
63
+ * Generate multiple prop sets in parallel
64
+ */
65
+ generateMany<T = Record<string, unknown>>(requests: GeneratePropsOptions[]): Promise<GeneratePropsResult<T>[]>;
66
+ /**
67
+ * Merge partial props with generated props
68
+ */
69
+ mergeWithGenerated<T extends Record<string, unknown>>(schema: PropSchema, partialProps: Partial<T>, options?: Omit<GeneratePropsOptions, 'schema' | 'context'>): Promise<T>;
70
+ /**
71
+ * Configure global AI props settings
72
+ */
73
+ configure(config: Partial<AIPropsConfig>): Promise<void>;
74
+ /**
75
+ * Get current configuration
76
+ */
77
+ getConfig(): Promise<AIPropsConfig>;
78
+ /**
79
+ * Reset configuration to defaults
80
+ */
81
+ resetConfig(): Promise<void>;
82
+ /**
83
+ * Get cached props by key
84
+ */
85
+ getCached<T>(key: string): Promise<PropsCacheEntry<T> | undefined>;
86
+ /**
87
+ * Set props in cache
88
+ */
89
+ setCached<T>(key: string, props: T): Promise<void>;
90
+ /**
91
+ * Delete cached entry by key
92
+ */
93
+ deleteCached(key: string): Promise<boolean>;
94
+ /**
95
+ * Clear all cached props
96
+ */
97
+ clearCache(): Promise<void>;
98
+ /**
99
+ * Get cache size
100
+ */
101
+ getCacheSize(): Promise<number>;
102
+ /**
103
+ * Create a cache key from schema and context
104
+ */
105
+ createCacheKey(schema: PropSchema, context?: Record<string, unknown>): Promise<string>;
106
+ /**
107
+ * Configure cache TTL
108
+ */
109
+ configureCache(ttl: number): Promise<void>;
110
+ /**
111
+ * Validate props against a schema
112
+ */
113
+ validate(props: Record<string, unknown>, schema: PropSchema): Promise<ValidationResult>;
114
+ /**
115
+ * Check if all required props are present
116
+ */
117
+ hasRequired(props: Record<string, unknown>, required: string[]): Promise<boolean>;
118
+ /**
119
+ * Get list of missing props according to schema
120
+ */
121
+ getMissing(props: Record<string, unknown>, schema: PropSchema): Promise<string[]>;
122
+ /**
123
+ * Check if props are complete according to schema
124
+ */
125
+ isComplete(props: Record<string, unknown>, schema: PropSchema): Promise<boolean>;
126
+ /**
127
+ * Sanitize props by removing unknown keys
128
+ */
129
+ sanitize<T extends Record<string, unknown>>(props: T, schema: PropSchema): Promise<Partial<T>>;
130
+ /**
131
+ * Merge props with default values
132
+ */
133
+ mergeDefaults<T extends Record<string, unknown>>(props: Partial<T>, defaults: Partial<T>, schema: PropSchema): Promise<Partial<T>>;
134
+ }
135
+ /**
136
+ * Create a typed HTTP RPC client for the ai-props worker
137
+ *
138
+ * This client communicates with the deployed ai-props worker over HTTP,
139
+ * sending JSON-RPC requests to the /rpc endpoint.
140
+ *
141
+ * @param config - Client configuration options
142
+ * @returns A typed RPC client with all PropsService methods
143
+ *
144
+ * @example Basic usage
145
+ * ```ts
146
+ * import { createPropsClient } from 'ai-props/client'
147
+ *
148
+ * const client = createPropsClient({
149
+ * url: 'https://ai-props.workers.dev',
150
+ * token: 'my-api-token'
151
+ * })
152
+ *
153
+ * // Generate props
154
+ * const result = await client.generate({
155
+ * schema: { title: 'Page title', description: 'Description' },
156
+ * context: { topic: 'Technology' }
157
+ * })
158
+ * console.log(result.props)
159
+ *
160
+ * // Validate props
161
+ * const validation = await client.validate(
162
+ * { title: 'Hello' },
163
+ * { title: 'string', description: 'string' }
164
+ * )
165
+ * console.log(validation.valid) // false - missing description
166
+ * ```
167
+ *
168
+ * @example With custom headers
169
+ * ```ts
170
+ * const client = createPropsClient({
171
+ * url: 'https://ai-props.workers.dev',
172
+ * headers: {
173
+ * 'X-Custom-Header': 'value',
174
+ * },
175
+ * timeout: 60000, // 60 seconds
176
+ * })
177
+ * ```
178
+ */
179
+ export declare function createPropsClient(config?: PropsHttpClientConfig): PropsClientAPI;
180
+ /**
181
+ * Create a typed client from a Cloudflare Service Binding
182
+ *
183
+ * Use this when you have a service binding to the ai-props worker
184
+ * in your wrangler.jsonc configuration.
185
+ *
186
+ * @param binding - The service binding from your env
187
+ * @returns A typed client interface
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * // wrangler.jsonc
192
+ * // {
193
+ * // "services": [
194
+ * // { "binding": "AI_PROPS", "service": "ai-props" }
195
+ * // ]
196
+ * // }
197
+ *
198
+ * import { createServiceClient } from 'ai-props/client'
199
+ * import type { PropsService } from 'ai-props/worker'
200
+ *
201
+ * interface Env {
202
+ * AI_PROPS: Service<PropsService>
203
+ * }
204
+ *
205
+ * export default {
206
+ * async fetch(request: Request, env: Env) {
207
+ * const client = createServiceClient(env.AI_PROPS)
208
+ * const result = await client.generate({
209
+ * schema: { title: 'Page title' }
210
+ * })
211
+ * return Response.json(result)
212
+ * }
213
+ * }
214
+ * ```
215
+ */
216
+ export declare function createServiceClient<T extends {
217
+ getService(): unknown;
218
+ }>(binding: T): ReturnType<T['getService']>;
219
+ /**
220
+ * Default client instance connected to the production ai-props worker
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * import client from 'ai-props/client'
225
+ *
226
+ * const result = await client.generate({
227
+ * schema: { title: 'Page title' }
228
+ * })
229
+ * ```
230
+ */
231
+ declare const client: PropsClientAPI;
232
+ export default client;
233
+ //# sourceMappingURL=client.d.ts.map
@@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AAE,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,EAAE,UAAU,GACjB,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;CACvB;AAOD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,GAAE,qBAA0B,GAAG,cAAc,CAyGpF;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS;IAAE,UAAU,IAAI,OAAO,CAAA;CAAE,EACrE,OAAO,EAAE,CAAC,GACT,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAE7B;AAID;;;;;;;;;;;GAWG;AACH,QAAA,MAAM,MAAM,EAAE,cAAoC,CAAA;AAElD,eAAe,MAAM,CAAA"}
package/dist/client.js ADDED
@@ -0,0 +1,191 @@
1
+ /**
2
+ * RPC Client for AI Props
3
+ *
4
+ * Provides a typed RPC client that connects to the deployed
5
+ * ai-props worker using rpc.do for remote procedure calls.
6
+ *
7
+ * This module is for client-side usage - it provides types and
8
+ * helpers for consuming the PropsService worker from other workers
9
+ * or client applications.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { createPropsClient } from 'ai-props/client'
14
+ *
15
+ * const client = createPropsClient('https://ai-props.workers.dev')
16
+ * const result = await client.generate({
17
+ * schema: { title: 'Page title', description: 'Page description' },
18
+ * context: { topic: 'AI' }
19
+ * })
20
+ * console.log(result.props)
21
+ * ```
22
+ *
23
+ * @packageDocumentation
24
+ */
25
+ export { isPropsRPCError, withRetry, calculateRetryDelay, DEFAULT_RETRY_CONFIG } from './rpc.js';
26
+ // ==================== HTTP Client Implementation ====================
27
+ /** Default URL for the ai-props worker */
28
+ const DEFAULT_URL = 'https://ai-props.do';
29
+ /**
30
+ * Create a typed HTTP RPC client for the ai-props worker
31
+ *
32
+ * This client communicates with the deployed ai-props worker over HTTP,
33
+ * sending JSON-RPC requests to the /rpc endpoint.
34
+ *
35
+ * @param config - Client configuration options
36
+ * @returns A typed RPC client with all PropsService methods
37
+ *
38
+ * @example Basic usage
39
+ * ```ts
40
+ * import { createPropsClient } from 'ai-props/client'
41
+ *
42
+ * const client = createPropsClient({
43
+ * url: 'https://ai-props.workers.dev',
44
+ * token: 'my-api-token'
45
+ * })
46
+ *
47
+ * // Generate props
48
+ * const result = await client.generate({
49
+ * schema: { title: 'Page title', description: 'Description' },
50
+ * context: { topic: 'Technology' }
51
+ * })
52
+ * console.log(result.props)
53
+ *
54
+ * // Validate props
55
+ * const validation = await client.validate(
56
+ * { title: 'Hello' },
57
+ * { title: 'string', description: 'string' }
58
+ * )
59
+ * console.log(validation.valid) // false - missing description
60
+ * ```
61
+ *
62
+ * @example With custom headers
63
+ * ```ts
64
+ * const client = createPropsClient({
65
+ * url: 'https://ai-props.workers.dev',
66
+ * headers: {
67
+ * 'X-Custom-Header': 'value',
68
+ * },
69
+ * timeout: 60000, // 60 seconds
70
+ * })
71
+ * ```
72
+ */
73
+ export function createPropsClient(config = {}) {
74
+ const url = config.url || DEFAULT_URL;
75
+ const timeout = config.timeout || 30000;
76
+ /**
77
+ * Make an RPC call to the remote service
78
+ */
79
+ async function rpc(method, args = []) {
80
+ const controller = new AbortController();
81
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
82
+ try {
83
+ const response = await fetch(`${url}/rpc`, {
84
+ method: 'POST',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ ...(config.token && { Authorization: `Bearer ${config.token}` }),
88
+ ...config.headers,
89
+ },
90
+ body: JSON.stringify({ method, args }),
91
+ signal: controller.signal,
92
+ });
93
+ if (!response.ok) {
94
+ const error = await response.json().catch(() => ({ error: 'Unknown error' }));
95
+ throw new Error(error.error || `RPC error: ${response.status}`);
96
+ }
97
+ const result = (await response.json());
98
+ if (result.error) {
99
+ throw new Error(result.error);
100
+ }
101
+ return result.result;
102
+ }
103
+ finally {
104
+ clearTimeout(timeoutId);
105
+ }
106
+ }
107
+ // Build client with typed methods
108
+ return {
109
+ // Generation
110
+ generate: (options) => rpc('generate', [options]),
111
+ getSync: (schema, context) => rpc('getSync', [schema, context]),
112
+ prefetch: (requests) => rpc('prefetch', [requests]),
113
+ generateMany: (requests) => rpc('generateMany', [requests]),
114
+ mergeWithGenerated: (schema, partialProps, options) => rpc('mergeWithGenerated', [schema, partialProps, options]),
115
+ // Configuration
116
+ configure: (cfg) => rpc('configure', [cfg]),
117
+ getConfig: () => rpc('getConfig'),
118
+ resetConfig: () => rpc('resetConfig'),
119
+ // Cache
120
+ getCached: (key) => rpc('getCached', [key]),
121
+ setCached: (key, props) => rpc('setCached', [key, props]),
122
+ deleteCached: (key) => rpc('deleteCached', [key]),
123
+ clearCache: () => rpc('clearCache'),
124
+ getCacheSize: () => rpc('getCacheSize'),
125
+ createCacheKey: (schema, context) => rpc('createCacheKey', [schema, context]),
126
+ configureCache: (ttl) => rpc('configureCache', [ttl]),
127
+ // Validation
128
+ validate: (props, schema) => rpc('validate', [props, schema]),
129
+ hasRequired: (props, required) => rpc('hasRequired', [props, required]),
130
+ getMissing: (props, schema) => rpc('getMissing', [props, schema]),
131
+ isComplete: (props, schema) => rpc('isComplete', [props, schema]),
132
+ sanitize: (props, schema) => rpc('sanitize', [props, schema]),
133
+ mergeDefaults: (props, defaults, schema) => rpc('mergeDefaults', [props, defaults, schema]),
134
+ };
135
+ }
136
+ // ==================== Service Binding Client ====================
137
+ /**
138
+ * Create a typed client from a Cloudflare Service Binding
139
+ *
140
+ * Use this when you have a service binding to the ai-props worker
141
+ * in your wrangler.jsonc configuration.
142
+ *
143
+ * @param binding - The service binding from your env
144
+ * @returns A typed client interface
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * // wrangler.jsonc
149
+ * // {
150
+ * // "services": [
151
+ * // { "binding": "AI_PROPS", "service": "ai-props" }
152
+ * // ]
153
+ * // }
154
+ *
155
+ * import { createServiceClient } from 'ai-props/client'
156
+ * import type { PropsService } from 'ai-props/worker'
157
+ *
158
+ * interface Env {
159
+ * AI_PROPS: Service<PropsService>
160
+ * }
161
+ *
162
+ * export default {
163
+ * async fetch(request: Request, env: Env) {
164
+ * const client = createServiceClient(env.AI_PROPS)
165
+ * const result = await client.generate({
166
+ * schema: { title: 'Page title' }
167
+ * })
168
+ * return Response.json(result)
169
+ * }
170
+ * }
171
+ * ```
172
+ */
173
+ export function createServiceClient(binding) {
174
+ return binding.getService();
175
+ }
176
+ // ==================== Default Export ====================
177
+ /**
178
+ * Default client instance connected to the production ai-props worker
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * import client from 'ai-props/client'
183
+ *
184
+ * const result = await client.generate({
185
+ * schema: { title: 'Page title' }
186
+ * })
187
+ * ```
188
+ */
189
+ const client = createPropsClient();
190
+ export default client;
191
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAkCH,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAsKhG,uEAAuE;AAEvE,0CAA0C;AAC1C,MAAM,WAAW,GAAG,qBAAqB,CAAA;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAAgC,EAAE;IAClE,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,IAAI,WAAW,CAAA;IACrC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,KAAK,CAAA;IAEvC;;OAEG;IACH,KAAK,UAAU,GAAG,CAAI,MAAc,EAAE,OAAkB,EAAE;QACxD,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QACxC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,E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