@agentshouse/mdruntime-adapter 0.1.0-cedb8e57837a7 → 0.1.0-cf059d0c8e9c0

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 (67) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +3 -68
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +1 -355
  5. package/dist/adapter.d.ts.map +1 -1
  6. package/dist/adapter.js +196 -410
  7. package/dist/app-reference.d.ts +2 -0
  8. package/dist/app-reference.d.ts.map +1 -0
  9. package/dist/app-reference.js +2 -0
  10. package/dist/crockford.d.ts +4 -0
  11. package/dist/crockford.d.ts.map +1 -0
  12. package/dist/crockford.js +3 -0
  13. package/dist/discovery.d.ts +12 -12
  14. package/dist/discovery.d.ts.map +1 -1
  15. package/dist/discovery.js +44 -51
  16. package/dist/history.d.ts +12 -0
  17. package/dist/history.d.ts.map +1 -0
  18. package/dist/history.js +91 -0
  19. package/dist/house-model.d.ts +2249 -0
  20. package/dist/house-model.d.ts.map +1 -0
  21. package/dist/house-model.js +1237 -0
  22. package/dist/index.d.ts +10 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +9 -1
  25. package/dist/model.d.ts +5 -2
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +10 -29
  28. package/dist/part.d.ts +14 -0
  29. package/dist/part.d.ts.map +1 -0
  30. package/dist/part.js +90 -0
  31. package/dist/read.d.ts +27 -59
  32. package/dist/read.d.ts.map +1 -1
  33. package/dist/read.js +167 -266
  34. package/dist/refusal.d.ts +0 -3
  35. package/dist/refusal.d.ts.map +1 -1
  36. package/dist/refusal.js +0 -5
  37. package/dist/room-stream.d.ts +19 -0
  38. package/dist/room-stream.d.ts.map +1 -0
  39. package/dist/room-stream.js +57 -0
  40. package/dist/room-types.d.ts +7 -0
  41. package/dist/room-types.d.ts.map +1 -0
  42. package/dist/room-types.js +126 -0
  43. package/dist/site.d.ts.map +1 -1
  44. package/dist/site.js +81 -193
  45. package/dist/stream.d.ts +13 -13
  46. package/dist/stream.d.ts.map +1 -1
  47. package/dist/stream.js +161 -409
  48. package/dist/transport.d.ts +49 -39
  49. package/dist/transport.d.ts.map +1 -1
  50. package/dist/transport.js +53 -11
  51. package/dist/upload.d.ts +12 -0
  52. package/dist/upload.d.ts.map +1 -0
  53. package/dist/upload.js +63 -0
  54. package/dist/write.d.ts +4 -0
  55. package/dist/write.d.ts.map +1 -1
  56. package/dist/write.js +22 -18
  57. package/package.json +8 -4
  58. package/AGENTS.md +0 -304
  59. package/dist/gather.d.ts +0 -4
  60. package/dist/gather.d.ts.map +0 -1
  61. package/dist/gather.js +0 -35
  62. package/dist/handed.d.ts +0 -15
  63. package/dist/handed.d.ts.map +0 -1
  64. package/dist/handed.js +0 -45
  65. package/dist/referrer.d.ts +0 -7
  66. package/dist/referrer.d.ts.map +0 -1
  67. package/dist/referrer.js +0 -35
package/README.md CHANGED
@@ -24,8 +24,8 @@ host.attach(port, 'my-app');
24
24
  ```
25
25
 
26
26
  App code never imports this package; it reaches the Runtime only through the
27
- client its host hands it. The rules an App's agent follows are in the
28
- package's `AGENTS.md`.
27
+ client its host hands it. The rules an App's agent follows are the `apps/` pages
28
+ of House's Guide, which House's `open_guide` opens.
29
29
 
30
30
  ## License
31
31
 
package/dist/actions.d.ts CHANGED
@@ -1,72 +1,7 @@
1
- import { type AcceptedMessage, type ActionDeclaration, type ActionRequest, type ActionResult, type ConversationAgents, type ConversationHistories, type ConversationHistoryRequest, type FileDeclaration } from '@agentshouse/mdruntime';
1
+ import type { ActionDeclaration, ActionRequest, ActionResult } from '@agentshouse/mdruntime';
2
2
  import type { HouseDoorAnswer, HouseTransport } from './transport.js';
3
- type Operated = {
4
- readonly operation_id?: string;
5
- };
6
- type ConversationControl = Operated & {
7
- readonly conversation: string;
8
- };
9
- type SentFile = FileDeclaration & {
10
- readonly blob: Blob;
11
- };
12
- export type AppInputs = {
13
- readonly 'conversation.answer': ConversationControl & {
14
- readonly answer: {
15
- readonly interaction_id: string;
16
- readonly response: unknown;
17
- };
18
- };
19
- readonly 'conversation.create': Operated & {
20
- readonly agent_id: string;
21
- };
22
- readonly 'conversation.delete': ConversationControl;
23
- readonly 'conversation.history': ConversationHistoryRequest;
24
- readonly 'conversation.interrupt': ConversationControl;
25
- readonly 'conversation.kill': ConversationControl;
26
- readonly 'conversation.send': ConversationControl & {
27
- readonly operation_id: string;
28
- readonly text?: string;
29
- readonly files?: readonly SentFile[];
30
- };
31
- readonly 'trigger.list': {
32
- readonly room_refs: readonly string[];
33
- };
34
- };
35
- export type AppAnswers = {
36
- readonly 'conversation.history': ConversationHistories;
37
- };
38
- export type AppAnswerAdditions = {
39
- readonly 'conversation.create': {
40
- readonly conversation: string;
41
- };
42
- readonly 'conversation.send': {
43
- readonly turn: string;
44
- };
45
- readonly 'record.list': {
46
- readonly attachments: readonly {
47
- readonly referrer: {
48
- readonly room_ref: string;
49
- readonly record: string;
50
- };
51
- }[];
52
- readonly records: readonly {
53
- readonly attachments: readonly {
54
- readonly referrer: {
55
- readonly room_ref: string;
56
- readonly record: string;
57
- };
58
- }[];
59
- }[];
60
- };
61
- };
62
- export declare const HOUSE_APP_ACTS: Readonly<Record<string, string>>;
63
- export declare const SITE_SUBMIT = "submit";
64
- export declare const HOUSE_SITE_ACTS: Readonly<Record<string, string>>;
65
- export declare const HOUSE_APP_BATCHES: Readonly<Partial<Record<string, readonly string[]>>>;
66
- export declare function houseConversationAgents(locator: string): ConversationAgents;
3
+ export type HouseActDoor = Pick<HouseTransport, 'door'>;
67
4
  export declare function declaredActions(routes: readonly string[]): readonly ActionDeclaration[];
68
- export declare function houseAction(transport: HouseTransport, request: ActionRequest): Promise<ActionResult>;
5
+ export declare function houseAction(transport: HouseActDoor, request: ActionRequest): Promise<ActionResult>;
69
6
  export declare function actionOutcome(answer: HouseDoorAnswer, name: string): ActionResult;
70
- export declare function acceptedMessage(entry: unknown): AcceptedMessage | null;
71
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@@
1
- import { changeBatchIdentity, } from '@agentshouse/mdruntime';
2
1
  import { doorRefusal } from './refusal.js';
3
- const HISTORY_PAGE = 100;
4
- const HISTORY_CONVERSATIONS = 64;
5
- const TRIGGER_ROOMS = 100;
6
- const OCTET_STREAM = 'application/octet-stream';
7
- function mapped(door, input = (given) => given) {
8
- return {
9
- door,
10
- async carry(transport, given, name) {
11
- return actionOutcome(await transport.door(`/door/${door}`, input(given)), name);
12
- },
13
- };
14
- }
15
- function carried(door, carry, batched, room) {
16
- return {
17
- door,
18
- ...(batched === undefined ? {} : { batched }),
19
- ...(room === undefined ? {} : { room }),
20
- carry: (transport, input, name) => carry(transport, `/door/${door}`, input, name),
21
- };
22
- }
23
- const APP_ACTS = {
24
- 'access.read': mapped('rooms/access/read'),
25
- 'conversation.answer': mapped('conversations/interaction/answer', answerBody),
26
- 'conversation.create': carried('conversations/create', createAction),
27
- 'conversation.delete': mapped('conversations/delete', controlBody),
28
- 'conversation.history': carried('conversations/history', historyAction, ['conversations']),
29
- 'conversation.interrupt': mapped('conversations/interrupt', controlBody),
30
- 'conversation.kill': mapped('conversations/kill', controlBody),
31
- 'conversation.markDone': mapped('conversations/done'),
32
- 'conversation.rename': mapped('conversations/rename'),
33
- 'conversation.retry': mapped('conversations/retry'),
34
- 'conversation.send': carried('conversations/send', sendAction),
35
- 'conversation.setOption': mapped('conversations/option'),
36
- 'feature.list': mapped('apps/features/read'),
37
- 'holder.list': mapped('apps/holders'),
38
- 'holder.ring': mapped('apps/ring'),
39
- 'member.list': mapped('rooms/members'),
40
- 'message.send': mapped('apps/messages/send'),
41
- 'processing.read': mapped('rooms/settings/processing/read'),
42
- 'record.list': carried('records/read', recordsAction),
43
- 'relay.read': mapped('apps/relay'),
44
- 'routine.list': mapped('routines/access'),
45
- 'routineAccess.list': mapped('routines/granted'),
46
- 'routineAllowance.list': mapped('routines/allowance'),
47
- 'routineRequest.list': mapped('routines/requests'),
48
- 'storage.read': mapped('storage/inventory'),
49
- 'trigger.list': carried('triggers/read', triggersAction, ['room_refs'], 'room_refs'),
50
- 'triggerOccurrence.list': mapped('triggers/fired'),
51
- };
52
- export const HOUSE_APP_ACTS = Object.fromEntries(Object.entries(APP_ACTS).map(([name, act]) => [name, act.door]));
53
- export const SITE_SUBMIT = 'submit';
54
- export const HOUSE_SITE_ACTS = {
55
- [SITE_SUBMIT]: 'site/submission',
56
- };
57
- export const HOUSE_APP_BATCHES = Object.fromEntries(Object.entries(APP_ACTS).flatMap(([name, act]) => act.batched === undefined ? [] : [[name, act.batched]]));
58
- const APP_NAMES = new Map(Object.entries(APP_ACTS).map(([name, act]) => [act.door, name]));
59
- const APP_ROOMS = new Map(Object.entries(APP_ACTS).flatMap(([name, act]) => act.room === undefined ? [] : [[name, act.room]]));
60
- export function houseConversationAgents(locator) {
61
- return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
62
- }
63
2
  export function declaredActions(routes) {
64
- return routes.map((route) => {
65
- const name = APP_NAMES.get(route) ?? route;
66
- return { name, inputShape: {}, resultKind: 'house', room: APP_ROOMS.get(name) ?? 'room_ref' };
67
- });
3
+ return routes.map((name) => ({ name, inputShape: {}, resultKind: 'house', room: 'room_ref' }));
68
4
  }
69
5
  export function houseAction(transport, request) {
70
- if (Object.hasOwn(APP_ACTS, request.name)) {
71
- return APP_ACTS[request.name].carry(transport, request.input, request.name);
72
- }
73
- if (APP_NAMES.has(request.name))
74
- return Promise.resolve(unimplemented(request.name));
75
6
  const route = routeOf(request.name);
76
7
  if (route === null)
77
8
  return Promise.resolve(malformed());
@@ -88,57 +19,12 @@ function routeOf(name) {
88
19
  return null;
89
20
  }
90
21
  }
91
- function record(value) {
92
- return typeof value === 'object' && value !== null ? value : {};
93
- }
94
- function text(value, name) {
95
- const held = record(value)[name];
96
- return typeof held === 'string' ? held : undefined;
97
- }
98
- function operationId(input) {
99
- return text(input, 'operation_id') ?? changeBatchIdentity();
100
- }
101
22
  function malformed() {
102
23
  return { kind: 'rejected', code: 'invalid_request', fields: {}, retryable: false };
103
24
  }
104
25
  function unimplemented(name) {
105
26
  return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
106
27
  }
107
- function createBody(input) {
108
- return { agent_id: text(input, 'agent_id'), operation_id: operationId(input) };
109
- }
110
- function controlBody(input) {
111
- return { conversation_id: text(input, 'conversation'), operation_id: operationId(input) };
112
- }
113
- function answerBody(input) {
114
- const nested = record(record(input).answer);
115
- return {
116
- conversation_id: text(input, 'conversation'),
117
- operation_id: operationId(input),
118
- interaction_id: text(nested, 'interaction_id'),
119
- response: nested.response,
120
- };
121
- }
122
- async function createAction(transport, route, input, name) {
123
- const outcome = actionOutcome(await transport.door(route, createBody(input)), name);
124
- if (outcome.kind !== 'action')
125
- return outcome;
126
- const held = record(outcome.result);
127
- const conversation = text(held, 'conversation_id');
128
- return conversation === undefined
129
- ? outcome
130
- : {
131
- ...outcome,
132
- result: { ...held, conversation },
133
- };
134
- }
135
- function withTurn(body) {
136
- const held = record(body);
137
- const turn = text(held, 'message_id');
138
- return turn === undefined
139
- ? body
140
- : { ...held, turn };
141
- }
142
28
  export function actionOutcome(answer, name) {
143
29
  if (answer.status === 401)
144
30
  return { kind: 'revoked-authorization' };
@@ -153,246 +39,6 @@ export function actionOutcome(answer, name) {
153
39
  return { kind: 'transport-uncertainty' };
154
40
  return { kind: 'action', result: answer.body };
155
41
  }
156
- function referredRecord(view, roomRef) {
157
- const held = record(view);
158
- if (roomRef === undefined || typeof held.record !== 'string' || !Array.isArray(held.attachments))
159
- return view;
160
- const referrer = {
161
- room_ref: roomRef,
162
- record: held.record,
163
- };
164
- return {
165
- ...held,
166
- attachments: held.attachments.map((attachment) => ({ ...record(attachment), referrer })),
167
- };
168
- }
169
- async function recordsAction(transport, route, input, name) {
170
- const outcome = actionOutcome(await transport.door(route, input), name);
171
- if (outcome.kind !== 'action')
172
- return outcome;
173
- const roomRef = text(input, 'room_ref');
174
- const read = record(outcome.result);
175
- return {
176
- ...outcome,
177
- result: Array.isArray(read.records)
178
- ? { ...read, records: read.records.map((view) => referredRecord(view, roomRef)) }
179
- : referredRecord(outcome.result, roomRef),
180
- };
181
- }
182
42
  async function carry(transport, route, name, input) {
183
43
  return actionOutcome(await transport.door(route, input), name);
184
44
  }
185
- function sendFile(entry) {
186
- const file = record(entry);
187
- const name = text(file, 'name');
188
- const bytes = file.byteLength;
189
- const sha256 = text(file, 'contentHash');
190
- if (name === undefined || typeof bytes !== 'number' || sha256 === undefined)
191
- return null;
192
- const mediaType = text(file, 'mediaType');
193
- const width = file.width;
194
- const height = file.height;
195
- return {
196
- name,
197
- media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
198
- bytes,
199
- sha256,
200
- ...(typeof width === 'number' && typeof height === 'number'
201
- ? { dimensions: { width, height } }
202
- : {}),
203
- };
204
- }
205
- function sendFiles(input) {
206
- const files = record(input).files;
207
- if (files === undefined)
208
- return [];
209
- if (!Array.isArray(files))
210
- return null;
211
- const declared = [];
212
- for (const entry of files) {
213
- const file = sendFile(entry);
214
- if (file === null)
215
- return null;
216
- declared.push(file);
217
- }
218
- return declared;
219
- }
220
- function sendBody(input) {
221
- const files = sendFiles(input);
222
- const operation = text(input, 'operation_id');
223
- if (files === null || operation === undefined)
224
- return null;
225
- const textValue = text(input, 'text');
226
- return {
227
- conversation_id: text(input, 'conversation'),
228
- operation_id: operation,
229
- ...(textValue !== undefined && textValue.length > 0 ? { text: textValue } : {}),
230
- ...(files.length === 0 ? {} : { files }),
231
- };
232
- }
233
- async function fileBytes(file) {
234
- const blob = record(file).blob;
235
- if (blob === undefined || typeof blob !== 'object' || blob === null)
236
- return null;
237
- if (!('arrayBuffer' in blob) || typeof blob.arrayBuffer !== 'function')
238
- return null;
239
- return new Uint8Array(await blob.arrayBuffer());
240
- }
241
- function uploadGrant(entry) {
242
- if (!('upload' in record(entry)))
243
- return 'absent';
244
- const upload = record(record(entry).upload);
245
- if (upload.method !== 'POST')
246
- return null;
247
- if (typeof upload.operation !== 'string' || typeof upload.url !== 'string')
248
- return null;
249
- return { method: 'POST', operation: upload.operation, url: upload.url };
250
- }
251
- async function uploadFiles(transport, input, result) {
252
- const sent = record(result).files;
253
- if (!Array.isArray(sent))
254
- return true;
255
- const files = Array.isArray(record(input).files) ? record(input).files : [];
256
- for (const [index, entry] of sent.entries()) {
257
- const grant = uploadGrant(entry);
258
- if (grant === 'absent')
259
- continue;
260
- const bytes = index < files.length ? await fileBytes(files[index]) : null;
261
- if (grant === null || bytes === null)
262
- return false;
263
- const answer = await transport.transfer({
264
- act: 'conversation_file_upload',
265
- url: grant.url,
266
- body: bytes,
267
- operation: grant.operation,
268
- });
269
- if (answer.status !== 200)
270
- return false;
271
- }
272
- return true;
273
- }
274
- async function sendAction(transport, route, input, name) {
275
- const body = sendBody(input);
276
- if (body === null)
277
- return malformed();
278
- const outcome = actionOutcome(await transport.door(route, body), name);
279
- if (outcome.kind !== 'action')
280
- return outcome;
281
- if (!(await uploadFiles(transport, input, outcome.result))) {
282
- return { kind: 'transport-uncertainty' };
283
- }
284
- return { ...outcome, result: withTurn(outcome.result) };
285
- }
286
- function historyCursor(value) {
287
- const acceptedAt = text(value, 'accepted_at');
288
- const messageId = text(value, 'message_id');
289
- if (acceptedAt === undefined || messageId === undefined)
290
- return null;
291
- return { accepted_at: acceptedAt, message_id: messageId };
292
- }
293
- function position(cursor) {
294
- return JSON.stringify({ accepted_at: cursor.accepted_at, message_id: cursor.message_id });
295
- }
296
- function positionCursor(value) {
297
- try {
298
- return historyCursor(JSON.parse(value));
299
- }
300
- catch {
301
- return null;
302
- }
303
- }
304
- export function acceptedMessage(entry) {
305
- const cursor = historyCursor(entry);
306
- if (cursor === null)
307
- return null;
308
- const message = structuredClone(record(entry));
309
- const referrer = { message: cursor.message_id };
310
- return {
311
- identity: cursor.message_id,
312
- turn: text(entry, 'turn_id') ?? cursor.message_id,
313
- position: position(cursor),
314
- text: text(entry, 'text') ?? '',
315
- message: Array.isArray(message.files)
316
- ? {
317
- ...message,
318
- files: message.files.map((file) => ({ ...record(file), referrer })),
319
- }
320
- : message,
321
- };
322
- }
323
- function historyPage(entry) {
324
- const held = record(entry);
325
- const conversation = text(held, 'conversation_id');
326
- if (conversation === undefined || !Array.isArray(held.messages))
327
- return null;
328
- const messages = [];
329
- for (const one of held.messages) {
330
- const message = acceptedMessage(one);
331
- if (message === null)
332
- return null;
333
- messages.push(message);
334
- }
335
- const next = held.next === null || held.next === undefined ? undefined : historyCursor(held.next);
336
- if (next === null)
337
- return null;
338
- return { conversation, messages, ...(next === undefined ? {} : { next: position(next) }) };
339
- }
340
- function historyAsks(input) {
341
- const asked = record(input).conversations;
342
- if (!Array.isArray(asked) || asked.length === 0)
343
- return null;
344
- const asks = [];
345
- for (const entry of asked) {
346
- const conversationId = text(entry, 'conversation');
347
- if (conversationId === undefined)
348
- return null;
349
- const afterValue = text(entry, 'after');
350
- const after = afterValue === undefined ? undefined : positionCursor(afterValue);
351
- if (after === null)
352
- return null;
353
- asks.push({ conversation_id: conversationId, ...(after === undefined ? {} : { after }) });
354
- }
355
- return asks;
356
- }
357
- async function triggersAction(transport, route, input, name) {
358
- const asked = record(input).room_refs;
359
- if (!Array.isArray(asked) || asked.length === 0)
360
- return malformed();
361
- const rooms = [];
362
- for (let at = 0; at < asked.length; at += TRIGGER_ROOMS) {
363
- const outcome = actionOutcome(await transport.door(route, { room_refs: asked.slice(at, at + TRIGGER_ROOMS) }), name);
364
- if (outcome.kind !== 'action')
365
- return outcome;
366
- const answered = record(outcome.result).rooms;
367
- if (!Array.isArray(answered))
368
- return { kind: 'transport-uncertainty' };
369
- rooms.push(...answered);
370
- }
371
- return { kind: 'action', result: { rooms } };
372
- }
373
- async function historyAction(transport, route, input, name) {
374
- const asks = historyAsks(input);
375
- if (asks === null)
376
- return malformed();
377
- const histories = [];
378
- for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
379
- const answer = await transport.door(route, {
380
- conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
381
- limit: HISTORY_PAGE,
382
- });
383
- const outcome = actionOutcome(answer, name);
384
- if (outcome.kind !== 'action')
385
- return outcome;
386
- const answered = record(outcome.result).histories;
387
- if (!Array.isArray(answered))
388
- return { kind: 'transport-uncertainty' };
389
- for (const entry of answered) {
390
- const page = historyPage(entry);
391
- if (page === null)
392
- return { kind: 'transport-uncertainty' };
393
- histories.push(page);
394
- }
395
- }
396
- const result = { histories };
397
- return { kind: 'action', result };
398
- }
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EACP,aAAa,EAmBd,MAAM,wBAAwB,CAAC;AA6ChC,OAAO,EASL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AA0BxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAyFrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA8mBpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAcd,MAAM,wBAAwB,CAAC;AA+BhC,OAAO,EAGL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAmBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AA6DrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAyZpF"}