@tungoai/sdk 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/LICENSE +21 -0
  3. package/README.kk.md +327 -0
  4. package/README.md +346 -2
  5. package/README.ru.md +327 -0
  6. package/dist/cjs/chat.d.ts +85 -0
  7. package/dist/cjs/chat.js +151 -0
  8. package/dist/cjs/client.d.ts +116 -0
  9. package/dist/cjs/client.js +223 -0
  10. package/dist/cjs/core.d.ts +210 -0
  11. package/dist/cjs/core.js +605 -0
  12. package/dist/cjs/errors.d.ts +212 -0
  13. package/dist/cjs/errors.js +410 -0
  14. package/dist/cjs/generated/resources.d.ts +1059 -0
  15. package/dist/cjs/generated/resources.js +1064 -0
  16. package/dist/cjs/generated/types.d.ts +4927 -0
  17. package/dist/cjs/generated/types.js +11 -0
  18. package/dist/cjs/index.d.ts +12 -0
  19. package/dist/cjs/index.js +80 -0
  20. package/dist/cjs/json.d.ts +10 -0
  21. package/dist/cjs/json.js +120 -0
  22. package/dist/cjs/money.d.ts +32 -0
  23. package/dist/cjs/money.js +141 -0
  24. package/dist/cjs/package.json +1 -0
  25. package/dist/cjs/pagination.d.ts +47 -0
  26. package/dist/cjs/pagination.js +113 -0
  27. package/dist/cjs/streaming.d.ts +74 -0
  28. package/dist/cjs/streaming.js +253 -0
  29. package/dist/cjs/version.d.ts +2 -0
  30. package/dist/cjs/version.js +6 -0
  31. package/dist/cjs/webhooks.d.ts +74 -0
  32. package/dist/cjs/webhooks.js +248 -0
  33. package/dist/esm/chat.d.ts +85 -0
  34. package/dist/esm/chat.js +143 -0
  35. package/dist/esm/client.d.ts +116 -0
  36. package/dist/esm/client.js +219 -0
  37. package/dist/esm/core.d.ts +210 -0
  38. package/dist/esm/core.js +589 -0
  39. package/dist/esm/errors.d.ts +212 -0
  40. package/dist/esm/errors.js +381 -0
  41. package/dist/esm/generated/resources.d.ts +1059 -0
  42. package/dist/esm/generated/resources.js +1048 -0
  43. package/dist/esm/generated/types.d.ts +4927 -0
  44. package/dist/esm/generated/types.js +10 -0
  45. package/dist/esm/index.d.ts +12 -0
  46. package/dist/esm/index.js +11 -0
  47. package/dist/esm/json.d.ts +10 -0
  48. package/dist/esm/json.js +116 -0
  49. package/dist/esm/money.d.ts +32 -0
  50. package/dist/esm/money.js +137 -0
  51. package/dist/esm/package.json +1 -0
  52. package/dist/esm/pagination.d.ts +47 -0
  53. package/dist/esm/pagination.js +107 -0
  54. package/dist/esm/streaming.d.ts +74 -0
  55. package/dist/esm/streaming.js +247 -0
  56. package/dist/esm/version.d.ts +2 -0
  57. package/dist/esm/version.js +3 -0
  58. package/dist/esm/webhooks.d.ts +74 -0
  59. package/dist/esm/webhooks.js +239 -0
  60. package/package.json +67 -4
@@ -0,0 +1,210 @@
1
+ import { TungoError } from './errors.js';
2
+ import type { ResponseInfo, StreamInit, Stream } from './streaming.js';
3
+ /** The production API host. */
4
+ export declare const DEFAULT_BASE_URL = "https://api.tungo.kz";
5
+ /** Default timeout of an ordinary request, in milliseconds. */
6
+ export declare const DEFAULT_TIMEOUT = 60000;
7
+ /** Default timeout of a non-streaming chat turn, in milliseconds. */
8
+ export declare const DEFAULT_LONG_RUNNING_TIMEOUT = 600000;
9
+ /** Default number of retries (so 3 attempts in total). */
10
+ export declare const DEFAULT_MAX_RETRIES = 2;
11
+ /** Default idle timeout of a stream, in milliseconds. */
12
+ export declare const DEFAULT_STREAM_IDLE_TIMEOUT = 120000;
13
+ /** A server-requested delay above this many seconds is not slept: the error is raised instead. */
14
+ export declare const MAX_RETRY_AFTER_SECONDS = 60;
15
+ /**
16
+ * The longest delay a platform timer supports, in milliseconds (2^31 - 1, about 24.8 days).
17
+ * A timeout above it, `Infinity` included, means "no time limit": no timer is started.
18
+ */
19
+ export declare const MAX_TIMER_MS = 2147483647;
20
+ /** The `fetch` function the client uses; pass your own to add a proxy, tracing or a mock. */
21
+ export type Fetch = (input: string, init: RequestInit) => Promise<Response>;
22
+ /** What the optional `logger` receives. It never contains credentials, header values or hook tokens. */
23
+ export interface LogEvent {
24
+ kind: 'request' | 'response' | 'retry' | 'error';
25
+ method: string;
26
+ /** The path template, e.g. `/v1/companies/{co}`; parameter values are not included. */
27
+ route: string;
28
+ /** 1 for the first attempt. */
29
+ attempt: number;
30
+ status?: number;
31
+ durationMs?: number;
32
+ requestId?: string | null;
33
+ /** For `retry`: how long the client waits before the next attempt. */
34
+ delayMs?: number;
35
+ error?: TungoError;
36
+ }
37
+ /** Client configuration. Every field is optional. */
38
+ export interface ClientOptions {
39
+ /** API key (`tg_...`). Default: the `TUNGO_API_KEY` environment variable. */
40
+ apiKey?: string | undefined;
41
+ /** Base URL of the API. Default: `TUNGO_BASE_URL`, else `https://api.tungo.kz`. */
42
+ baseUrl?: string | undefined;
43
+ /** Webhook signing secret (`whsec_...`) used by `verifyWebhook`. Default: `TUNGO_WEBHOOK_SECRET`. */
44
+ webhookSecret?: string | undefined;
45
+ /**
46
+ * Timeout of one request in milliseconds, including reading the body. Default: 60 000.
47
+ * It must be greater than zero (0 is rejected, it does not mean "no timeout"); `Infinity`, or
48
+ * any value above {@link MAX_TIMER_MS}, disables the time limit. It does not apply to
49
+ * non-streaming chat turns, which have their own `longRunningTimeout`.
50
+ */
51
+ timeout?: number | undefined;
52
+ /** Timeout of a non-streaming chat turn in milliseconds, with the same rules as `timeout`. Default: 600 000. */
53
+ longRunningTimeout?: number | undefined;
54
+ /** How many times a failed request is retried when repeating it is safe. Default: 2. */
55
+ maxRetries?: number | undefined;
56
+ /**
57
+ * Milliseconds a stream may stay silent before it fails with a TimeoutError. Default: 120 000.
58
+ * 0, `Infinity` or a value above {@link MAX_TIMER_MS} disables the check.
59
+ */
60
+ streamIdleTimeout?: number | undefined;
61
+ /** Headers added to every request. */
62
+ defaultHeaders?: Record<string, string> | undefined;
63
+ /** Custom `fetch` implementation. Default: the global `fetch`. */
64
+ fetch?: Fetch | undefined;
65
+ /** How the API key is sent: the `x-api-key` header (default) or `Authorization: Bearer`. */
66
+ authScheme?: 'x-api-key' | 'bearer' | undefined;
67
+ /** Receives one event per request, response, retry and failure. */
68
+ logger?: ((event: LogEvent) => void) | undefined;
69
+ }
70
+ /** Options accepted by every method as its last argument. */
71
+ export interface RequestOptions {
72
+ /**
73
+ * Timeout of this request in milliseconds (for a stream: the time allowed to connect). It wins
74
+ * over both client timeouts. Greater than zero; `Infinity` disables the time limit.
75
+ */
76
+ timeout?: number | undefined;
77
+ /** Retries for this request. */
78
+ maxRetries?: number | undefined;
79
+ /** Extra headers; a null value removes a default header. */
80
+ headers?: Record<string, string | null | undefined> | undefined;
81
+ /** Your own `Idempotency-Key`. Without it the SDK generates one for operations that declare the header. */
82
+ idempotencyKey?: string | undefined;
83
+ /** Cancels the request (and its retries and waits). It is combined with the timeout. */
84
+ signal?: AbortSignal | undefined;
85
+ /** For streams: milliseconds without data before the stream fails. */
86
+ streamIdleTimeout?: number | undefined;
87
+ }
88
+ /** Description of one HTTP call. The generated resource methods build these. */
89
+ export interface RequestSpec {
90
+ method: string;
91
+ /** Path template such as `/v1/companies/{co}`. */
92
+ path: string;
93
+ /** Values of the path parameters; each is validated and percent-encoded as one segment. */
94
+ pathParams?: Record<string, string> | undefined;
95
+ /** Query parameters; null and undefined values are omitted, arrays repeat the key. */
96
+ query?: object | undefined;
97
+ /** JSON body. */
98
+ body?: unknown;
99
+ /** Exact bytes to send instead of a JSON body. */
100
+ rawBody?: Uint8Array | string | undefined;
101
+ /** Content type of `rawBody`. */
102
+ contentType?: string | undefined;
103
+ /** Operation-specific headers. */
104
+ headers?: Record<string, string | undefined> | undefined;
105
+ /** `key`: send the API key. `public` and `hook`: never send credentials. Default: `key`. */
106
+ auth?: 'key' | 'public' | 'hook' | undefined;
107
+ /** Wire name of the idempotency header when the operation declares one. */
108
+ idempotencyHeader?: string | undefined;
109
+ /** Use the long-running timeout by default. */
110
+ longRunning?: boolean | undefined;
111
+ /** The success response has no body. */
112
+ emptyResult?: boolean | undefined;
113
+ options?: RequestOptions | undefined;
114
+ }
115
+ /** A decoded response together with its status and headers. */
116
+ export interface WithResponse<T> {
117
+ data: T;
118
+ response: ResponseInfo;
119
+ }
120
+ /** Reads an environment variable on Node.js, Bun and Deno; undefined elsewhere or when empty. */
121
+ export declare function readEnv(name: string): string | undefined;
122
+ /** A random RFC 4122 version 4 UUID from the platform CSPRNG. */
123
+ export declare function uuid4(): string;
124
+ /**
125
+ * Percent-encodes a value as ONE path segment (`/`, `?`, `#`, spaces, CR/LF and non-ASCII are all
126
+ * encoded). An empty value, `.` and `..` are rejected because they would change the path.
127
+ */
128
+ export declare function encodePathSegment(value: unknown, name?: string): string;
129
+ /** Fills a path template (`/v1/companies/{co}`) with encoded parameter values. */
130
+ export declare function buildPath(template: string, params: Record<string, string> | undefined): string;
131
+ /**
132
+ * Builds a query string (without the leading `?`): booleans as `true`/`false`, dates as RFC 3339,
133
+ * arrays as repeated keys, null and undefined omitted, `+` encoded as `%2B`.
134
+ */
135
+ export declare function buildQuery(query: object | undefined): string;
136
+ /** Delay before retry number `attempt` (0-based), in milliseconds: min(0.5 s * 2^attempt, 8 s) * [0.75, 1]. */
137
+ export declare function backoffDelay(attempt: number, random?: () => number): number;
138
+ /** Statuses after which a request may be repeated. */
139
+ export declare function isRetryableStatus(status: number): boolean;
140
+ /**
141
+ * The retry rule: the failure must be transient (a retryable status, a connection error or a
142
+ * timeout) AND repeating the request must be safe (GET, HEAD, PUT, DELETE, or any request that
143
+ * carries an idempotency key).
144
+ */
145
+ export declare function shouldRetry(method: string, hasIdempotencyKey: boolean, cause: number | 'connection_error' | 'timeout'): boolean;
146
+ /**
147
+ * Starts a timer, or none (undefined) when the delay is infinite or longer than the platform can
148
+ * count: `setTimeout` would fire such a timer at once instead of never.
149
+ */
150
+ export declare function startTimer(callback: () => void, ms: number): ReturnType<typeof setTimeout> | undefined;
151
+ /**
152
+ * Waits `ms` milliseconds; rejects with an AbortError as soon as the signal aborts. A delay above
153
+ * {@link MAX_TIMER_MS} waits until the signal aborts.
154
+ */
155
+ export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
156
+ /**
157
+ * A promise of a decoded response that also gives access to the raw response:
158
+ * `await client.companies.get(id)` yields the data, `.withResponse()` adds status, headers,
159
+ * request id and rate-limit information of that very response.
160
+ */
161
+ export declare class APIPromise<T> extends Promise<T> {
162
+ #private;
163
+ constructor(result: Promise<WithResponse<T>>);
164
+ /** The decoded data together with the status, headers, request id and rate limit of the response. */
165
+ withResponse(): Promise<WithResponse<T>>;
166
+ /** Only the status, headers, request id and rate limit of the response. */
167
+ asResponse(): Promise<ResponseInfo>;
168
+ then<A = T, B = never>(onfulfilled?: ((value: T) => A | PromiseLike<A>) | null, onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null): Promise<A | B>;
169
+ catch<B = never>(onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null): Promise<T | B>;
170
+ finally(onfinally?: (() => void) | null): Promise<T>;
171
+ static get [Symbol.species](): PromiseConstructor;
172
+ }
173
+ /**
174
+ * A promise of an open stream that can also be iterated directly:
175
+ * `for await (const event of client.feed.listEvents(co))`. Awaiting it gives the stream object
176
+ * (with `response`, `close()` and, for chat streams, `dialogId` and `turnNo`).
177
+ */
178
+ export declare class StreamPromise<T, S extends Stream<T> = Stream<T>> extends Promise<S> implements AsyncIterable<T> {
179
+ #private;
180
+ constructor(stream: Promise<S>);
181
+ then<A = S, B = never>(onfulfilled?: ((value: S) => A | PromiseLike<A>) | null, onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null): Promise<A | B>;
182
+ catch<B = never>(onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null): Promise<S | B>;
183
+ finally(onfinally?: (() => void) | null): Promise<S>;
184
+ [Symbol.asyncIterator](): AsyncGenerator<T, void, undefined>;
185
+ static get [Symbol.species](): PromiseConstructor;
186
+ }
187
+ /**
188
+ * The HTTP engine shared by all resources: configuration, authentication, retries, timeouts,
189
+ * cancellation and decoding. It is immutable after construction and safe to share.
190
+ */
191
+ export declare class APIClient {
192
+ #private;
193
+ /** Base URL without a trailing slash. */
194
+ readonly baseUrl: string;
195
+ /** Default request timeout in milliseconds. */
196
+ readonly timeout: number;
197
+ /** Default timeout of non-streaming chat turns in milliseconds. */
198
+ readonly longRunningTimeout: number;
199
+ /** Default number of retries. */
200
+ readonly maxRetries: number;
201
+ /** Default idle timeout of streams in milliseconds. */
202
+ readonly streamIdleTimeout: number;
203
+ constructor(options?: ClientOptions);
204
+ /** The webhook secret to use: the explicit one, else the client's; a ConfigError when there is none. */
205
+ webhookSecret(explicit?: string): string;
206
+ /** Performs a request and decodes the JSON response. */
207
+ request<T>(spec: RequestSpec): APIPromise<T>;
208
+ /** Opens a server-sent event stream; `make` wraps the connection into the stream object. */
209
+ openStream<S>(spec: RequestSpec, make: (init: StreamInit) => S): Promise<S>;
210
+ }