@colineapp/kairo-core 0.1.0 → 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.
- package/dist/index.d.ts +120 -82
- package/dist/index.js +231 -244
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,134 +1,172 @@
|
|
|
1
1
|
export declare const KAIRO_BASE_URL: "https://coline.app";
|
|
2
2
|
export declare const KAIRO_OAUTH_PATHS: {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
export declare const KAIRO_REQUIRED_SCOPES: readonly [
|
|
3
|
+
readonly authorize: "/api/kairo/oauth/authorize";
|
|
4
|
+
readonly token: "/api/kairo/oauth/token";
|
|
5
|
+
readonly deviceAuthorize: "/api/kairo/oauth/device/authorize";
|
|
6
|
+
readonly revoke: "/api/kairo/oauth/revoke";
|
|
7
|
+
readonly agentStream: "/api/kairo/agent/stream";
|
|
8
|
+
readonly models: "/api/kairo/models";
|
|
9
|
+
};
|
|
10
|
+
export declare const KAIRO_REQUIRED_SCOPES: readonly [
|
|
11
|
+
"kairo.agent:stream",
|
|
12
|
+
"kairo.models:read",
|
|
13
|
+
];
|
|
11
14
|
export declare const KAIRO_DEFAULT_SCOPE: string;
|
|
12
|
-
export declare const KAIRO_MODEL_IDS: readonly [
|
|
15
|
+
export declare const KAIRO_MODEL_IDS: readonly [
|
|
16
|
+
"gpt-5-nano",
|
|
17
|
+
"gpt-5-mini",
|
|
18
|
+
"gpt-5",
|
|
19
|
+
"gpt-5.1",
|
|
20
|
+
"gpt-5.2",
|
|
21
|
+
"claude-opus-4.6",
|
|
22
|
+
"claude-opus-4.5",
|
|
23
|
+
"claude-sonnet-4.5",
|
|
24
|
+
"grok-4-fast-reasoning",
|
|
25
|
+
"grok-4-fast-non-reasoning",
|
|
26
|
+
"deepseek-v3.2",
|
|
27
|
+
"deepseek-v3.2-speciale",
|
|
28
|
+
"kimi-k2-thinking",
|
|
29
|
+
];
|
|
13
30
|
export type KairoModelId = (typeof KAIRO_MODEL_IDS)[number];
|
|
14
31
|
export declare const KAIRO_DEFAULT_MODEL: KairoModelId;
|
|
15
32
|
export type KairoOauthErrorResponse = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
33
|
+
error: string;
|
|
34
|
+
error_description?: string;
|
|
35
|
+
error_uri?: string;
|
|
19
36
|
};
|
|
20
37
|
export type KairoTokenResponse = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
38
|
+
access_token: string;
|
|
39
|
+
token_type: string;
|
|
40
|
+
expires_in: number;
|
|
41
|
+
scope?: string;
|
|
42
|
+
refresh_token?: string;
|
|
26
43
|
};
|
|
27
44
|
export type KairoDeviceAuthorizationResponse = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
45
|
+
device_code: string;
|
|
46
|
+
user_code: string;
|
|
47
|
+
verification_uri: string;
|
|
48
|
+
verification_uri_complete?: string;
|
|
49
|
+
expires_in: number;
|
|
50
|
+
interval?: number;
|
|
51
|
+
scope?: string;
|
|
35
52
|
};
|
|
36
53
|
export type KairoRole = "system" | "user" | "model" | "function";
|
|
37
54
|
export type KairoTextPart = {
|
|
38
|
-
|
|
55
|
+
text: string;
|
|
39
56
|
};
|
|
40
57
|
export type KairoInlineDataPart = {
|
|
41
|
-
|
|
58
|
+
inlineData:
|
|
59
|
+
| {
|
|
42
60
|
mimeType: string;
|
|
43
61
|
data: string;
|
|
44
|
-
|
|
62
|
+
}
|
|
63
|
+
| {
|
|
45
64
|
imageUrl: string;
|
|
46
|
-
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
47
67
|
fileData: string;
|
|
48
68
|
filename?: string;
|
|
49
|
-
|
|
69
|
+
}
|
|
70
|
+
| {
|
|
50
71
|
fileId: string;
|
|
51
72
|
filename?: string;
|
|
52
|
-
|
|
73
|
+
};
|
|
53
74
|
};
|
|
54
75
|
export type KairoFunctionCall = {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
76
|
+
id?: string;
|
|
77
|
+
name: string;
|
|
78
|
+
args: Record<string, unknown>;
|
|
79
|
+
rawArguments?: string;
|
|
59
80
|
};
|
|
60
81
|
export type KairoFunctionCallPart = {
|
|
61
|
-
|
|
82
|
+
functionCall: KairoFunctionCall;
|
|
62
83
|
};
|
|
63
84
|
export type KairoFunctionResponsePart = {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
85
|
+
functionResponse: {
|
|
86
|
+
name: string;
|
|
87
|
+
response: unknown;
|
|
88
|
+
id?: string;
|
|
89
|
+
};
|
|
69
90
|
};
|
|
70
|
-
export type KairoMessagePart =
|
|
91
|
+
export type KairoMessagePart =
|
|
92
|
+
| KairoTextPart
|
|
93
|
+
| KairoInlineDataPart
|
|
94
|
+
| KairoFunctionCallPart
|
|
95
|
+
| KairoFunctionResponsePart;
|
|
71
96
|
export type KairoContent = {
|
|
72
|
-
|
|
73
|
-
|
|
97
|
+
role: KairoRole;
|
|
98
|
+
parts: KairoMessagePart[];
|
|
74
99
|
};
|
|
75
100
|
export type KairoAgentStreamRequest = {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
101
|
+
contents: KairoContent[];
|
|
102
|
+
modelType?: KairoModelId | string;
|
|
103
|
+
reasoningLevel?: number;
|
|
104
|
+
thinkingBudget?: number;
|
|
105
|
+
traceId?: string;
|
|
81
106
|
} & Record<string, unknown>;
|
|
82
107
|
export type KairoOauthErrorDetails = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
108
|
+
code: string;
|
|
109
|
+
description: string | null;
|
|
110
|
+
uri: string | null;
|
|
86
111
|
};
|
|
87
112
|
export type KairoErrorOptions = {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
113
|
+
message: string;
|
|
114
|
+
status: number | null;
|
|
115
|
+
code: string;
|
|
116
|
+
description?: string | null;
|
|
117
|
+
details?: unknown;
|
|
93
118
|
};
|
|
94
119
|
export declare class KairoError extends Error {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
120
|
+
readonly status: number | null;
|
|
121
|
+
readonly code: string;
|
|
122
|
+
readonly description: string | null;
|
|
123
|
+
readonly details: unknown;
|
|
124
|
+
constructor(options: KairoErrorOptions);
|
|
100
125
|
}
|
|
101
126
|
export declare function resolveBaseUrl(baseUrl?: string): string;
|
|
102
|
-
export declare function buildKairoUrl(
|
|
127
|
+
export declare function buildKairoUrl(
|
|
128
|
+
baseUrl: string | undefined,
|
|
129
|
+
path: string,
|
|
130
|
+
): string;
|
|
103
131
|
export declare const KAIRO_TOKEN_EXPIRY_SAFETY_MS = 30000;
|
|
104
|
-
export declare function computeExpiresAt(
|
|
132
|
+
export declare function computeExpiresAt(
|
|
133
|
+
expiresInSeconds: number,
|
|
134
|
+
nowMs?: number,
|
|
135
|
+
safetyMs?: number,
|
|
136
|
+
): number;
|
|
105
137
|
export declare function isExpired(expiresAt: number, nowMs?: number): boolean;
|
|
106
138
|
export declare function assertOk(response: Response): Promise<Response>;
|
|
107
139
|
export type KairoSseEvent = {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
};
|
|
113
|
-
export declare function iterateSse(
|
|
140
|
+
raw: string;
|
|
141
|
+
data: unknown;
|
|
142
|
+
event: string | null;
|
|
143
|
+
parsed: boolean;
|
|
144
|
+
};
|
|
145
|
+
export declare function iterateSse(
|
|
146
|
+
response: Response,
|
|
147
|
+
): AsyncGenerator<KairoSseEvent>;
|
|
114
148
|
export declare function extractTextChunks(data: unknown): string[];
|
|
115
|
-
export declare function accumulateText(
|
|
149
|
+
export declare function accumulateText(
|
|
150
|
+
events: AsyncIterable<KairoSseEvent>,
|
|
151
|
+
): Promise<string>;
|
|
116
152
|
export type KairoTokenSet = {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
153
|
+
accessToken: string;
|
|
154
|
+
refreshToken: string | null;
|
|
155
|
+
expiresAt: number;
|
|
156
|
+
scope: string | null;
|
|
157
|
+
tokenType: string;
|
|
122
158
|
};
|
|
123
159
|
export declare function toTokenSet(response: KairoTokenResponse): KairoTokenSet;
|
|
124
160
|
export type KairoModelListOptions = {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
161
|
+
baseUrl?: string;
|
|
162
|
+
fetch?: typeof fetch;
|
|
163
|
+
signal?: AbortSignal;
|
|
128
164
|
};
|
|
129
165
|
export type KairoModelListResponse = {
|
|
130
|
-
|
|
131
|
-
|
|
166
|
+
defaultModel: string;
|
|
167
|
+
models: string[];
|
|
132
168
|
};
|
|
133
|
-
export declare function listKairoModels(
|
|
134
|
-
|
|
169
|
+
export declare function listKairoModels(
|
|
170
|
+
options?: KairoModelListOptions,
|
|
171
|
+
): Promise<KairoModelListResponse>;
|
|
172
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,295 +1,282 @@
|
|
|
1
|
-
import { createParser
|
|
1
|
+
import { createParser } from "eventsource-parser";
|
|
2
2
|
export const KAIRO_BASE_URL = "https://coline.app";
|
|
3
3
|
export const KAIRO_OAUTH_PATHS = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
authorize: "/api/kairo/oauth/authorize",
|
|
5
|
+
token: "/api/kairo/oauth/token",
|
|
6
|
+
deviceAuthorize: "/api/kairo/oauth/device/authorize",
|
|
7
|
+
revoke: "/api/kairo/oauth/revoke",
|
|
8
|
+
agentStream: "/api/kairo/agent/stream",
|
|
9
|
+
models: "/api/kairo/models",
|
|
10
10
|
};
|
|
11
11
|
export const KAIRO_REQUIRED_SCOPES = [
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
"kairo.agent:stream",
|
|
13
|
+
"kairo.models:read",
|
|
14
14
|
];
|
|
15
15
|
export const KAIRO_DEFAULT_SCOPE = KAIRO_REQUIRED_SCOPES.join(" ");
|
|
16
16
|
export const KAIRO_MODEL_IDS = [
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
"gpt-5-nano",
|
|
18
|
+
"gpt-5-mini",
|
|
19
|
+
"gpt-5",
|
|
20
|
+
"gpt-5.1",
|
|
21
|
+
"gpt-5.2",
|
|
22
|
+
"claude-opus-4.6",
|
|
23
|
+
"claude-opus-4.5",
|
|
24
|
+
"claude-sonnet-4.5",
|
|
25
|
+
"grok-4-fast-reasoning",
|
|
26
|
+
"grok-4-fast-non-reasoning",
|
|
27
|
+
"deepseek-v3.2",
|
|
28
|
+
"deepseek-v3.2-speciale",
|
|
29
|
+
"kimi-k2-thinking",
|
|
27
30
|
];
|
|
28
31
|
export const KAIRO_DEFAULT_MODEL = "gpt-5.2";
|
|
29
32
|
export class KairoError extends Error {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
status;
|
|
34
|
+
code;
|
|
35
|
+
description;
|
|
36
|
+
details;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
super(options.message);
|
|
39
|
+
this.name = "KairoError";
|
|
40
|
+
this.status = options.status;
|
|
41
|
+
this.code = options.code;
|
|
42
|
+
this.description = options.description ?? null;
|
|
43
|
+
this.details = options.details ?? null;
|
|
44
|
+
}
|
|
42
45
|
}
|
|
43
46
|
function isRecord(value) {
|
|
44
|
-
|
|
47
|
+
return typeof value === "object" && value !== null;
|
|
45
48
|
}
|
|
46
49
|
function readOauthErrorDetails(value) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return { code, description, uri };
|
|
50
|
+
if (!isRecord(value)) return null;
|
|
51
|
+
const code = typeof value.error === "string" ? value.error : null;
|
|
52
|
+
if (!code) return null;
|
|
53
|
+
const description =
|
|
54
|
+
typeof value.error_description === "string"
|
|
55
|
+
? value.error_description
|
|
56
|
+
: null;
|
|
57
|
+
const uri = typeof value.error_uri === "string" ? value.error_uri : null;
|
|
58
|
+
return { code, description, uri };
|
|
57
59
|
}
|
|
58
60
|
export function resolveBaseUrl(baseUrl) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return trimmed.length > 0 ? trimmed : KAIRO_BASE_URL;
|
|
61
|
+
if (!baseUrl) return KAIRO_BASE_URL;
|
|
62
|
+
const trimmed = baseUrl.trim();
|
|
63
|
+
return trimmed.length > 0 ? trimmed : KAIRO_BASE_URL;
|
|
63
64
|
}
|
|
64
65
|
function resolveFetch(override) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
66
|
+
const fetchImpl = override ?? globalThis.fetch;
|
|
67
|
+
if (!fetchImpl) {
|
|
68
|
+
throw new KairoError({
|
|
69
|
+
message: "No fetch implementation is available",
|
|
70
|
+
status: null,
|
|
71
|
+
code: "missing_fetch",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return fetchImpl;
|
|
74
75
|
}
|
|
75
76
|
export function buildKairoUrl(baseUrl, path) {
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
const origin = resolveBaseUrl(baseUrl);
|
|
78
|
+
return new URL(path, origin).toString();
|
|
78
79
|
}
|
|
79
80
|
export const KAIRO_TOKEN_EXPIRY_SAFETY_MS = 30_000;
|
|
80
|
-
export function computeExpiresAt(
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
export function computeExpiresAt(
|
|
82
|
+
expiresInSeconds,
|
|
83
|
+
nowMs = Date.now(),
|
|
84
|
+
safetyMs = KAIRO_TOKEN_EXPIRY_SAFETY_MS,
|
|
85
|
+
) {
|
|
86
|
+
const ttlMs = Math.max(0, expiresInSeconds * 1000 - safetyMs);
|
|
87
|
+
return nowMs + ttlMs;
|
|
83
88
|
}
|
|
84
89
|
export function isExpired(expiresAt, nowMs = Date.now()) {
|
|
85
|
-
|
|
90
|
+
return expiresAt <= nowMs;
|
|
86
91
|
}
|
|
87
92
|
export async function assertOk(response) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
details: payload,
|
|
109
|
-
});
|
|
93
|
+
if (response.ok) return response;
|
|
94
|
+
let payload = null;
|
|
95
|
+
try {
|
|
96
|
+
payload = await response.clone().json();
|
|
97
|
+
} catch {
|
|
98
|
+
payload = null;
|
|
99
|
+
}
|
|
100
|
+
const oauthError = readOauthErrorDetails(payload);
|
|
101
|
+
const message = oauthError?.description
|
|
102
|
+
? oauthError.description
|
|
103
|
+
: oauthError?.code
|
|
104
|
+
? oauthError.code
|
|
105
|
+
: `Request failed with status ${response.status}`;
|
|
106
|
+
throw new KairoError({
|
|
107
|
+
message,
|
|
108
|
+
status: response.status,
|
|
109
|
+
code: oauthError?.code ?? "http_error",
|
|
110
|
+
description: oauthError?.description ?? null,
|
|
111
|
+
details: payload,
|
|
112
|
+
});
|
|
110
113
|
}
|
|
111
114
|
function safeParseJson(raw) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
115
|
+
try {
|
|
116
|
+
return {
|
|
117
|
+
parsed: true,
|
|
118
|
+
value: JSON.parse(raw),
|
|
119
|
+
};
|
|
120
|
+
} catch {
|
|
121
|
+
return {
|
|
122
|
+
parsed: false,
|
|
123
|
+
value: raw,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
124
126
|
}
|
|
125
127
|
function deriveEventName(parsed, fallback) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
if (isRecord(parsed) && typeof parsed.event === "string") {
|
|
129
|
+
return parsed.event;
|
|
130
|
+
}
|
|
131
|
+
return fallback ?? null;
|
|
130
132
|
}
|
|
131
133
|
export async function* iterateSse(response) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
if (!response.body) {
|
|
135
|
+
throw new KairoError({
|
|
136
|
+
message: "Streaming response has no body",
|
|
137
|
+
status: response.status,
|
|
138
|
+
code: "missing_stream_body",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const reader = response.body.getReader();
|
|
142
|
+
const decoder = new TextDecoder();
|
|
143
|
+
const queue = [];
|
|
144
|
+
let pendingResolve = null;
|
|
145
|
+
let done = false;
|
|
146
|
+
let pumpError = null;
|
|
147
|
+
const push = (event) => {
|
|
148
|
+
if (pendingResolve) {
|
|
149
|
+
const resolve = pendingResolve;
|
|
150
|
+
pendingResolve = null;
|
|
151
|
+
resolve(event);
|
|
152
|
+
return;
|
|
138
153
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
queue.push(event);
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
done = true;
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
const onParseEvent = (event) => {
|
|
160
|
-
const parsed = safeParseJson(event.data);
|
|
161
|
-
const eventName = deriveEventName(parsed.value, event.event);
|
|
162
|
-
push({
|
|
163
|
-
raw: event.data,
|
|
164
|
-
data: parsed.value,
|
|
165
|
-
event: eventName,
|
|
166
|
-
parsed: parsed.parsed,
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
const parser = createParser({
|
|
170
|
-
onEvent: onParseEvent,
|
|
171
|
-
onError: (_error) => {
|
|
172
|
-
},
|
|
154
|
+
if (event) {
|
|
155
|
+
queue.push(event);
|
|
156
|
+
} else {
|
|
157
|
+
done = true;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const onParseEvent = (event) => {
|
|
161
|
+
const parsed = safeParseJson(event.data);
|
|
162
|
+
const eventName = deriveEventName(parsed.value, event.event);
|
|
163
|
+
push({
|
|
164
|
+
raw: event.data,
|
|
165
|
+
data: parsed.value,
|
|
166
|
+
event: eventName,
|
|
167
|
+
parsed: parsed.parsed,
|
|
173
168
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (!value)
|
|
181
|
-
continue;
|
|
182
|
-
parser.feed(decoder.decode(value, { stream: true }));
|
|
183
|
-
}
|
|
184
|
-
const tail = decoder.decode();
|
|
185
|
-
if (tail) {
|
|
186
|
-
parser.feed(tail);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
catch (error) {
|
|
190
|
-
pumpError = error;
|
|
191
|
-
}
|
|
192
|
-
finally {
|
|
193
|
-
try {
|
|
194
|
-
reader.releaseLock();
|
|
195
|
-
}
|
|
196
|
-
catch {
|
|
197
|
-
}
|
|
198
|
-
push(null);
|
|
199
|
-
}
|
|
200
|
-
})();
|
|
169
|
+
};
|
|
170
|
+
const parser = createParser({
|
|
171
|
+
onEvent: onParseEvent,
|
|
172
|
+
onError: (_error) => {},
|
|
173
|
+
});
|
|
174
|
+
const pump = (async () => {
|
|
201
175
|
try {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
finally {
|
|
221
|
-
await pump;
|
|
176
|
+
while (true) {
|
|
177
|
+
const { done: readerDone, value } = await reader.read();
|
|
178
|
+
if (readerDone) break;
|
|
179
|
+
if (!value) continue;
|
|
180
|
+
parser.feed(decoder.decode(value, { stream: true }));
|
|
181
|
+
}
|
|
182
|
+
const tail = decoder.decode();
|
|
183
|
+
if (tail) {
|
|
184
|
+
parser.feed(tail);
|
|
185
|
+
}
|
|
186
|
+
} catch (error) {
|
|
187
|
+
pumpError = error;
|
|
188
|
+
} finally {
|
|
189
|
+
try {
|
|
190
|
+
reader.releaseLock();
|
|
191
|
+
} catch {}
|
|
192
|
+
push(null);
|
|
222
193
|
}
|
|
223
|
-
|
|
224
|
-
|
|
194
|
+
})();
|
|
195
|
+
try {
|
|
196
|
+
while (true) {
|
|
197
|
+
if (queue.length > 0) {
|
|
198
|
+
const next = queue.shift();
|
|
199
|
+
if (next) {
|
|
200
|
+
yield next;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (done) break;
|
|
205
|
+
const awaited = await new Promise((resolve) => {
|
|
206
|
+
pendingResolve = resolve;
|
|
207
|
+
});
|
|
208
|
+
if (!awaited) break;
|
|
209
|
+
yield awaited;
|
|
225
210
|
}
|
|
211
|
+
} finally {
|
|
212
|
+
await pump;
|
|
213
|
+
}
|
|
214
|
+
if (pumpError) {
|
|
215
|
+
throw pumpError;
|
|
216
|
+
}
|
|
226
217
|
}
|
|
227
218
|
function readPartsFromCandidate(value) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return Array.isArray(parts) ? parts : [];
|
|
219
|
+
if (!isRecord(value)) return [];
|
|
220
|
+
const parts = value.parts;
|
|
221
|
+
return Array.isArray(parts) ? parts : [];
|
|
232
222
|
}
|
|
233
223
|
function extractTextFromPart(part) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
return [];
|
|
224
|
+
if (!isRecord(part)) return [];
|
|
225
|
+
if (typeof part.text === "string" && part.text.length > 0) {
|
|
226
|
+
return [part.text];
|
|
227
|
+
}
|
|
228
|
+
const type = typeof part.type === "string" ? part.type : null;
|
|
229
|
+
if (!type) return [];
|
|
230
|
+
if (type === "output_text" || type === "input_text") {
|
|
231
|
+
return typeof part.text === "string" && part.text.length > 0
|
|
232
|
+
? [part.text]
|
|
233
|
+
: [];
|
|
234
|
+
}
|
|
235
|
+
return [];
|
|
248
236
|
}
|
|
249
237
|
export function extractTextChunks(data) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
return chunks;
|
|
238
|
+
if (!isRecord(data)) return [];
|
|
239
|
+
const chunks = [];
|
|
240
|
+
if (typeof data.text === "string" && data.text.length > 0) {
|
|
241
|
+
chunks.push(data.text);
|
|
242
|
+
}
|
|
243
|
+
const candidate = data.candidate_content;
|
|
244
|
+
const parts = readPartsFromCandidate(candidate);
|
|
245
|
+
for (const part of parts) {
|
|
246
|
+
chunks.push(...extractTextFromPart(part));
|
|
247
|
+
}
|
|
248
|
+
return chunks;
|
|
262
249
|
}
|
|
263
250
|
export async function accumulateText(events) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}
|
|
251
|
+
let text = "";
|
|
252
|
+
for await (const event of events) {
|
|
253
|
+
const chunks = extractTextChunks(event.data);
|
|
254
|
+
for (const chunk of chunks) {
|
|
255
|
+
text += chunk;
|
|
270
256
|
}
|
|
271
|
-
|
|
257
|
+
}
|
|
258
|
+
return text;
|
|
272
259
|
}
|
|
273
260
|
export function toTokenSet(response) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
261
|
+
return {
|
|
262
|
+
accessToken: response.access_token,
|
|
263
|
+
refreshToken: response.refresh_token ?? null,
|
|
264
|
+
expiresAt: computeExpiresAt(response.expires_in),
|
|
265
|
+
scope: response.scope ?? null,
|
|
266
|
+
tokenType: response.token_type,
|
|
267
|
+
};
|
|
281
268
|
}
|
|
282
269
|
export async function listKairoModels(options = {}) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
270
|
+
const fetchImpl = resolveFetch(options.fetch);
|
|
271
|
+
const url = buildKairoUrl(options.baseUrl, KAIRO_OAUTH_PATHS.models);
|
|
272
|
+
const response = await fetchImpl(url, {
|
|
273
|
+
method: "GET",
|
|
274
|
+
headers: {
|
|
275
|
+
Accept: "application/json",
|
|
276
|
+
},
|
|
277
|
+
signal: options.signal,
|
|
278
|
+
});
|
|
279
|
+
await assertOk(response);
|
|
280
|
+
return await response.json();
|
|
294
281
|
}
|
|
295
|
-
//# sourceMappingURL=index.js.map
|
|
282
|
+
//# sourceMappingURL=index.js.map
|