@tambo-ai/typescript-sdk 0.83.0 → 0.84.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 (71) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/core/streaming.d.mts +33 -0
  3. package/core/streaming.d.mts.map +1 -0
  4. package/core/streaming.d.ts +33 -0
  5. package/core/streaming.d.ts.map +1 -0
  6. package/core/streaming.js +263 -0
  7. package/core/streaming.js.map +1 -0
  8. package/core/streaming.mjs +258 -0
  9. package/core/streaming.mjs.map +1 -0
  10. package/internal/decoders/line.d.mts +17 -0
  11. package/internal/decoders/line.d.mts.map +1 -0
  12. package/internal/decoders/line.d.ts +17 -0
  13. package/internal/decoders/line.d.ts.map +1 -0
  14. package/internal/decoders/line.js +113 -0
  15. package/internal/decoders/line.js.map +1 -0
  16. package/internal/decoders/line.mjs +108 -0
  17. package/internal/decoders/line.mjs.map +1 -0
  18. package/internal/parse.d.mts.map +1 -1
  19. package/internal/parse.d.ts.map +1 -1
  20. package/internal/parse.js +10 -0
  21. package/internal/parse.js.map +1 -1
  22. package/internal/parse.mjs +10 -0
  23. package/internal/parse.mjs.map +1 -1
  24. package/internal/request-options.d.mts +2 -0
  25. package/internal/request-options.d.mts.map +1 -1
  26. package/internal/request-options.d.ts +2 -0
  27. package/internal/request-options.d.ts.map +1 -1
  28. package/internal/request-options.js.map +1 -1
  29. package/internal/request-options.mjs.map +1 -1
  30. package/package.json +11 -1
  31. package/resources/threads/index.d.mts +1 -1
  32. package/resources/threads/index.d.mts.map +1 -1
  33. package/resources/threads/index.d.ts +1 -1
  34. package/resources/threads/index.d.ts.map +1 -1
  35. package/resources/threads/index.js.map +1 -1
  36. package/resources/threads/index.mjs.map +1 -1
  37. package/resources/threads/runs.d.mts +35 -9
  38. package/resources/threads/runs.d.mts.map +1 -1
  39. package/resources/threads/runs.d.ts +35 -9
  40. package/resources/threads/runs.d.ts.map +1 -1
  41. package/resources/threads/runs.js +13 -8
  42. package/resources/threads/runs.js.map +1 -1
  43. package/resources/threads/runs.mjs +13 -8
  44. package/resources/threads/runs.mjs.map +1 -1
  45. package/resources/threads/threads.d.mts +2 -2
  46. package/resources/threads/threads.d.mts.map +1 -1
  47. package/resources/threads/threads.d.ts +2 -2
  48. package/resources/threads/threads.d.ts.map +1 -1
  49. package/resources/threads/threads.js.map +1 -1
  50. package/resources/threads/threads.mjs.map +1 -1
  51. package/src/core/streaming.ts +315 -0
  52. package/src/internal/decoders/line.ts +135 -0
  53. package/src/internal/parse.ts +14 -0
  54. package/src/internal/request-options.ts +2 -0
  55. package/src/resources/threads/index.ts +2 -0
  56. package/src/resources/threads/runs.ts +46 -12
  57. package/src/resources/threads/threads.ts +4 -0
  58. package/src/streaming.ts +2 -0
  59. package/src/version.ts +1 -1
  60. package/streaming.d.mts +2 -0
  61. package/streaming.d.mts.map +1 -0
  62. package/streaming.d.ts +2 -0
  63. package/streaming.d.ts.map +1 -0
  64. package/streaming.js +6 -0
  65. package/streaming.js.map +1 -0
  66. package/streaming.mjs +2 -0
  67. package/streaming.mjs.map +1 -0
  68. package/version.d.mts +1 -1
  69. package/version.d.ts +1 -1
  70. package/version.js +1 -1
  71. package/version.mjs +1 -1
@@ -0,0 +1,135 @@
1
+ import { concatBytes, decodeUTF8, encodeUTF8 } from '../utils/bytes';
2
+
3
+ export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
4
+
5
+ /**
6
+ * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
7
+ * reading lines from text.
8
+ *
9
+ * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
10
+ */
11
+ export class LineDecoder {
12
+ // prettier-ignore
13
+ static NEWLINE_CHARS = new Set(['\n', '\r']);
14
+ static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
15
+
16
+ #buffer: Uint8Array;
17
+ #carriageReturnIndex: number | null;
18
+
19
+ constructor() {
20
+ this.#buffer = new Uint8Array();
21
+ this.#carriageReturnIndex = null;
22
+ }
23
+
24
+ decode(chunk: Bytes): string[] {
25
+ if (chunk == null) {
26
+ return [];
27
+ }
28
+
29
+ const binaryChunk =
30
+ chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
31
+ : typeof chunk === 'string' ? encodeUTF8(chunk)
32
+ : chunk;
33
+
34
+ this.#buffer = concatBytes([this.#buffer, binaryChunk]);
35
+
36
+ const lines: string[] = [];
37
+ let patternIndex;
38
+ while ((patternIndex = findNewlineIndex(this.#buffer, this.#carriageReturnIndex)) != null) {
39
+ if (patternIndex.carriage && this.#carriageReturnIndex == null) {
40
+ // skip until we either get a corresponding `\n`, a new `\r` or nothing
41
+ this.#carriageReturnIndex = patternIndex.index;
42
+ continue;
43
+ }
44
+
45
+ // we got double \r or \rtext\n
46
+ if (
47
+ this.#carriageReturnIndex != null &&
48
+ (patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)
49
+ ) {
50
+ lines.push(decodeUTF8(this.#buffer.subarray(0, this.#carriageReturnIndex - 1)));
51
+ this.#buffer = this.#buffer.subarray(this.#carriageReturnIndex);
52
+ this.#carriageReturnIndex = null;
53
+ continue;
54
+ }
55
+
56
+ const endIndex =
57
+ this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
58
+
59
+ const line = decodeUTF8(this.#buffer.subarray(0, endIndex));
60
+ lines.push(line);
61
+
62
+ this.#buffer = this.#buffer.subarray(patternIndex.index);
63
+ this.#carriageReturnIndex = null;
64
+ }
65
+
66
+ return lines;
67
+ }
68
+
69
+ flush(): string[] {
70
+ if (!this.#buffer.length) {
71
+ return [];
72
+ }
73
+ return this.decode('\n');
74
+ }
75
+ }
76
+
77
+ /**
78
+ * This function searches the buffer for the end patterns, (\r or \n)
79
+ * and returns an object with the index preceding the matched newline and the
80
+ * index after the newline char. `null` is returned if no new line is found.
81
+ *
82
+ * ```ts
83
+ * findNewLineIndex('abc\ndef') -> { preceding: 2, index: 3 }
84
+ * ```
85
+ */
86
+ function findNewlineIndex(
87
+ buffer: Uint8Array,
88
+ startIndex: number | null,
89
+ ): { preceding: number; index: number; carriage: boolean } | null {
90
+ const newline = 0x0a; // \n
91
+ const carriage = 0x0d; // \r
92
+
93
+ for (let i = startIndex ?? 0; i < buffer.length; i++) {
94
+ if (buffer[i] === newline) {
95
+ return { preceding: i, index: i + 1, carriage: false };
96
+ }
97
+
98
+ if (buffer[i] === carriage) {
99
+ return { preceding: i, index: i + 1, carriage: true };
100
+ }
101
+ }
102
+
103
+ return null;
104
+ }
105
+
106
+ export function findDoubleNewlineIndex(buffer: Uint8Array): number {
107
+ // This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
108
+ // and returns the index right after the first occurrence of any pattern,
109
+ // or -1 if none of the patterns are found.
110
+ const newline = 0x0a; // \n
111
+ const carriage = 0x0d; // \r
112
+
113
+ for (let i = 0; i < buffer.length - 1; i++) {
114
+ if (buffer[i] === newline && buffer[i + 1] === newline) {
115
+ // \n\n
116
+ return i + 2;
117
+ }
118
+ if (buffer[i] === carriage && buffer[i + 1] === carriage) {
119
+ // \r\r
120
+ return i + 2;
121
+ }
122
+ if (
123
+ buffer[i] === carriage &&
124
+ buffer[i + 1] === newline &&
125
+ i + 3 < buffer.length &&
126
+ buffer[i + 2] === carriage &&
127
+ buffer[i + 3] === newline
128
+ ) {
129
+ // \r\n\r\n
130
+ return i + 4;
131
+ }
132
+ }
133
+
134
+ return -1;
135
+ }
@@ -1,6 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
3
  import type { FinalRequestOptions } from './request-options';
4
+ import { Stream } from '../core/streaming';
4
5
  import { type TamboAI } from '../client';
5
6
  import { formatRequestDetails, loggerFor } from './utils/log';
6
7
 
@@ -16,6 +17,19 @@ export type APIResponseProps = {
16
17
  export async function defaultParseResponse<T>(client: TamboAI, props: APIResponseProps): Promise<T> {
17
18
  const { response, requestLogID, retryOfRequestLogID, startTime } = props;
18
19
  const body = await (async () => {
20
+ if (props.options.stream) {
21
+ loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);
22
+
23
+ // Note: there is an invariant here that isn't represented in the type system
24
+ // that if you set `stream: true` the response type must also be `Stream<T>`
25
+
26
+ if (props.options.__streamClass) {
27
+ return props.options.__streamClass.fromSSEResponse(response, props.controller, client) as any;
28
+ }
29
+
30
+ return Stream.fromSSEResponse(response, props.controller, client) as any;
31
+ }
32
+
19
33
  // fetch refuses to read the body when the status code is 204.
20
34
  if (response.status === 204) {
21
35
  return null as T;
@@ -3,6 +3,7 @@
3
3
  import { NullableHeaders } from './headers';
4
4
 
5
5
  import type { BodyInit } from './builtin-types';
6
+ import { Stream } from '../core/streaming';
6
7
  import type { HTTPMethod, MergedRequestInit } from './types';
7
8
  import { type HeadersLike } from './headers';
8
9
 
@@ -76,6 +77,7 @@ export type RequestOptions = {
76
77
  defaultBaseURL?: string | undefined;
77
78
 
78
79
  __binaryResponse?: boolean | undefined;
80
+ __streamClass?: typeof Stream;
79
81
  };
80
82
 
81
83
  export type EncodedContent = { bodyHeaders: HeadersLike; body: BodyInit };
@@ -10,7 +10,9 @@ export {
10
10
  export {
11
11
  Runs,
12
12
  type InputMessage,
13
+ type RunCreateResponse,
13
14
  type RunDeleteResponse,
15
+ type RunRunResponse,
14
16
  type RunCreateParams,
15
17
  type RunDeleteParams,
16
18
  type RunRunParams,
@@ -4,6 +4,7 @@ import { APIResource } from '../../core/resource';
4
4
  import * as RunsAPI from './runs';
5
5
  import * as ThreadsAPI from './threads';
6
6
  import { APIPromise } from '../../core/api-promise';
7
+ import { Stream } from '../../core/streaming';
7
8
  import { buildHeaders } from '../../internal/headers';
8
9
  import { RequestOptions } from '../../internal/request-options';
9
10
  import { path } from '../../internal/utils/path';
@@ -15,7 +16,7 @@ export class Runs extends APIResource {
15
16
  *
16
17
  * @example
17
18
  * ```ts
18
- * await client.threads.runs.create({
19
+ * const run = await client.threads.runs.create({
19
20
  * message: {
20
21
  * content: [{ text: 'Hello, world!', type: 'text' }],
21
22
  * role: 'user',
@@ -23,12 +24,13 @@ export class Runs extends APIResource {
23
24
  * });
24
25
  * ```
25
26
  */
26
- create(body: RunCreateParams, options?: RequestOptions): APIPromise<void> {
27
+ create(body: RunCreateParams, options?: RequestOptions): APIPromise<Stream<RunCreateResponse>> {
27
28
  return this._client.post('/v1/threads/runs', {
28
29
  body,
29
30
  ...options,
30
- headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),
31
- });
31
+ headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
32
+ stream: true,
33
+ }) as APIPromise<Stream<RunCreateResponse>>;
32
34
  }
33
35
 
34
36
  /**
@@ -54,20 +56,24 @@ export class Runs extends APIResource {
54
56
  *
55
57
  * @example
56
58
  * ```ts
57
- * await client.threads.runs.run('thr_abc123xyz', {
58
- * message: {
59
- * content: [{ text: 'Hello, world!', type: 'text' }],
60
- * role: 'user',
59
+ * const response = await client.threads.runs.run(
60
+ * 'thr_abc123xyz',
61
+ * {
62
+ * message: {
63
+ * content: [{ text: 'Hello, world!', type: 'text' }],
64
+ * role: 'user',
65
+ * },
61
66
  * },
62
- * });
67
+ * );
63
68
  * ```
64
69
  */
65
- run(threadID: string, body: RunRunParams, options?: RequestOptions): APIPromise<void> {
70
+ run(threadID: string, body: RunRunParams, options?: RequestOptions): APIPromise<Stream<RunRunResponse>> {
66
71
  return this._client.post(path`/v1/threads/${threadID}/runs`, {
67
72
  body,
68
73
  ...options,
69
- headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),
70
- });
74
+ headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
75
+ stream: true,
76
+ }) as APIPromise<Stream<RunRunResponse>>;
71
77
  }
72
78
  }
73
79
 
@@ -88,6 +94,19 @@ export interface InputMessage {
88
94
  metadata?: unknown;
89
95
  }
90
96
 
97
+ export interface RunCreateResponse {
98
+ /**
99
+ * Event type discriminator (e.g., RUN_STARTED, TEXT_MESSAGE_CONTENT,
100
+ * TOOL_CALL_START)
101
+ */
102
+ type: string;
103
+
104
+ /**
105
+ * Unix timestamp (milliseconds) when event was generated
106
+ */
107
+ timestamp?: number;
108
+ }
109
+
91
110
  export interface RunDeleteResponse {
92
111
  /**
93
112
  * The run ID that was cancelled
@@ -100,6 +119,19 @@ export interface RunDeleteResponse {
100
119
  status: 'cancelled';
101
120
  }
102
121
 
122
+ export interface RunRunResponse {
123
+ /**
124
+ * Event type discriminator (e.g., RUN_STARTED, TEXT_MESSAGE_CONTENT,
125
+ * TOOL_CALL_START)
126
+ */
127
+ type: string;
128
+
129
+ /**
130
+ * Unix timestamp (milliseconds) when event was generated
131
+ */
132
+ timestamp?: number;
133
+ }
134
+
103
135
  export interface RunCreateParams {
104
136
  /**
105
137
  * The user's message
@@ -346,7 +378,9 @@ export namespace RunRunParams {
346
378
  export declare namespace Runs {
347
379
  export {
348
380
  type InputMessage as InputMessage,
381
+ type RunCreateResponse as RunCreateResponse,
349
382
  type RunDeleteResponse as RunDeleteResponse,
383
+ type RunRunResponse as RunRunResponse,
350
384
  type RunCreateParams as RunCreateParams,
351
385
  type RunDeleteParams as RunDeleteParams,
352
386
  type RunRunParams as RunRunParams,
@@ -15,9 +15,11 @@ import * as RunsAPI from './runs';
15
15
  import {
16
16
  InputMessage,
17
17
  RunCreateParams,
18
+ RunCreateResponse,
18
19
  RunDeleteParams,
19
20
  RunDeleteResponse,
20
21
  RunRunParams,
22
+ RunRunResponse,
21
23
  Runs,
22
24
  } from './runs';
23
25
  import { APIPromise } from '../../core/api-promise';
@@ -528,7 +530,9 @@ export declare namespace Threads {
528
530
  export {
529
531
  Runs as Runs,
530
532
  type InputMessage as InputMessage,
533
+ type RunCreateResponse as RunCreateResponse,
531
534
  type RunDeleteResponse as RunDeleteResponse,
535
+ type RunRunResponse as RunRunResponse,
532
536
  type RunCreateParams as RunCreateParams,
533
537
  type RunDeleteParams as RunDeleteParams,
534
538
  type RunRunParams as RunRunParams,
@@ -0,0 +1,2 @@
1
+ /** @deprecated Import from ./core/streaming instead */
2
+ export * from './core/streaming';
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.83.0'; // x-release-please-version
1
+ export const VERSION = '0.84.0'; // x-release-please-version
@@ -0,0 +1,2 @@
1
+ export * from "./core/streaming.mjs";
2
+ //# sourceMappingURL=streaming.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.d.mts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
package/streaming.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./core/streaming.js";
2
+ //# sourceMappingURL=streaming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
package/streaming.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("./internal/tslib.js");
4
+ /** @deprecated Import from ./core/streaming instead */
5
+ tslib_1.__exportStar(require("./core/streaming.js"), exports);
6
+ //# sourceMappingURL=streaming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.js","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";;;AAAA,uDAAuD;AACvD,8DAAiC"}
package/streaming.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./core/streaming.mjs";
2
+ //# sourceMappingURL=streaming.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.mjs","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":""}
package/version.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.83.0";
1
+ export declare const VERSION = "0.84.0";
2
2
  //# sourceMappingURL=version.d.mts.map
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.83.0";
1
+ export declare const VERSION = "0.84.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '0.83.0'; // x-release-please-version
4
+ exports.VERSION = '0.84.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.83.0'; // x-release-please-version
1
+ export const VERSION = '0.84.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map