@lectico/api 0.1.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.
- package/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/client.d.ts +52 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +191 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +104 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/agents.d.ts +49 -0
- package/dist/resources/agents.d.ts.map +1 -0
- package/dist/resources/agents.js +114 -0
- package/dist/resources/agents.js.map +1 -0
- package/dist/resources/conversations.d.ts +9 -0
- package/dist/resources/conversations.d.ts.map +1 -0
- package/dist/resources/conversations.js +23 -0
- package/dist/resources/conversations.js.map +1 -0
- package/dist/resources/files.d.ts +17 -0
- package/dist/resources/files.d.ts.map +1 -0
- package/dist/resources/files.js +23 -0
- package/dist/resources/files.js.map +1 -0
- package/dist/resources/knowledge.d.ts +14 -0
- package/dist/resources/knowledge.d.ts.map +1 -0
- package/dist/resources/knowledge.js +18 -0
- package/dist/resources/knowledge.js.map +1 -0
- package/dist/resources/leads.d.ts +15 -0
- package/dist/resources/leads.d.ts.map +1 -0
- package/dist/resources/leads.js +28 -0
- package/dist/resources/leads.js.map +1 -0
- package/dist/resources/usage.d.ts +9 -0
- package/dist/resources/usage.d.ts.map +1 -0
- package/dist/resources/usage.js +12 -0
- package/dist/resources/usage.js.map +1 -0
- package/dist/resources/webhooks.d.ts +33 -0
- package/dist/resources/webhooks.d.ts.map +1 -0
- package/dist/resources/webhooks.js +48 -0
- package/dist/resources/webhooks.js.map +1 -0
- package/dist/types.d.ts +230 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/dist/webhooks.d.ts +18 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +74 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Contenfo LLC (Lectico)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @lectico/api
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@lectico/api)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
Official Node.js SDK for the [Lectico](https://lectico.com) API — create AI assistants trained on your content, manage conversations, capture leads, and receive webhooks.
|
|
7
|
+
|
|
8
|
+
> **Beta / Early Access.** This SDK is pre-1.0 and the surface may change. Pin to an exact version in production. Breaking changes will be called out in release notes until `1.0.0`.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @lectico/api
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Node.js 18 or newer. ESM only.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import Lectico from "@lectico/api";
|
|
22
|
+
|
|
23
|
+
const lectico = new Lectico({ apiKey: process.env.LECTICO_API_KEY! });
|
|
24
|
+
|
|
25
|
+
// Create an agent
|
|
26
|
+
const agent = await lectico.agents.create({
|
|
27
|
+
type: "support",
|
|
28
|
+
name: "Helpdesk Bot",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Send a message (streaming by default)
|
|
32
|
+
const stream = await lectico.agents.messages.create(agent.id, {
|
|
33
|
+
message: "How do I reset my password?",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
for await (const ev of stream as AsyncIterable<any>) {
|
|
37
|
+
if (ev.type === "token") process.stdout.write(ev.content ?? "");
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Get a test API key (`sk_test_*`) from the [Lectico dashboard](https://app.lectico.com).
|
|
42
|
+
|
|
43
|
+
## Streaming vs JSON responses
|
|
44
|
+
|
|
45
|
+
Chat messages stream Server-Sent Events by default. Pass `stream: false` for a single JSON response:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const reply = await lectico.agents.messages.create(agent.id, {
|
|
49
|
+
message: "Hello",
|
|
50
|
+
stream: false,
|
|
51
|
+
});
|
|
52
|
+
// reply is a ChatMessage
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Stream event types include `token`, `sources`, `chunks`, `done`, `expert_thinking`, `escalation_offer`, `support_contact`, `error`.
|
|
56
|
+
|
|
57
|
+
## Webhook signature verification
|
|
58
|
+
|
|
59
|
+
Webhooks are signed with HMAC-SHA256. Verify the signature before trusting the payload:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import express from "express";
|
|
63
|
+
import { constructEvent, WebhookSignatureError } from "@lectico/api/webhooks";
|
|
64
|
+
|
|
65
|
+
const app = express();
|
|
66
|
+
|
|
67
|
+
app.post(
|
|
68
|
+
"/webhooks/lectico",
|
|
69
|
+
express.raw({ type: "application/json" }),
|
|
70
|
+
(req, res) => {
|
|
71
|
+
try {
|
|
72
|
+
const event = constructEvent(
|
|
73
|
+
req.body, // Buffer (raw body)
|
|
74
|
+
req.header("Lectico-Signature"),
|
|
75
|
+
process.env.LECTICO_WEBHOOK_SECRET!, // whsec_... from dashboard
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
switch (event.type) {
|
|
79
|
+
case "agent.created":
|
|
80
|
+
// ...
|
|
81
|
+
break;
|
|
82
|
+
case "lead.captured":
|
|
83
|
+
// ...
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
res.status(200).json({ received: true });
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (err instanceof WebhookSignatureError) {
|
|
90
|
+
return res.status(400).send(`Invalid signature: ${err.message}`);
|
|
91
|
+
}
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`constructEvent` validates the `t=...,v1=...` signature and rejects payloads older than 5 minutes (configurable via `toleranceSeconds`) to prevent replay attacks.
|
|
99
|
+
|
|
100
|
+
## Pagination
|
|
101
|
+
|
|
102
|
+
Lists are cursor-paginated. Iterate all results with `iterAll`:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
for await (const agent of lectico.agents.iterAll({ type: "support" })) {
|
|
106
|
+
console.log(agent.id, agent.name);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Or page manually:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const page = await lectico.agents.list({ limit: 50 });
|
|
114
|
+
if (page.meta.pagination.has_more) {
|
|
115
|
+
const next = await lectico.agents.list({
|
|
116
|
+
limit: 50,
|
|
117
|
+
starting_after: page.meta.pagination.next_cursor,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Error handling
|
|
123
|
+
|
|
124
|
+
All API errors extend `LecticoError`. Check the specific class to react appropriately:
|
|
125
|
+
|
|
126
|
+
| Class | HTTP | Meaning |
|
|
127
|
+
| ------------------------ | ---- | ------------------------------------ |
|
|
128
|
+
| `AuthenticationError` | 401 | Invalid or missing API key |
|
|
129
|
+
| `PaymentRequiredError` | 402 | Out of credits / plan limit reached |
|
|
130
|
+
| `PermissionDeniedError` | 403 | Key lacks permission for the resource|
|
|
131
|
+
| `NotFoundError` | 404 | Resource does not exist |
|
|
132
|
+
| `ConflictError` | 409 | Slug collision, idempotency mismatch |
|
|
133
|
+
| `InvalidRequestError` | 400 | Bad input — see `.param` |
|
|
134
|
+
| `RateLimitError` | 429 | Too many requests — see `.retryAfterSeconds` |
|
|
135
|
+
| `InternalServerError` | 5xx | Transient or unexpected server error |
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { RateLimitError, NotFoundError } from "@lectico/api";
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
await lectico.agents.get("agt_missing");
|
|
142
|
+
} catch (err) {
|
|
143
|
+
if (err instanceof NotFoundError) {
|
|
144
|
+
// ...
|
|
145
|
+
} else if (err instanceof RateLimitError) {
|
|
146
|
+
await sleep(err.retryAfterSeconds * 1000);
|
|
147
|
+
} else {
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The client automatically retries `429` and `5xx` responses with exponential backoff (default: 2 retries). Configure with `maxRetries` in the constructor.
|
|
154
|
+
|
|
155
|
+
## Idempotency
|
|
156
|
+
|
|
157
|
+
Pass an `idempotencyKey` to safely retry `POST` requests:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
await lectico.agents.create(
|
|
161
|
+
{ type: "support", name: "Bot" },
|
|
162
|
+
{ idempotencyKey: crypto.randomUUID() },
|
|
163
|
+
);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Configuration
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
new Lectico({
|
|
170
|
+
apiKey: "sk_live_...", // required
|
|
171
|
+
baseUrl: "https://api.lectico.com", // default
|
|
172
|
+
timeoutMs: 30000, // per-request timeout
|
|
173
|
+
maxRetries: 2, // 429/5xx retries
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Resources available
|
|
178
|
+
|
|
179
|
+
- `agents` — create, list, get, update, delete, `iterAll`, `messages.create` (streaming)
|
|
180
|
+
- `knowledge` — list, create, delete
|
|
181
|
+
- `conversations` — list, get
|
|
182
|
+
- `leads` — list, get, delete
|
|
183
|
+
- `webhooks` — create, list, get, update, delete, deliveries, test
|
|
184
|
+
- `files` — create (presigned URL), get, delete
|
|
185
|
+
- `usage` — summary (plan + usage)
|
|
186
|
+
|
|
187
|
+
## Documentation
|
|
188
|
+
|
|
189
|
+
Full API reference: **coming soon at [docs.lectico.com/api](https://docs.lectico.com/api)**.
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
[MIT](./LICENSE) © Contenfo LLC (Lectico)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ApiResponse, ApiList } from "./types.js";
|
|
2
|
+
export interface LecticoOptions {
|
|
3
|
+
/** API key. Use sk_live_* in production, sk_test_* in sandbox. */
|
|
4
|
+
apiKey: string;
|
|
5
|
+
/** Override the API base URL. Defaults to https://api.lectico.com. */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
/** Per-request timeout in milliseconds. Default 30000 (30s). */
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Maximum number of automatic retries for transient errors (429, 5xx).
|
|
11
|
+
* Default: 2. Set 0 to disable.
|
|
12
|
+
*/
|
|
13
|
+
maxRetries?: number;
|
|
14
|
+
/** Custom fetch (for testing). Defaults to global fetch. */
|
|
15
|
+
fetch?: typeof fetch;
|
|
16
|
+
}
|
|
17
|
+
export interface RequestOptions {
|
|
18
|
+
/** Idempotency-Key header for safe retry of POST requests. */
|
|
19
|
+
idempotencyKey?: string;
|
|
20
|
+
/** Override per-request timeout. */
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
/** Override per-request retry count. */
|
|
23
|
+
maxRetries?: number;
|
|
24
|
+
/** AbortSignal for cancellation. */
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
export declare class HttpClient {
|
|
28
|
+
private readonly apiKey;
|
|
29
|
+
private readonly baseUrl;
|
|
30
|
+
private readonly defaultTimeoutMs;
|
|
31
|
+
private readonly defaultMaxRetries;
|
|
32
|
+
private readonly fetchImpl;
|
|
33
|
+
constructor(opts: LecticoOptions);
|
|
34
|
+
/** Build standard headers for every request */
|
|
35
|
+
private headers;
|
|
36
|
+
/**
|
|
37
|
+
* Core fetch with retry on transient errors (429, 5xx).
|
|
38
|
+
* Returns the parsed JSON body. Throws LecticoError on non-2xx after retries.
|
|
39
|
+
*/
|
|
40
|
+
request<T>(method: string, path: string, opts?: {
|
|
41
|
+
body?: unknown;
|
|
42
|
+
query?: Record<string, string | number | undefined | null>;
|
|
43
|
+
headers?: Record<string, string>;
|
|
44
|
+
} & RequestOptions): Promise<T>;
|
|
45
|
+
/** Make a streaming POST request — returns the raw Response. Used by chat. */
|
|
46
|
+
stream(path: string, body: unknown, opts?: RequestOptions): Promise<Response>;
|
|
47
|
+
private buildUrl;
|
|
48
|
+
private parseErrorBody;
|
|
49
|
+
private isRetryable;
|
|
50
|
+
}
|
|
51
|
+
export type { ApiResponse, ApiList };
|
|
52
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAIrE,MAAM,WAAW,cAAc;IAC7B,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,8DAA8D;IAC9D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oCAAoC;IACpC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAC1C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAe;gBAE7B,IAAI,EAAE,cAAc;IAShC,+CAA+C;IAC/C,OAAO,CAAC,OAAO;IAUf;;;OAGG;IACG,OAAO,CAAC,CAAC,EACb,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE;QACJ,IAAI,CAAC,EAAE,OAAO,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC,CAAC;QAC3D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAClC,GAAG,cAAmB,GACtB,OAAO,CAAC,CAAC,CAAC;IA6Db,8EAA8E;IACxE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAE,cAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuBvF,OAAO,CAAC,QAAQ;YAeF,cAAc;IAwB5B,OAAO,CAAC,WAAW;CAMpB;AAiCD,YAAY,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// REQ-083 — HTTP client wrapper for the Lectico API
|
|
2
|
+
import { mapHttpToError, RateLimitError, InternalServerError } from "./errors.js";
|
|
3
|
+
const SDK_VERSION = "0.1.0";
|
|
4
|
+
export class HttpClient {
|
|
5
|
+
apiKey;
|
|
6
|
+
baseUrl;
|
|
7
|
+
defaultTimeoutMs;
|
|
8
|
+
defaultMaxRetries;
|
|
9
|
+
fetchImpl;
|
|
10
|
+
constructor(opts) {
|
|
11
|
+
if (!opts.apiKey)
|
|
12
|
+
throw new Error("LecticoClient: apiKey is required");
|
|
13
|
+
this.apiKey = opts.apiKey;
|
|
14
|
+
this.baseUrl = (opts.baseUrl ?? "https://api.lectico.com").replace(/\/+$/, "");
|
|
15
|
+
this.defaultTimeoutMs = opts.timeoutMs ?? 30_000;
|
|
16
|
+
this.defaultMaxRetries = opts.maxRetries ?? 2;
|
|
17
|
+
this.fetchImpl = opts.fetch ?? fetch;
|
|
18
|
+
}
|
|
19
|
+
/** Build standard headers for every request */
|
|
20
|
+
headers(extra) {
|
|
21
|
+
return {
|
|
22
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"Accept": "application/json",
|
|
25
|
+
"User-Agent": `lectico-sdk-node/${SDK_VERSION}`,
|
|
26
|
+
...extra,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Core fetch with retry on transient errors (429, 5xx).
|
|
31
|
+
* Returns the parsed JSON body. Throws LecticoError on non-2xx after retries.
|
|
32
|
+
*/
|
|
33
|
+
async request(method, path, opts = {}) {
|
|
34
|
+
const url = this.buildUrl(path, opts.query);
|
|
35
|
+
const headers = this.headers(opts.headers);
|
|
36
|
+
if (opts.idempotencyKey)
|
|
37
|
+
headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
38
|
+
const body = opts.body !== undefined ? JSON.stringify(opts.body) : undefined;
|
|
39
|
+
const maxRetries = opts.maxRetries ?? this.defaultMaxRetries;
|
|
40
|
+
const timeoutMs = opts.timeoutMs ?? this.defaultTimeoutMs;
|
|
41
|
+
let attempt = 0;
|
|
42
|
+
let lastError;
|
|
43
|
+
while (attempt <= maxRetries) {
|
|
44
|
+
attempt++;
|
|
45
|
+
const ctrl = new AbortController();
|
|
46
|
+
const timer = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
47
|
+
const signal = opts.signal
|
|
48
|
+
? combineSignals(ctrl.signal, opts.signal)
|
|
49
|
+
: ctrl.signal;
|
|
50
|
+
let res;
|
|
51
|
+
try {
|
|
52
|
+
res = await this.fetchImpl(url, { method, headers, body, signal });
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
if (attempt > maxRetries)
|
|
57
|
+
throw err;
|
|
58
|
+
await sleep(backoffMs(attempt));
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
if (res.ok) {
|
|
63
|
+
if (res.status === 204)
|
|
64
|
+
return undefined;
|
|
65
|
+
const text = await res.text();
|
|
66
|
+
if (!text)
|
|
67
|
+
return undefined;
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(text);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
throw new Error(`Lectico API returned non-JSON response: ${text.slice(0, 200)}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Non-2xx — parse error body, decide retry
|
|
76
|
+
const errBody = await this.parseErrorBody(res);
|
|
77
|
+
const apiErr = mapHttpToError(res.status, errBody, res.headers.get("retry-after"));
|
|
78
|
+
lastError = apiErr;
|
|
79
|
+
if (attempt > maxRetries || !this.isRetryable(apiErr)) {
|
|
80
|
+
throw apiErr;
|
|
81
|
+
}
|
|
82
|
+
// Wait before retry
|
|
83
|
+
const waitMs = apiErr instanceof RateLimitError && apiErr.retryAfterSeconds
|
|
84
|
+
? apiErr.retryAfterSeconds * 1000
|
|
85
|
+
: backoffMs(attempt);
|
|
86
|
+
await sleep(waitMs);
|
|
87
|
+
}
|
|
88
|
+
throw lastError ?? new Error("Lectico request failed for unknown reason");
|
|
89
|
+
}
|
|
90
|
+
/** Make a streaming POST request — returns the raw Response. Used by chat. */
|
|
91
|
+
async stream(path, body, opts = {}) {
|
|
92
|
+
const url = this.buildUrl(path);
|
|
93
|
+
const headers = this.headers({ "Accept": "text/event-stream" });
|
|
94
|
+
if (opts.idempotencyKey)
|
|
95
|
+
headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
96
|
+
const ctrl = new AbortController();
|
|
97
|
+
const timer = setTimeout(() => ctrl.abort(), opts.timeoutMs ?? this.defaultTimeoutMs);
|
|
98
|
+
const signal = opts.signal ? combineSignals(ctrl.signal, opts.signal) : ctrl.signal;
|
|
99
|
+
const res = await this.fetchImpl(url, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers,
|
|
102
|
+
body: JSON.stringify(body),
|
|
103
|
+
signal,
|
|
104
|
+
});
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
if (!res.ok) {
|
|
107
|
+
const errBody = await this.parseErrorBody(res);
|
|
108
|
+
throw mapHttpToError(res.status, errBody, res.headers.get("retry-after"));
|
|
109
|
+
}
|
|
110
|
+
return res;
|
|
111
|
+
}
|
|
112
|
+
buildUrl(path, query) {
|
|
113
|
+
let url = `${this.baseUrl}${path.startsWith("/") ? path : "/" + path}`;
|
|
114
|
+
if (query) {
|
|
115
|
+
const params = new URLSearchParams();
|
|
116
|
+
for (const [k, v] of Object.entries(query)) {
|
|
117
|
+
if (v !== undefined && v !== null && v !== "") {
|
|
118
|
+
params.set(k, String(v));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const q = params.toString();
|
|
122
|
+
if (q)
|
|
123
|
+
url += "?" + q;
|
|
124
|
+
}
|
|
125
|
+
return url;
|
|
126
|
+
}
|
|
127
|
+
async parseErrorBody(res) {
|
|
128
|
+
const text = await res.text();
|
|
129
|
+
if (!text) {
|
|
130
|
+
return {
|
|
131
|
+
error: { type: "internal_error", code: "empty_body", message: `HTTP ${res.status}` },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const parsed = JSON.parse(text);
|
|
136
|
+
if (parsed.error?.type)
|
|
137
|
+
return parsed;
|
|
138
|
+
return {
|
|
139
|
+
error: {
|
|
140
|
+
type: "internal_error",
|
|
141
|
+
code: "unknown",
|
|
142
|
+
message: typeof parsed === "string" ? parsed : text.slice(0, 200),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return {
|
|
148
|
+
error: { type: "internal_error", code: "non_json", message: text.slice(0, 200) },
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
isRetryable(err) {
|
|
153
|
+
// Retry on rate-limit and 5xx
|
|
154
|
+
if (err instanceof RateLimitError)
|
|
155
|
+
return true;
|
|
156
|
+
if (err instanceof InternalServerError && err.status >= 500)
|
|
157
|
+
return true;
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
// Helpers
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
function sleep(ms) {
|
|
165
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
166
|
+
}
|
|
167
|
+
function backoffMs(attempt) {
|
|
168
|
+
// Exponential with jitter: 500ms, 1500ms, 4500ms, ...
|
|
169
|
+
const base = Math.min(500 * Math.pow(3, attempt - 1), 30_000);
|
|
170
|
+
const jitter = Math.random() * 200;
|
|
171
|
+
return base + jitter;
|
|
172
|
+
}
|
|
173
|
+
/** Combine multiple AbortSignals so the request aborts if ANY fires. */
|
|
174
|
+
function combineSignals(...signals) {
|
|
175
|
+
if (signals.length === 1)
|
|
176
|
+
return signals[0];
|
|
177
|
+
// Node 20+ has AbortSignal.any
|
|
178
|
+
const anyFn = AbortSignal.any;
|
|
179
|
+
if (typeof anyFn === "function")
|
|
180
|
+
return anyFn(signals);
|
|
181
|
+
// Fallback for older runtimes
|
|
182
|
+
const ctrl = new AbortController();
|
|
183
|
+
for (const s of signals) {
|
|
184
|
+
if (s.aborted)
|
|
185
|
+
ctrl.abort(s.reason);
|
|
186
|
+
else
|
|
187
|
+
s.addEventListener("abort", () => ctrl.abort(s.reason), { once: true });
|
|
188
|
+
}
|
|
189
|
+
return ctrl.signal;
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,oDAAoD;AAEpD,OAAO,EAAE,cAAc,EAAgB,cAAc,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAGhG,MAAM,WAAW,GAAG,OAAO,CAAC;AA6B5B,MAAM,OAAO,UAAU;IACJ,MAAM,CAAS;IACf,OAAO,CAAS;IAChB,gBAAgB,CAAS;IACzB,iBAAiB,CAAS;IAC1B,SAAS,CAAe;IAEzC,YAAY,IAAoB;QAC9B,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,mCA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AO,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/E,CAAC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC;AACrB,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ApiErrorBody } from "./types.js";
|
|
2
|
+
export declare class LecticoError extends Error {
|
|
3
|
+
readonly type: ApiErrorBody["error"]["type"];
|
|
4
|
+
readonly code: string;
|
|
5
|
+
readonly status: number;
|
|
6
|
+
readonly param?: string;
|
|
7
|
+
readonly docUrl?: string;
|
|
8
|
+
readonly requestId?: string;
|
|
9
|
+
constructor(status: number, body: ApiErrorBody);
|
|
10
|
+
}
|
|
11
|
+
/** 401 — invalid or missing API key */
|
|
12
|
+
export declare class AuthenticationError extends LecticoError {
|
|
13
|
+
constructor(status: number, body: ApiErrorBody);
|
|
14
|
+
}
|
|
15
|
+
/** 402 — out of credits / plan limit reached */
|
|
16
|
+
export declare class PaymentRequiredError extends LecticoError {
|
|
17
|
+
constructor(status: number, body: ApiErrorBody);
|
|
18
|
+
}
|
|
19
|
+
/** 403 — API key lacks scope */
|
|
20
|
+
export declare class PermissionDeniedError extends LecticoError {
|
|
21
|
+
constructor(status: number, body: ApiErrorBody);
|
|
22
|
+
}
|
|
23
|
+
/** 404 — resource not found */
|
|
24
|
+
export declare class NotFoundError extends LecticoError {
|
|
25
|
+
constructor(status: number, body: ApiErrorBody);
|
|
26
|
+
}
|
|
27
|
+
/** 409 — conflict (idempotency mismatch, slug collision, etc.) */
|
|
28
|
+
export declare class ConflictError extends LecticoError {
|
|
29
|
+
constructor(status: number, body: ApiErrorBody);
|
|
30
|
+
}
|
|
31
|
+
/** 400 / 422 — invalid input */
|
|
32
|
+
export declare class InvalidRequestError extends LecticoError {
|
|
33
|
+
constructor(status: number, body: ApiErrorBody);
|
|
34
|
+
}
|
|
35
|
+
/** 429 — rate limit exceeded */
|
|
36
|
+
export declare class RateLimitError extends LecticoError {
|
|
37
|
+
readonly retryAfterSeconds?: number;
|
|
38
|
+
constructor(status: number, body: ApiErrorBody, retryAfter?: string | null);
|
|
39
|
+
}
|
|
40
|
+
/** 5xx — Lectico-side internal error */
|
|
41
|
+
export declare class InternalServerError extends LecticoError {
|
|
42
|
+
constructor(status: number, body: ApiErrorBody);
|
|
43
|
+
}
|
|
44
|
+
/** Map an HTTP status + parsed body to the correct error class */
|
|
45
|
+
export declare function mapHttpToError(status: number, body: ApiErrorBody, retryAfterHeader?: string | null): LecticoError;
|
|
46
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,qBAAa,YAAa,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;gBAEhB,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAU/C;AAED,uCAAuC;AACvC,qBAAa,mBAAoB,SAAQ,YAAY;gBACvC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,gDAAgD;AAChD,qBAAa,oBAAqB,SAAQ,YAAY;gBACxC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,gCAAgC;AAChC,qBAAa,qBAAsB,SAAQ,YAAY;gBACzC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,+BAA+B;AAC/B,qBAAa,aAAc,SAAQ,YAAY;gBACjC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,kEAAkE;AAClE,qBAAa,aAAc,SAAQ,YAAY;gBACjC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,gCAAgC;AAChC,qBAAa,mBAAoB,SAAQ,YAAY;gBACvC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,gCAAgC;AAChC,qBAAa,cAAe,SAAQ,YAAY;IAC9C,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;gBACxB,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI;CAQ3E;AAED,wCAAwC;AACxC,qBAAa,mBAAoB,SAAQ,YAAY;gBACvC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAI/C;AAED,kEAAkE;AAClE,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,YAAY,EAClB,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,GAC/B,YAAY,CAoBd"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// REQ-083 — SDK error classes (typed by API error type)
|
|
2
|
+
export class LecticoError extends Error {
|
|
3
|
+
type;
|
|
4
|
+
code;
|
|
5
|
+
status;
|
|
6
|
+
param;
|
|
7
|
+
docUrl;
|
|
8
|
+
requestId;
|
|
9
|
+
constructor(status, body) {
|
|
10
|
+
super(body.error.message);
|
|
11
|
+
this.name = "LecticoError";
|
|
12
|
+
this.type = body.error.type;
|
|
13
|
+
this.code = body.error.code;
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.param = body.error.param;
|
|
16
|
+
this.docUrl = body.error.doc_url;
|
|
17
|
+
this.requestId = body.meta?.request_id;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** 401 — invalid or missing API key */
|
|
21
|
+
export class AuthenticationError extends LecticoError {
|
|
22
|
+
constructor(status, body) {
|
|
23
|
+
super(status, body);
|
|
24
|
+
this.name = "AuthenticationError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** 402 — out of credits / plan limit reached */
|
|
28
|
+
export class PaymentRequiredError extends LecticoError {
|
|
29
|
+
constructor(status, body) {
|
|
30
|
+
super(status, body);
|
|
31
|
+
this.name = "PaymentRequiredError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** 403 — API key lacks scope */
|
|
35
|
+
export class PermissionDeniedError extends LecticoError {
|
|
36
|
+
constructor(status, body) {
|
|
37
|
+
super(status, body);
|
|
38
|
+
this.name = "PermissionDeniedError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** 404 — resource not found */
|
|
42
|
+
export class NotFoundError extends LecticoError {
|
|
43
|
+
constructor(status, body) {
|
|
44
|
+
super(status, body);
|
|
45
|
+
this.name = "NotFoundError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** 409 — conflict (idempotency mismatch, slug collision, etc.) */
|
|
49
|
+
export class ConflictError extends LecticoError {
|
|
50
|
+
constructor(status, body) {
|
|
51
|
+
super(status, body);
|
|
52
|
+
this.name = "ConflictError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** 400 / 422 — invalid input */
|
|
56
|
+
export class InvalidRequestError extends LecticoError {
|
|
57
|
+
constructor(status, body) {
|
|
58
|
+
super(status, body);
|
|
59
|
+
this.name = "InvalidRequestError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** 429 — rate limit exceeded */
|
|
63
|
+
export class RateLimitError extends LecticoError {
|
|
64
|
+
retryAfterSeconds;
|
|
65
|
+
constructor(status, body, retryAfter) {
|
|
66
|
+
super(status, body);
|
|
67
|
+
this.name = "RateLimitError";
|
|
68
|
+
if (retryAfter) {
|
|
69
|
+
const parsed = Number.parseInt(retryAfter, 10);
|
|
70
|
+
if (!Number.isNaN(parsed))
|
|
71
|
+
this.retryAfterSeconds = parsed;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** 5xx — Lectico-side internal error */
|
|
76
|
+
export class InternalServerError extends LecticoError {
|
|
77
|
+
constructor(status, body) {
|
|
78
|
+
super(status, body);
|
|
79
|
+
this.name = "InternalServerError";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Map an HTTP status + parsed body to the correct error class */
|
|
83
|
+
export function mapHttpToError(status, body, retryAfterHeader) {
|
|
84
|
+
switch (status) {
|
|
85
|
+
case 400:
|
|
86
|
+
case 422:
|
|
87
|
+
return new InvalidRequestError(status, body);
|
|
88
|
+
case 401:
|
|
89
|
+
return new AuthenticationError(status, body);
|
|
90
|
+
case 402:
|
|
91
|
+
return new PaymentRequiredError(status, body);
|
|
92
|
+
case 403:
|
|
93
|
+
return new PermissionDeniedError(status, body);
|
|
94
|
+
case 404:
|
|
95
|
+
return new NotFoundError(status, body);
|
|
96
|
+
case 409:
|
|
97
|
+
return new ConflictError(status, body);
|
|
98
|
+
case 429:
|
|
99
|
+
return new RateLimitError(status, body, retryAfterHeader);
|
|
100
|
+
default:
|
|
101
|
+
return new InternalServerError(status, body);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,wDAAwD;AAIxD,MAAM,OAAO,YAAa,SAAQ,KAAK;IAC5B,IAAI,CAAgC;IACpC,IAAI,CAAS;IACb,MAAM,CAAS;IACf,KAAK,CAAU;IACf,MAAM,CAAU;IAChB,SAAS,CAAU;IAE5B,YAAY,MAAc,EAAE,IA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N,OAAO,IAAI,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC/C,KAAK,GAAG;YACN,OAAO,IAAI,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC/C,KAAK,GAAG;YACN,OAAO,IAAI,oBAAoB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAChD,KAAK,GAAG;YACN,OAAO,IAAI,qBAAqB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACjD,KAAK,GAAG;YACN,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACzC,KAAK,GAAG;YACN,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACzC,KAAK,GAAG;YACN,OAAO,IAAI,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAC5D;YACE,OAAO,IAAI,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACjD,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type LecticoOptions } from "./client.js";
|
|
2
|
+
import { AgentsResource } from "./resources/agents.js";
|
|
3
|
+
import { KnowledgeResource } from "./resources/knowledge.js";
|
|
4
|
+
import { ConversationsResource } from "./resources/conversations.js";
|
|
5
|
+
import { LeadsResource } from "./resources/leads.js";
|
|
6
|
+
import { WebhooksResource } from "./resources/webhooks.js";
|
|
7
|
+
import { FilesResource } from "./resources/files.js";
|
|
8
|
+
import { UsageResource } from "./resources/usage.js";
|
|
9
|
+
export default class Lectico {
|
|
10
|
+
readonly agents: AgentsResource;
|
|
11
|
+
readonly knowledge: KnowledgeResource;
|
|
12
|
+
readonly conversations: ConversationsResource;
|
|
13
|
+
readonly leads: LeadsResource;
|
|
14
|
+
readonly webhooks: WebhooksResource;
|
|
15
|
+
readonly files: FilesResource;
|
|
16
|
+
readonly usage: UsageResource;
|
|
17
|
+
private readonly client;
|
|
18
|
+
constructor(opts: LecticoOptions);
|
|
19
|
+
}
|
|
20
|
+
export { Lectico };
|
|
21
|
+
export * from "./errors.js";
|
|
22
|
+
export * from "./types.js";
|
|
23
|
+
export { constructEvent, WebhookSignatureError } from "./webhooks.js";
|
|
24
|
+
export type { StreamEvent } from "./resources/agents.js";
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAc,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,iBAAiB,CAAC;IACtC,QAAQ,CAAC,aAAa,EAAE,qBAAqB,CAAC;IAC9C,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IACpC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;gBAExB,IAAI,EAAE,cAAc;CAUjC;AAED,OAAO,EAAE,OAAO,EAAE,CAAC;AACnB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACtE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC"}
|