@ywldan/kiro 0.0.0-stage → 0.1.1

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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +429 -2
  3. package/dist/agent/compact.js +133 -0
  4. package/dist/agent/compact.js.map +1 -0
  5. package/dist/agent/loop.js +253 -0
  6. package/dist/agent/loop.js.map +1 -0
  7. package/dist/agent/prompt.js +53 -0
  8. package/dist/agent/prompt.js.map +1 -0
  9. package/dist/agent/tokens.js +36 -0
  10. package/dist/agent/tokens.js.map +1 -0
  11. package/dist/config.js +382 -0
  12. package/dist/config.js.map +1 -0
  13. package/dist/env.js +203 -0
  14. package/dist/env.js.map +1 -0
  15. package/dist/index.js +272 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/model/cli-ui.js +458 -0
  18. package/dist/model/cli-ui.js.map +1 -0
  19. package/dist/model/cli.js +382 -0
  20. package/dist/model/cli.js.map +1 -0
  21. package/dist/model/contexts.js +61 -0
  22. package/dist/model/contexts.js.map +1 -0
  23. package/dist/model/discovery.js +225 -0
  24. package/dist/model/discovery.js.map +1 -0
  25. package/dist/model/presets.js +60 -0
  26. package/dist/model/presets.js.map +1 -0
  27. package/dist/model/wizard.js +663 -0
  28. package/dist/model/wizard.js.map +1 -0
  29. package/dist/providers/index.js +10 -0
  30. package/dist/providers/index.js.map +1 -0
  31. package/dist/providers/openai.js +422 -0
  32. package/dist/providers/openai.js.map +1 -0
  33. package/dist/providers/types.js +19 -0
  34. package/dist/providers/types.js.map +1 -0
  35. package/dist/secrets.js +63 -0
  36. package/dist/secrets.js.map +1 -0
  37. package/dist/sessions.js +248 -0
  38. package/dist/sessions.js.map +1 -0
  39. package/dist/tools/diff.js +32 -0
  40. package/dist/tools/diff.js.map +1 -0
  41. package/dist/tools/fs.js +524 -0
  42. package/dist/tools/fs.js.map +1 -0
  43. package/dist/tools/glob.js +105 -0
  44. package/dist/tools/glob.js.map +1 -0
  45. package/dist/tools/ignore.js +63 -0
  46. package/dist/tools/ignore.js.map +1 -0
  47. package/dist/tools/index.js +65 -0
  48. package/dist/tools/index.js.map +1 -0
  49. package/dist/tools/patch.js +191 -0
  50. package/dist/tools/patch.js.map +1 -0
  51. package/dist/tools/paths.js +85 -0
  52. package/dist/tools/paths.js.map +1 -0
  53. package/dist/tools/search.js +553 -0
  54. package/dist/tools/search.js.map +1 -0
  55. package/dist/tools/shell.js +189 -0
  56. package/dist/tools/shell.js.map +1 -0
  57. package/dist/tools/types.js +16 -0
  58. package/dist/tools/types.js.map +1 -0
  59. package/dist/ui/App.js +1074 -0
  60. package/dist/ui/App.js.map +1 -0
  61. package/dist/ui/ApprovalPrompt.js +48 -0
  62. package/dist/ui/ApprovalPrompt.js.map +1 -0
  63. package/dist/ui/Input.js +213 -0
  64. package/dist/ui/Input.js.map +1 -0
  65. package/dist/ui/MessageList.js +214 -0
  66. package/dist/ui/MessageList.js.map +1 -0
  67. package/dist/ui/ModelPicker.js +100 -0
  68. package/dist/ui/ModelPicker.js.map +1 -0
  69. package/dist/ui/StatusBar.js +63 -0
  70. package/dist/ui/StatusBar.js.map +1 -0
  71. package/dist/ui/approval-policy.js +22 -0
  72. package/dist/ui/approval-policy.js.map +1 -0
  73. package/dist/ui/commands.js +30 -0
  74. package/dist/ui/commands.js.map +1 -0
  75. package/dist/ui/export.js +65 -0
  76. package/dist/ui/export.js.map +1 -0
  77. package/dist/ui/history.js +86 -0
  78. package/dist/ui/history.js.map +1 -0
  79. package/dist/ui/markdown.js +285 -0
  80. package/dist/ui/markdown.js.map +1 -0
  81. package/dist/ui/model-picker.js +59 -0
  82. package/dist/ui/model-picker.js.map +1 -0
  83. package/dist/ui/mouse.js +36 -0
  84. package/dist/ui/mouse.js.map +1 -0
  85. package/dist/ui/profile-list.js +40 -0
  86. package/dist/ui/profile-list.js.map +1 -0
  87. package/dist/ui/text.js +328 -0
  88. package/dist/ui/text.js.map +1 -0
  89. package/dist/ui/types.js +3 -0
  90. package/dist/ui/types.js.map +1 -0
  91. package/dist/version.js +2 -0
  92. package/dist/version.js.map +1 -0
  93. package/package.json +58 -4
@@ -0,0 +1,253 @@
1
+ import { ProviderError } from '../providers/types.js';
2
+ import { executeTool, isReadOnlyTool, toolDefinitions, } from '../tools/index.js';
3
+ export const MAX_TOOL_ROUNDS = 25;
4
+ /** Groups contiguous read-only calls so they can run in parallel; mutations stay sequential. */
5
+ export function planToolBatches(calls) {
6
+ const batches = [];
7
+ for (const call of calls) {
8
+ const readOnly = isReadOnlyTool(call.function.name);
9
+ const current = batches[batches.length - 1];
10
+ if (readOnly && current?.readOnly) {
11
+ current.calls.push(call);
12
+ }
13
+ else {
14
+ batches.push({ readOnly, calls: [call] });
15
+ }
16
+ }
17
+ return batches;
18
+ }
19
+ function errorMessage(error) {
20
+ if (error instanceof Error)
21
+ return error.message;
22
+ return String(error);
23
+ }
24
+ function cancelledResult() {
25
+ return {
26
+ content: 'Cancelled by user before this tool ran.',
27
+ summary: 'cancelled',
28
+ status: 'denied',
29
+ };
30
+ }
31
+ /**
32
+ * Runs one user turn, appending assistant/tool messages to `messages` as it goes.
33
+ * The messages array must already contain the system prompt and the new user message.
34
+ */
35
+ export async function* runAgentTurn(options) {
36
+ const { provider, messages, ctx } = options;
37
+ const maxRounds = options.maxToolRounds ?? MAX_TOOL_ROUNDS;
38
+ let rounds = 0;
39
+ let compactionAttempted = false;
40
+ let overflowRetried = false;
41
+ while (true) {
42
+ if (ctx.signal.aborted) {
43
+ yield { type: 'done', reason: 'aborted' };
44
+ return;
45
+ }
46
+ if (!compactionAttempted && options.compactContext) {
47
+ try {
48
+ const compacted = await options.compactContext(messages, ctx.signal);
49
+ if (compacted) {
50
+ messages.splice(0, messages.length, ...compacted.messages);
51
+ compactionAttempted = true;
52
+ yield {
53
+ type: 'notice',
54
+ level: 'info',
55
+ message: `Context was compacted: ${compacted.summarizedMessages} earlier messages summarized ` +
56
+ `(~${compacted.beforeTokens.toLocaleString()} → ~${compacted.afterTokens.toLocaleString()} tokens).`,
57
+ };
58
+ }
59
+ }
60
+ catch (error) {
61
+ compactionAttempted = true;
62
+ if (!ctx.signal.aborted) {
63
+ yield {
64
+ type: 'notice',
65
+ level: 'error',
66
+ message: `Could not compact context: ${errorMessage(error)}`,
67
+ };
68
+ }
69
+ }
70
+ }
71
+ if (ctx.signal.aborted) {
72
+ yield { type: 'done', reason: 'aborted' };
73
+ return;
74
+ }
75
+ const textParts = [];
76
+ const toolCalls = [];
77
+ let streamError;
78
+ let finishReason;
79
+ try {
80
+ for await (const event of provider.stream(messages, toolDefinitions, { signal: ctx.signal })) {
81
+ if (ctx.signal.aborted)
82
+ break;
83
+ if (event.type === 'text') {
84
+ textParts.push(event.text);
85
+ yield { type: 'text', text: event.text };
86
+ }
87
+ else if (event.type === 'tool_call') {
88
+ toolCalls.push(event.toolCall);
89
+ }
90
+ else if (event.type === 'usage') {
91
+ yield { type: 'usage', usage: event.usage };
92
+ }
93
+ else if (event.type === 'finish') {
94
+ finishReason = event.reason;
95
+ }
96
+ else if (event.type === 'notice') {
97
+ yield { type: 'notice', level: 'info', message: event.message };
98
+ }
99
+ }
100
+ }
101
+ catch (error) {
102
+ streamError = error;
103
+ }
104
+ const text = textParts.join('');
105
+ if (ctx.signal.aborted) {
106
+ if (text) {
107
+ messages.push({ role: 'assistant', content: text });
108
+ yield { type: 'assistant', content: text };
109
+ }
110
+ yield { type: 'done', reason: 'aborted' };
111
+ return;
112
+ }
113
+ if (streamError) {
114
+ const overflow = streamError instanceof ProviderError && streamError.overflow;
115
+ if (overflow && !overflowRetried && options.compactContext) {
116
+ overflowRetried = true;
117
+ compactionAttempted = true;
118
+ let compacted = null;
119
+ let compactError;
120
+ try {
121
+ compacted = await options.compactContext(messages, ctx.signal, { force: true });
122
+ }
123
+ catch (error) {
124
+ compactError = error;
125
+ }
126
+ if (compacted) {
127
+ messages.splice(0, messages.length, ...compacted.messages);
128
+ if (!ctx.signal.aborted) {
129
+ yield {
130
+ type: 'notice',
131
+ level: 'info',
132
+ message: `The endpoint rejected the request as too long. ${compacted.summarizedMessages} earlier ` +
133
+ `messages were summarized (~${compacted.beforeTokens.toLocaleString()} → ` +
134
+ `~${compacted.afterTokens.toLocaleString()} tokens); retrying the turn.`,
135
+ };
136
+ }
137
+ continue;
138
+ }
139
+ if (ctx.signal.aborted)
140
+ continue;
141
+ yield {
142
+ type: 'notice',
143
+ level: 'error',
144
+ message: compactError
145
+ ? `The request exceeded the model's context window and compaction failed: ${errorMessage(compactError)}`
146
+ : `The request exceeded the model's context window, but there was nothing left to summarize. ` +
147
+ 'Set a smaller contextWindow for this profile, or start a new session.',
148
+ };
149
+ }
150
+ if (text) {
151
+ messages.push({ role: 'assistant', content: text });
152
+ yield { type: 'assistant', content: text };
153
+ }
154
+ yield { type: 'notice', level: 'error', message: errorMessage(streamError) };
155
+ yield { type: 'done', reason: 'error' };
156
+ return;
157
+ }
158
+ if (finishReason === 'length') {
159
+ yield {
160
+ type: 'notice',
161
+ level: 'error',
162
+ message: toolCalls.length > 0
163
+ ? 'The model hit its max_tokens limit mid-response, so tool call arguments may be incomplete. ' +
164
+ 'Raise maxTokens for this profile or ask for smaller steps.'
165
+ : 'The model hit its max_tokens limit and the response was cut short. ' +
166
+ 'Raise maxTokens for this profile, or ask it to continue.',
167
+ };
168
+ }
169
+ if (toolCalls.length === 0) {
170
+ if (text) {
171
+ messages.push({ role: 'assistant', content: text });
172
+ yield { type: 'assistant', content: text };
173
+ }
174
+ else {
175
+ yield { type: 'notice', level: 'info', message: 'The model returned an empty response.' };
176
+ }
177
+ yield { type: 'done', reason: 'complete' };
178
+ return;
179
+ }
180
+ messages.push({
181
+ role: 'assistant',
182
+ content: text || null,
183
+ tool_calls: toolCalls,
184
+ });
185
+ if (text)
186
+ yield { type: 'assistant', content: text };
187
+ for (const batch of planToolBatches(toolCalls)) {
188
+ if (ctx.signal.aborted) {
189
+ for (const toolCall of batch.calls) {
190
+ const result = cancelledResult();
191
+ messages.push({
192
+ role: 'tool',
193
+ tool_call_id: toolCall.id,
194
+ name: toolCall.function.name,
195
+ content: result.content,
196
+ });
197
+ yield { type: 'tool-start', toolCall };
198
+ yield { type: 'tool-end', toolCall, result, durationMs: 0 };
199
+ }
200
+ continue;
201
+ }
202
+ if (!batch.readOnly) {
203
+ for (const toolCall of batch.calls) {
204
+ yield { type: 'tool-start', toolCall };
205
+ const startedAt = Date.now();
206
+ const result = await executeTool(toolCall.function.name, toolCall.function.arguments, ctx);
207
+ const durationMs = Date.now() - startedAt;
208
+ messages.push({
209
+ role: 'tool',
210
+ tool_call_id: toolCall.id,
211
+ name: toolCall.function.name,
212
+ content: result.content,
213
+ });
214
+ yield { type: 'tool-end', toolCall, result, durationMs };
215
+ }
216
+ continue;
217
+ }
218
+ for (const toolCall of batch.calls) {
219
+ yield { type: 'tool-start', toolCall };
220
+ }
221
+ const outcomes = await Promise.all(batch.calls.map(async (toolCall) => {
222
+ const startedAt = Date.now();
223
+ const result = await executeTool(toolCall.function.name, toolCall.function.arguments, ctx);
224
+ return { result, durationMs: Date.now() - startedAt };
225
+ }));
226
+ for (const [index, outcome] of outcomes.entries()) {
227
+ const toolCall = batch.calls[index];
228
+ messages.push({
229
+ role: 'tool',
230
+ tool_call_id: toolCall.id,
231
+ name: toolCall.function.name,
232
+ content: outcome.result.content,
233
+ });
234
+ yield { type: 'tool-end', toolCall, result: outcome.result, durationMs: outcome.durationMs };
235
+ }
236
+ }
237
+ rounds++;
238
+ if (ctx.signal.aborted) {
239
+ yield { type: 'done', reason: 'aborted' };
240
+ return;
241
+ }
242
+ if (rounds >= maxRounds) {
243
+ yield {
244
+ type: 'notice',
245
+ level: 'info',
246
+ message: `Stopped after ${maxRounds} tool rounds. Send another message to continue.`,
247
+ };
248
+ yield { type: 'done', reason: 'max-iterations' };
249
+ return;
250
+ }
251
+ }
252
+ }
253
+ //# sourceMappingURL=loop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loop.js","sourceRoot":"","sources":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I,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;YACjD,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { platform, release } from 'node:os';
3
+ import path from 'node:path';
4
+ const MAX_AGENTS_MD_BYTES = 200_000;
5
+ function osName() {
6
+ switch (platform()) {
7
+ case 'win32':
8
+ return `Windows (${release()})`;
9
+ case 'darwin':
10
+ return `macOS (${release()})`;
11
+ default:
12
+ return `${platform()} (${release()})`;
13
+ }
14
+ }
15
+ export async function buildSystemPrompt(cwd) {
16
+ const base = [
17
+ 'You are Kiro, a terminal-based coding agent working in the user\'s current directory.',
18
+ `Working directory: ${cwd}`,
19
+ `Operating system: ${osName()}`,
20
+ '',
21
+ 'How to work:',
22
+ '- Explore before editing. Use glob to find files and grep to locate symbols or strings; read only the files and ranges you need with read_file.',
23
+ '- Do not read a whole repository. Search first, then read focused sections (read_file offset/limit).',
24
+ '- Make minimal, focused changes. Do not refactor or add unrequested features.',
25
+ '- Use edit_file for a single exact replacement (set replace_all=true when the same text must change everywhere).',
26
+ '- Use multi_edit when making several replacements in one file; it validates every edit before writing, so either all changes land or none do.',
27
+ '- Use apply_patch for coordinated changes across multiple files, or when you already have a unified diff. Patches are validated before anything is written.',
28
+ '- Use write_file only for new files or complete rewrites.',
29
+ '- All file paths are resolved relative to the working directory. Paths escaping it require user approval.',
30
+ '- Prefer run_command for builds, tests, and quick checks so you can verify your work. Commands run in the working directory.',
31
+ '- Tool output may be truncated. Use read_file offsets, narrower grep patterns/globs, or smaller result limits if you need more detail.',
32
+ '- If a tool returns an error, read it carefully and adjust; edit_file/multi_edit errors include a hint about the closest line or whitespace mismatch.',
33
+ '- The user must approve write_file, edit_file, multi_edit, apply_patch, and run_command. If they deny a call, respect the decision and explain alternatives instead of retrying the same action.',
34
+ '- Do not invent file contents or command output. Keep final answers concise: say what changed and include relevant verification results.',
35
+ '- A turn is limited to 25 tool-call rounds. Keep work focused and finish when the task is done.',
36
+ '',
37
+ 'You have these tools: read_file, write_file, edit_file, multi_edit, apply_patch, list_dir, glob, grep, run_command.',
38
+ ].join('\n');
39
+ const agentsPath = path.join(cwd, 'AGENTS.md');
40
+ try {
41
+ const content = await readFile(agentsPath, 'utf8');
42
+ const clipped = content.length > MAX_AGENTS_MD_BYTES
43
+ ? content.slice(0, MAX_AGENTS_MD_BYTES) + '\n\n[AGENTS.md truncated by Kiro]'
44
+ : content;
45
+ return `${base}\n\n# Project instructions from AGENTS.md\n\n${clipped.trimEnd()}\n`;
46
+ }
47
+ catch (error) {
48
+ if (error.code === 'ENOENT')
49
+ return base;
50
+ return `${base}\n\n[Could not read AGENTS.md: ${error.message}]\n`;
51
+ }
52
+ }
53
+ //# sourceMappingURL=prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/agent/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,mBAAmB,GAAG,OAAO,CAAC;AAEpC,SAAS,MAAM;IACb,QAAQ,QAAQ,EAAE,EAAE,CAAC;QACnB,KAAK,OAAO;YACV,OAAO,YAAY,OAAO,EAAE,GAAG,CAAC;QAClC,KAAK,QAAQ;YACX,OAAO,UAAU,OAAO,EAAE,GAAG,CAAC;QAChC;YACE,OAAO,GAAG,QAAQ,EAAE,KAAK,OAAO,EAAE,GAAG,CAAC;IAC1C,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,GAAW;IACjD,MAAM,IAAI,GAAG;QACX,uFAAuF;QACvF,sBAAsB,GAAG,EAAE;QAC3B,qBAAqB,MAAM,EAAE,EAAE;QAC/B,EAAE;QACF,cAAc;QACd,iJAAiJ;QACjJ,sGAAsG;QACtG,+EAA+E;QAC/E,kHAAkH;QAClH,+IAA+I;QAC/I,6JAA6J;QAC7J,2DAA2D;QAC3D,2GAA2G;QAC3G,8HAA8H;QAC9H,wIAAwI;QACxI,uJAAuJ;QACvJ,kMAAkM;QAClM,0IAA0I;QAC1I,iGAAiG;QACjG,EAAE;QACF,qHAAqH;KACtH,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAEb,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAC/C,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACnD,MAAM,OAAO,GACX,OAAO,CAAC,MAAM,GAAG,mBAAmB;YAClC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,mBAAmB,CAAC,GAAG,mCAAmC;YAC7E,CAAC,CAAC,OAAO,CAAC;QACd,OAAO,GAAG,IAAI,gDAAgD,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC;IACtF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACpE,OAAO,GAAG,IAAI,kCAAmC,KAAe,CAAC,OAAO,KAAK,CAAC;IAChF,CAAC;AACH,CAAC"}
@@ -0,0 +1,36 @@
1
+ const MESSAGE_OVERHEAD_TOKENS = 4;
2
+ /** Rough estimate used when the endpoint does not report usage. */
3
+ export function estimateTextTokens(text) {
4
+ if (!text)
5
+ return 0;
6
+ return Math.ceil(text.length / 4);
7
+ }
8
+ export function estimateMessageTokens(message) {
9
+ let total = MESSAGE_OVERHEAD_TOKENS;
10
+ if (message.content)
11
+ total += estimateTextTokens(message.content);
12
+ if (message.tool_calls?.length) {
13
+ total += estimateTextTokens(JSON.stringify(message.tool_calls));
14
+ }
15
+ if (message.name)
16
+ total += estimateTextTokens(message.name);
17
+ return total;
18
+ }
19
+ export function estimateMessagesTokens(messages) {
20
+ return messages.reduce((total, message) => total + estimateMessageTokens(message), 0);
21
+ }
22
+ /** Compact, human-readable token counts for the status bar and notices. */
23
+ export function formatTokens(count) {
24
+ const value = Math.max(0, Math.round(count));
25
+ if (value < 1_000)
26
+ return String(value);
27
+ if (value < 10_000)
28
+ return `${Math.round(value / 100) / 10}k`;
29
+ return `${Math.round(value / 1_000)}k`;
30
+ }
31
+ export function formatPercent(used, window) {
32
+ if (window <= 0)
33
+ return '0%';
34
+ return `${Math.min(999, Math.round((used / window) * 100))}%`;
35
+ }
36
+ //# sourceMappingURL=tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../src/agent/tokens.ts"],"names":[],"mappings":"AAEA,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAElC,mEAAmE;AACnE,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,IAAI,CAAC,IAAI;QAAE,OAAO,CAAC,CAAC;IACpB,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,OAAoB;IACxD,IAAI,KAAK,GAAG,uBAAuB,CAAC;IACpC,IAAI,OAAO,CAAC,OAAO;QAAE,KAAK,IAAI,kBAAkB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClE,IAAI,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;QAC/B,KAAK,IAAI,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,OAAO,CAAC,IAAI;QAAE,KAAK,IAAI,kBAAkB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,QAAu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