@hintergrund/ui 0.1.6 → 0.1.8

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.
@@ -0,0 +1,248 @@
1
+ import { i as v, r as I, m as M, G as S, c as R, a as L, b as _ } from "./google-shared-Vko-wWPR.js";
2
+ import { y as x, E as G, z as O, D as A } from "./Builder-CHLO8Qn6.js";
3
+ import { b as E, s as N } from "./transform-messages-CkpQp6fJ.js";
4
+ typeof globalThis.process > "u" && (globalThis.process = { env: {}, platform: "browser", version: "", versions: {}, browser: !0, nextTick: function(e) {
5
+ return Promise.resolve().then(e);
6
+ } });
7
+ let P = 0;
8
+ const f = (e, s, n) => {
9
+ const i = new O();
10
+ return (async () => {
11
+ const t = {
12
+ role: "assistant",
13
+ content: [],
14
+ api: "google-generative-ai",
15
+ provider: e.provider,
16
+ model: e.id,
17
+ usage: {
18
+ input: 0,
19
+ output: 0,
20
+ cacheRead: 0,
21
+ cacheWrite: 0,
22
+ totalTokens: 0,
23
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
24
+ },
25
+ stopReason: "stop",
26
+ timestamp: Date.now()
27
+ };
28
+ try {
29
+ const r = n?.apiKey || x(e.provider) || "", c = W(e, r, n?.headers);
30
+ let u = B(e, s, n);
31
+ const y = await n?.onPayload?.(u, e);
32
+ y !== void 0 && (u = y);
33
+ const T = await c.models.generateContentStream(u);
34
+ i.push({ type: "start", partial: t });
35
+ let a = null;
36
+ const b = t.content, l = () => b.length - 1;
37
+ for await (const g of T) {
38
+ t.responseId ||= g.responseId;
39
+ const d = g.candidates?.[0];
40
+ if (d?.content?.parts)
41
+ for (const o of d.content.parts) {
42
+ if (o.text !== void 0) {
43
+ const h = v(o);
44
+ (!a || h && a.type !== "thinking" || !h && a.type !== "text") && (a && (a.type === "text" ? i.push({
45
+ type: "text_end",
46
+ contentIndex: b.length - 1,
47
+ content: a.text,
48
+ partial: t
49
+ }) : i.push({
50
+ type: "thinking_end",
51
+ contentIndex: l(),
52
+ content: a.thinking,
53
+ partial: t
54
+ })), h ? (a = { type: "thinking", thinking: "", thinkingSignature: void 0 }, t.content.push(a), i.push({ type: "thinking_start", contentIndex: l(), partial: t })) : (a = { type: "text", text: "" }, t.content.push(a), i.push({ type: "text_start", contentIndex: l(), partial: t }))), a.type === "thinking" ? (a.thinking += o.text, a.thinkingSignature = I(a.thinkingSignature, o.thoughtSignature), i.push({
55
+ type: "thinking_delta",
56
+ contentIndex: l(),
57
+ delta: o.text,
58
+ partial: t
59
+ })) : (a.text += o.text, a.textSignature = I(a.textSignature, o.thoughtSignature), i.push({
60
+ type: "text_delta",
61
+ contentIndex: l(),
62
+ delta: o.text,
63
+ partial: t
64
+ }));
65
+ }
66
+ if (o.functionCall) {
67
+ a && (a.type === "text" ? i.push({
68
+ type: "text_end",
69
+ contentIndex: l(),
70
+ content: a.text,
71
+ partial: t
72
+ }) : i.push({
73
+ type: "thinking_end",
74
+ contentIndex: l(),
75
+ content: a.thinking,
76
+ partial: t
77
+ }), a = null);
78
+ const h = o.functionCall.id, p = {
79
+ type: "toolCall",
80
+ id: !h || t.content.some((C) => C.type === "toolCall" && C.id === h) ? `${o.functionCall.name}_${Date.now()}_${++P}` : h,
81
+ name: o.functionCall.name || "",
82
+ arguments: o.functionCall.args ?? {},
83
+ ...o.thoughtSignature && { thoughtSignature: o.thoughtSignature }
84
+ };
85
+ t.content.push(p), i.push({ type: "toolcall_start", contentIndex: l(), partial: t }), i.push({
86
+ type: "toolcall_delta",
87
+ contentIndex: l(),
88
+ delta: JSON.stringify(p.arguments),
89
+ partial: t
90
+ }), i.push({ type: "toolcall_end", contentIndex: l(), toolCall: p, partial: t });
91
+ }
92
+ }
93
+ d?.finishReason && (t.stopReason = M(d.finishReason), t.content.some((o) => o.type === "toolCall") && (t.stopReason = "toolUse")), g.usageMetadata && (t.usage = {
94
+ input: (g.usageMetadata.promptTokenCount || 0) - (g.usageMetadata.cachedContentTokenCount || 0),
95
+ output: (g.usageMetadata.candidatesTokenCount || 0) + (g.usageMetadata.thoughtsTokenCount || 0),
96
+ cacheRead: g.usageMetadata.cachedContentTokenCount || 0,
97
+ cacheWrite: 0,
98
+ totalTokens: g.usageMetadata.totalTokenCount || 0,
99
+ cost: {
100
+ input: 0,
101
+ output: 0,
102
+ cacheRead: 0,
103
+ cacheWrite: 0,
104
+ total: 0
105
+ }
106
+ }, A(e, t.usage));
107
+ }
108
+ if (a && (a.type === "text" ? i.push({
109
+ type: "text_end",
110
+ contentIndex: l(),
111
+ content: a.text,
112
+ partial: t
113
+ }) : i.push({
114
+ type: "thinking_end",
115
+ contentIndex: l(),
116
+ content: a.thinking,
117
+ partial: t
118
+ })), n?.signal?.aborted)
119
+ throw new Error("Request was aborted");
120
+ if (t.stopReason === "aborted" || t.stopReason === "error")
121
+ throw new Error("An unknown error occurred");
122
+ i.push({ type: "done", reason: t.stopReason, message: t }), i.end();
123
+ } catch (r) {
124
+ for (const c of t.content)
125
+ "index" in c && delete c.index;
126
+ t.stopReason = n?.signal?.aborted ? "aborted" : "error", t.errorMessage = r instanceof Error ? r.message : JSON.stringify(r), i.push({ type: "error", reason: t.stopReason, error: t }), i.end();
127
+ }
128
+ })(), i;
129
+ }, J = (e, s, n) => {
130
+ const i = n?.apiKey || x(e.provider);
131
+ if (!i)
132
+ throw new Error(`No API key for provider: ${e.provider}`);
133
+ const t = E(e, n, i);
134
+ if (!n?.reasoning)
135
+ return f(e, s, { ...t, thinking: { enabled: !1 } });
136
+ const r = G(e, n.reasoning), c = r === "off" ? "high" : r, u = e;
137
+ return m(u) || w(u) || k(u) ? f(e, s, {
138
+ ...t,
139
+ thinking: {
140
+ enabled: !0,
141
+ level: H(c, u)
142
+ }
143
+ }) : f(e, s, {
144
+ ...t,
145
+ thinking: {
146
+ enabled: !0,
147
+ budgetTokens: K(u, c, n.thinkingBudgets)
148
+ }
149
+ });
150
+ };
151
+ function W(e, s, n) {
152
+ const i = {};
153
+ return e.baseUrl && (i.baseUrl = e.baseUrl, i.apiVersion = ""), (e.headers || n) && (i.headers = { ...e.headers, ...n }), new S({
154
+ apiKey: s,
155
+ httpOptions: Object.keys(i).length > 0 ? i : void 0
156
+ });
157
+ }
158
+ function B(e, s, n = {}) {
159
+ const i = R(e, s), t = {};
160
+ n.temperature !== void 0 && (t.temperature = n.temperature), n.maxTokens !== void 0 && (t.maxOutputTokens = n.maxTokens);
161
+ const r = {
162
+ ...Object.keys(t).length > 0 && t,
163
+ ...s.systemPrompt && { systemInstruction: N(s.systemPrompt) },
164
+ ...s.tools && s.tools.length > 0 && { tools: L(s.tools) }
165
+ };
166
+ if (s.tools && s.tools.length > 0 && n.toolChoice ? r.toolConfig = {
167
+ functionCallingConfig: {
168
+ mode: _(n.toolChoice)
169
+ }
170
+ } : r.toolConfig = void 0, n.thinking?.enabled && e.reasoning) {
171
+ const u = { includeThoughts: !0 };
172
+ n.thinking.level !== void 0 ? u.thinkingLevel = n.thinking.level : n.thinking.budgetTokens !== void 0 && (u.thinkingBudget = n.thinking.budgetTokens), r.thinkingConfig = u;
173
+ } else e.reasoning && n.thinking && !n.thinking.enabled && (r.thinkingConfig = D(e));
174
+ if (n.signal) {
175
+ if (n.signal.aborted)
176
+ throw new Error("Request aborted");
177
+ r.abortSignal = n.signal;
178
+ }
179
+ return {
180
+ model: e.id,
181
+ contents: i,
182
+ config: r
183
+ };
184
+ }
185
+ function k(e) {
186
+ return /gemma-?4/.test(e.id.toLowerCase());
187
+ }
188
+ function m(e) {
189
+ return /gemini-3(?:\.\d+)?-pro/.test(e.id.toLowerCase());
190
+ }
191
+ function w(e) {
192
+ return /gemini-3(?:\.\d+)?-flash/.test(e.id.toLowerCase());
193
+ }
194
+ function D(e) {
195
+ return m(e) ? { thinkingLevel: "LOW" } : w(e) ? { thinkingLevel: "MINIMAL" } : k(e) ? { thinkingLevel: "MINIMAL" } : { thinkingBudget: 0 };
196
+ }
197
+ function H(e, s) {
198
+ if (m(s))
199
+ switch (e) {
200
+ case "minimal":
201
+ case "low":
202
+ return "LOW";
203
+ case "medium":
204
+ case "high":
205
+ return "HIGH";
206
+ }
207
+ if (k(s))
208
+ switch (e) {
209
+ case "minimal":
210
+ case "low":
211
+ return "MINIMAL";
212
+ case "medium":
213
+ case "high":
214
+ return "HIGH";
215
+ }
216
+ switch (e) {
217
+ case "minimal":
218
+ return "MINIMAL";
219
+ case "low":
220
+ return "LOW";
221
+ case "medium":
222
+ return "MEDIUM";
223
+ case "high":
224
+ return "HIGH";
225
+ }
226
+ }
227
+ function K(e, s, n) {
228
+ return n?.[s] !== void 0 ? n[s] : e.id.includes("2.5-pro") ? {
229
+ minimal: 128,
230
+ low: 2048,
231
+ medium: 8192,
232
+ high: 32768
233
+ }[s] : e.id.includes("2.5-flash-lite") ? {
234
+ minimal: 512,
235
+ low: 2048,
236
+ medium: 8192,
237
+ high: 24576
238
+ }[s] : e.id.includes("2.5-flash") ? {
239
+ minimal: 128,
240
+ low: 2048,
241
+ medium: 8192,
242
+ high: 24576
243
+ }[s] : -1;
244
+ }
245
+ export {
246
+ f as streamGoogle,
247
+ J as streamSimpleGoogle
248
+ };