@zimtsui/typechat 0.0.137 → 0.0.138

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zimtsui/typechat",
3
- "version": "0.0.137",
3
+ "version": "0.0.138",
4
4
  "exports": {
5
5
  ".": "./build/exports.js",
6
6
  "./xml": "./build/exports/xml.js",
@@ -1,28 +0,0 @@
1
- import { Engine } from '../../engine.ts';
2
- import { Function } from '../../function.ts';
3
- import OpenAI from 'openai';
4
- import { ToolCodec } from '../openai-responses/tool-codec.ts';
5
- import { Media } from '../../media.ts';
6
- import { Text } from '../../text.ts';
7
- export declare class MessageCodec<in out fdm extends Function.Decl.Map.Proto> {
8
- protected cacheDeveloperMessages: WeakMap<Engine.Message.Developer, string>;
9
- protected cacheInputMessages: WeakMap<Engine.Message.Input<Function.Decl.Proto>, OpenAI.Responses.ResponseInput>;
10
- protected cacheOutputMessages: WeakMap<Engine.Message.Output<Function.Decl.Proto>, OpenAI.Responses.ResponseInput>;
11
- protected cacheResponseIds: WeakMap<Engine.Message.Output<Function.Decl.Proto>, string>;
12
- protected toolCodec: ToolCodec<fdm>;
13
- protected messageValidator: Engine.MessageValidator.From<fdm>;
14
- constructor(options: MessageCodec.Options<fdm>);
15
- decodeOutputMessage(raw: OpenAI.Responses.Response): Engine.Message.Output.From<fdm>;
16
- encodeUserMessagePart(part: Text | Media): OpenAI.Responses.ResponseInputContent;
17
- encodeInputMessage(inm: Engine.Message.Input.From<fdm>): OpenAI.Responses.ResponseInput;
18
- encodeOutputMessage(outm: Engine.Message.Output.From<fdm>): OpenAI.Responses.ResponseInput;
19
- getResponseId(outm: Engine.Message.Output.From<fdm>): string | undefined;
20
- encodeDeveloperMessage(developerMessage: Engine.Message.Developer): string;
21
- encodeChatMessage(chatMessage: Engine.Session.ChatMessage.From<fdm>): OpenAI.Responses.ResponseInput;
22
- }
23
- export declare namespace MessageCodec {
24
- interface Options<in out fdm extends Function.Decl.Map.Proto> {
25
- toolCodec: ToolCodec<fdm>;
26
- messageValidator: Engine.MessageValidator.From<fdm>;
27
- }
28
- }
@@ -1,112 +0,0 @@
1
- import { Engine } from "../../engine.js";
2
- import { Function } from "../../function.js";
3
- import { Media } from "../../media.js";
4
- import { Text } from "../../text.js";
5
- export class MessageCodec {
6
- cacheDeveloperMessages = new WeakMap();
7
- cacheInputMessages = new WeakMap();
8
- cacheOutputMessages = new WeakMap();
9
- cacheResponseIds = new WeakMap();
10
- toolCodec;
11
- messageValidator;
12
- constructor(options) {
13
- this.toolCodec = options.toolCodec;
14
- this.messageValidator = options.messageValidator;
15
- }
16
- decodeOutputMessage(raw) {
17
- const parts = [];
18
- for (const item of raw.output) {
19
- if (item.type === 'message')
20
- for (const part of item.content)
21
- if (part.type === 'output_text')
22
- parts.push(new Text(part.text));
23
- else if (part.type === 'refusal')
24
- throw new Engine.Exceptions.InferenceError('Refusal', { cause: raw });
25
- else
26
- throw new Error('Unsupported API output message part.', { cause: part });
27
- else if (item.type === 'function_call')
28
- parts.push(this.toolCodec.decodeFunctionCall(item));
29
- else if (item.type === 'reasoning') { }
30
- else
31
- throw new Error('Unsupported API output item.', { cause: item });
32
- }
33
- const outm = new Engine.Message.Output(parts);
34
- this.messageValidator.validateOutputMessage(outm);
35
- if (raw.output.every(item => item.type !== 'computer_call_output' && item.type !== 'additional_tools')) { }
36
- else
37
- throw new Error('Computer calls are not supported yet.');
38
- this.cacheOutputMessages.set(outm, raw.output);
39
- this.cacheResponseIds.set(outm, raw.id);
40
- return outm;
41
- }
42
- encodeUserMessagePart(part) {
43
- if (part instanceof Text)
44
- return {
45
- type: 'input_text',
46
- text: part.raw,
47
- };
48
- else if (part instanceof Media.Text)
49
- return {
50
- type: 'input_text',
51
- text: part.quote(),
52
- };
53
- else if (part instanceof Media.Image)
54
- return {
55
- type: 'input_image',
56
- image_url: `data:${part.mimeType.essence};base64,${part}`,
57
- detail: 'auto',
58
- };
59
- else
60
- throw new Error('Unsupported user message part.', { cause: part });
61
- }
62
- encodeInputMessage(inm) {
63
- if (this.cacheInputMessages.has(inm))
64
- return this.cacheInputMessages.get(inm);
65
- this.messageValidator.validateInputMessage(inm);
66
- const responseInput = [];
67
- const content = [];
68
- for (const part of inm.parts)
69
- if (part instanceof Function.Response) {
70
- const fr = part;
71
- responseInput.push(this.toolCodec.encodeFunctionResponse(fr));
72
- }
73
- else
74
- content.push(this.encodeUserMessagePart(part));
75
- if (content.length)
76
- responseInput.push({
77
- type: 'message',
78
- role: 'user',
79
- content,
80
- });
81
- this.cacheInputMessages.set(inm, responseInput);
82
- return responseInput;
83
- }
84
- encodeOutputMessage(outm) {
85
- if (this.cacheOutputMessages.has(outm))
86
- return this.cacheOutputMessages.get(outm);
87
- throw new Error('Only native output message allowed.', { cause: outm });
88
- }
89
- getResponseId(outm) {
90
- return this.cacheResponseIds.get(outm);
91
- }
92
- encodeDeveloperMessage(developerMessage) {
93
- if (this.cacheDeveloperMessages.has(developerMessage))
94
- return this.cacheDeveloperMessages.get(developerMessage);
95
- const raw = developerMessage.getOnlyTextParts().map(part => part.raw).join('');
96
- this.cacheDeveloperMessages.set(developerMessage, raw);
97
- return raw;
98
- }
99
- encodeChatMessage(chatMessage) {
100
- if (chatMessage instanceof Engine.Message.Input) {
101
- const inm = chatMessage;
102
- return this.encodeInputMessage(inm);
103
- }
104
- else if (chatMessage instanceof Engine.Message.Output) {
105
- const outm = chatMessage;
106
- return this.encodeOutputMessage(outm);
107
- }
108
- else
109
- throw new Error();
110
- }
111
- }
112
- //# sourceMappingURL=message-codec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-codec.js","sourceRoot":"","sources":["../../../src/engines/openai-compatible/message-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAG7C,OAAO,EAAE,KAAK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AErC,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;IAEM,sBAAsB,CAAC,gBAA0C;QACpE,IAAI,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,gBAAgB,CAAC;YAAE,OAAO,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,gBAAgB,CAAE,CAAC;QACjH,MAAM,GAAG,GAAG,gBAAgB,CAAC,gBAAgB,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC/E,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;QACvD,OAAO,GAAG,CAAC;IACf,CAAC;IAEM,iBAAiB,CACpB,WAAiD;QAEjD,IAAI,WAAW,YAAY,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YAC9C,MAAM,GAAG,GAAG,WAA6C,CAAC;YAC1D,OAAO,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,WAAW,YAAY,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACtD,MAAM,IAAI,GAAG,WAA8C,CAAC;YAC5D,OAAO,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC;QAC1C,CAAC;;YACI,MAAM,IAAI,KAAK,EAAE,CAAC;IAC3B,CAAC;CACJ"}
@@ -1,3 +0,0 @@
1
- import { ToolChoice } from '../../tool-choice.ts';
2
- import type { OpenAI } from 'openai';
3
- export declare function encode(toolChoice: ToolChoice): OpenAI.Responses.ToolChoiceOptions | OpenAI.Responses.ToolChoiceAllowed;
@@ -1,14 +0,0 @@
1
- import { ToolChoice } from "../../tool-choice.js";
2
- export function encode(toolChoice) {
3
- if (toolChoice === ToolChoice.NONE)
4
- return 'none';
5
- else if (toolChoice === ToolChoice.REQUIRED)
6
- return 'auto';
7
- else if (toolChoice === ToolChoice.ANYONE)
8
- return 'auto';
9
- else if (toolChoice === ToolChoice.AUTO)
10
- return 'auto';
11
- else
12
- throw new Error();
13
- }
14
- //# sourceMappingURL=tool-choice-codec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tool-choice-codec.js","sourceRoot":"","sources":["../../../src/engines/openai-compatible/tool-choice-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAIlD,MAAM,UAAU,MAAM,CAClB,UAAsB;IAEtB,IAAI,UAAU,KAAK,UAAU,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC;SAC7C,IAAI,UAAU,KAAK,UAAU,CAAC,QAAQ;QAAE,OAAO,MAAM,CAAC;SACtD,IAAI,UAAU,KAAK,UAAU,CAAC,MAAM;QAAE,OAAO,MAAM,CAAC;SACpD,IAAI,UAAU,KAAK,UAAU,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC;;QAClD,MAAM,IAAI,KAAK,EAAE,CAAC;AAC3B,CAAC"}
@@ -1,36 +0,0 @@
1
- import { type InferenceOptions, type ProviderSpecs, Engine } from '../../engine.ts';
2
- import { Function } from '../../function.ts';
3
- import OpenAI from 'openai';
4
- import { type InferenceContext } from '../../inference-context.ts';
5
- import { Throttle } from '../../throttle.ts';
6
- import type { MessageCodec } from './message-codec.ts';
7
- import type { ToolCodec } from '../openai-responses/tool-codec.ts';
8
- import type { Billing } from '../openai-responses/billing.ts';
9
- import { ToolChoice } from '../../tool-choice.ts';
10
- export declare class Transport<in out fdm extends Function.Decl.Map.Proto> implements Engine.Transport<fdm> {
11
- protected client: OpenAI;
12
- protected inferenceParams: InferenceOptions;
13
- protected providerSpec: ProviderSpecs;
14
- protected fdm: fdm;
15
- protected throttle: Throttle;
16
- protected toolChoice: ToolChoice;
17
- protected messageCodec: MessageCodec<fdm>;
18
- protected toolCodec: ToolCodec<fdm>;
19
- protected billing: Billing;
20
- constructor(options: Transport.Options<fdm>);
21
- protected makeParams(session: Engine.Session.From<fdm>): OpenAI.Responses.ResponseCreateParamsStreaming;
22
- protected logAiMessage(output: OpenAI.Responses.ResponseOutputItem[]): void;
23
- fetch(wfctx: InferenceContext, session: Engine.Session.From<fdm>, signal?: AbortSignal): Promise<Engine.Message.Output.From<fdm>>;
24
- }
25
- export declare namespace Transport {
26
- interface Options<in out fdm extends Function.Decl.Map.Proto> {
27
- inferenceParams: InferenceOptions;
28
- providerSpec: ProviderSpecs;
29
- fdm: fdm;
30
- throttle: Throttle;
31
- toolChoice: ToolChoice;
32
- messageCodec: MessageCodec<fdm>;
33
- toolCodec: ToolCodec<fdm>;
34
- billing: Billing;
35
- }
36
- }
@@ -1,121 +0,0 @@
1
- import { Engine } from "../../engine.js";
2
- import OpenAI from 'openai';
3
- import { loggers } from "../../telemetry.js";
4
- import * as ToolChoiceCodec from "./tool-choice-codec.js";
5
- import * as Undici from 'undici';
6
- import assert from 'node:assert';
7
- export class Transport {
8
- client;
9
- inferenceParams;
10
- providerSpec;
11
- fdm;
12
- throttle;
13
- toolChoice;
14
- messageCodec;
15
- toolCodec;
16
- billing;
17
- constructor(options) {
18
- this.client = new OpenAI({
19
- baseURL: options.providerSpec.baseUrl,
20
- apiKey: options.providerSpec.apiKey,
21
- fetch: Undici.fetch,
22
- fetchOptions: { dispatcher: options.providerSpec.dispatcher },
23
- defaultHeaders: new Headers(options.inferenceParams.additionalHeaders),
24
- });
25
- this.inferenceParams = options.inferenceParams;
26
- this.providerSpec = options.providerSpec;
27
- this.fdm = options.fdm;
28
- this.throttle = options.throttle;
29
- this.toolChoice = options.toolChoice;
30
- this.messageCodec = options.messageCodec;
31
- this.toolCodec = options.toolCodec;
32
- this.billing = options.billing;
33
- }
34
- makeParams(session) {
35
- const tools = this.toolCodec.encodeFunctionDeclarationMap();
36
- let input;
37
- let previous_response_id = undefined;
38
- let instructions = undefined;
39
- const lastSecondMessage = session.chatMessages.at(-2);
40
- if (lastSecondMessage instanceof Engine.Message.Output && this.messageCodec.getResponseId(lastSecondMessage)) {
41
- const lastMessage = session.chatMessages.at(-1);
42
- assert(lastMessage instanceof Engine.Message.Input);
43
- input = this.messageCodec.encodeInputMessage(lastMessage);
44
- previous_response_id = this.messageCodec.getResponseId(lastSecondMessage);
45
- }
46
- else {
47
- input = session.chatMessages.flatMap(chatMessage => this.messageCodec.encodeChatMessage(chatMessage));
48
- instructions = session.developerMessage && this.messageCodec.encodeDeveloperMessage(session.developerMessage);
49
- }
50
- return {
51
- model: this.inferenceParams.model,
52
- include: ['reasoning.encrypted_content'],
53
- store: true,
54
- stream: true,
55
- input,
56
- previous_response_id,
57
- instructions,
58
- tools: tools.length ? tools : undefined,
59
- tool_choice: tools.length ? ToolChoiceCodec.encode(this.toolChoice) : undefined,
60
- parallel_tool_calls: tools.length ? this.inferenceParams.parallelToolCall : undefined,
61
- ...this.inferenceParams.additionalOptions,
62
- };
63
- }
64
- logAiMessage(output) {
65
- for (const item of output)
66
- if (item.type === 'message') {
67
- if (item.content.every(part => part.type === 'output_text')) { }
68
- else
69
- throw new Engine.Exceptions.InferenceError('Refusal', { cause: output });
70
- loggers.inference.info(item.content.map(part => part.text).join(''));
71
- }
72
- else if (item.type === 'function_call')
73
- loggers.message.info(item);
74
- else if (item.type === 'reasoning') { }
75
- else
76
- loggers.message.info(item);
77
- }
78
- async fetch(wfctx, session, signal) {
79
- await this.throttle.requests(wfctx);
80
- const params = this.makeParams(session);
81
- loggers.message.debug(params);
82
- let response = null;
83
- try {
84
- const stream = await this.client.responses.create(params, { signal });
85
- for await (const event of stream) {
86
- loggers.stream.trace(event);
87
- if (event.type === 'response.completed')
88
- response = event.response;
89
- else if (event.type === 'response.incomplete')
90
- response = event.response;
91
- else if (event.type === 'response.failed')
92
- response = event.response;
93
- else if (event.type === 'error')
94
- throw new Engine.Exceptions.InferenceError('Response stream error', { cause: event });
95
- }
96
- }
97
- catch (e) {
98
- if (e instanceof OpenAI.APIError)
99
- throw new Engine.Exceptions.APIError(undefined, { cause: e });
100
- else if (e instanceof TypeError)
101
- throw new Engine.Exceptions.APIError(undefined, { cause: e });
102
- else
103
- throw e;
104
- }
105
- if (response) { }
106
- else
107
- throw new Engine.Exceptions.APIError('Stream shut down');
108
- loggers.message.debug(response);
109
- if (response.status === 'completed') { }
110
- else
111
- throw new Engine.Exceptions.InferenceError('Abnormal response status', { cause: response });
112
- if (response.usage) { }
113
- else
114
- throw new Error();
115
- this.logAiMessage(response.output);
116
- wfctx.cost?.(this.billing.charge(response.usage));
117
- loggers.message.info(response.usage);
118
- return this.messageCodec.decodeOutputMessage(response);
119
- }
120
- }
121
- //# sourceMappingURL=transport.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../src/engines/openai-compatible/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEpF,OAAO,MAA8B,MAAM,QAAQ,CAAC;AAGpD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAI7C,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAC;AAE1D,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,MAAM,MAAM,aAAa,CAAC;AAIjC,MAAM,OAAO,SAAS;IAGR,MAAM,CAAS;IACf,eAAe,CAAmB;IAClC,YAAY,CAAgB;IAC5B,GAAG,CAAM;IACT,QAAQ,CAAW;IACnB,UAAU,CAAa;IACvB,YAAY,CAAoB;IAChC,SAAS,CAAiB;IAC1B,OAAO,CAAU;IAE3B,YAAmB,OAA+B;QAC9C,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACrB,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,OAAO;YACrC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,MAAM;YACnC,KAAK,EAAE,MAAM,CAAC,KAAgC;YAC9C,YAAY,EAAE,EAAE,UAAU,EAAE,OAA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,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QAClD,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAErC,OAAO,IAAI,CAAC,YAAY,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC3D,CAAC;CACJ"}
@@ -1,19 +0,0 @@
1
- import { Engine } from '../engine.ts';
2
- import { Function } from '../function.ts';
3
- import { MessageCodec } from './openai-compatible/message-codec.ts';
4
- import { ToolCodec } from './openai-responses/tool-codec.ts';
5
- import { Billing } from './openai-responses/billing.ts';
6
- import * as TransportModule from './openai-compatible/transport.ts';
7
- export type OpenAICompatibleEngine<fdm extends Function.Decl.Map.Proto> = OpenAICompatibleEngine.Instance<fdm>;
8
- export declare namespace OpenAICompatibleEngine {
9
- class Instance<in out fdm extends Function.Decl.Map.Proto> extends Engine.Instance<fdm> {
10
- protected options: Engine.Options<fdm>;
11
- protected toolCodec: ToolCodec<fdm>;
12
- protected messageCodec: MessageCodec<fdm>;
13
- protected billing: Billing;
14
- protected transport: Transport<fdm>;
15
- constructor(options: Engine.Options<fdm>);
16
- }
17
- function create<fdm extends Function.Decl.Map.Proto>(options: Engine.Options<fdm>): Engine<fdm>;
18
- export import Transport = TransportModule.Transport;
19
- }
@@ -1,43 +0,0 @@
1
- import { Engine } from "../engine.js";
2
- import { MessageCodec } from "./openai-compatible/message-codec.js";
3
- import { ToolCodec } from "./openai-responses/tool-codec.js";
4
- import { Billing } from "./openai-responses/billing.js";
5
- import * as TransportModule from "./openai-compatible/transport.js";
6
- export var OpenAICompatibleEngine;
7
- (function (OpenAICompatibleEngine) {
8
- class Instance extends Engine.Instance {
9
- options;
10
- toolCodec;
11
- messageCodec;
12
- billing;
13
- transport;
14
- constructor(options) {
15
- super(options);
16
- this.options = options;
17
- this.toolCodec = new ToolCodec({ fdm: this.fdm });
18
- this.messageCodec = new MessageCodec({
19
- toolCodec: this.toolCodec,
20
- messageValidator: this.messageValidator,
21
- });
22
- this.billing = new Billing({ pricing: this.pricing });
23
- this.transport = new OpenAICompatibleEngine.Transport({
24
- inferenceParams: this.inferenceOptions,
25
- providerSpec: this.providerSpecs,
26
- fdm: this.fdm,
27
- throttle: this.throttle,
28
- toolChoice: this.toolChoice,
29
- messageCodec: this.messageCodec,
30
- toolCodec: this.toolCodec,
31
- billing: this.billing,
32
- });
33
- }
34
- }
35
- OpenAICompatibleEngine.Instance = Instance;
36
- create;
37
- function create(options) {
38
- return new Instance(options);
39
- }
40
- OpenAICompatibleEngine.create = create;
41
- OpenAICompatibleEngine.Transport = TransportModule.Transport;
42
- })(OpenAICompatibleEngine || (OpenAICompatibleEngine = {}));
43
- //# sourceMappingURL=openai-compatible.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"openai-compatible.js","sourceRoot":"","sources":["../../src/engines/openai-compatible.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AACxD,OAAO,KAAK,eAAe,MAAM,kCAAkC,CAAC;AAMpE,MAAM,KAAW,sBAAsB,CAuCtC;AAvCD,WAAiB,sBAAsB;IACnC,MAAa,QAEX,SAAQ,MAAM,CAAC,QAAa;QAMG;QALnB,SAAS,CAAiB;QAC1B,YAAY,CAAoB;QAChC,OAAO,CAAU;QACR,SAAS,CAAiB;QAE7C,YAA6B,OAA4B;YACrD,KAAK,CAAC,OAAO,CAAC,CAAC;YADU,YAAO,GAAP,OAAO,CAAqB;YAGrD,IAAI,CAAC,SAAS,GAAG,IAAI,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;YAClD,IAAI,CAAC,YAAY,GAAG,IAAI,YAAY,CAAC;gBACjC,SAAS,EAAE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