prompt-api-polyfill 1.14.0 → 1.14.2

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.
@@ -1,74 +1,56 @@
1
- import { GoogleGenAI as s } from "@google/genai";
2
- import { P as a, D as i } from "../chunks/defaults-CzvdT-At.js";
3
- class l extends a {
4
- #n;
5
- #t;
6
- #e;
7
- constructor(t) {
8
- super(t.modelName || i.gemini.modelName), this.#n = new s({ apiKey: t.apiKey });
9
- }
10
- /**
11
- * Creates a model session.
12
- * @param {Object} options - LanguageModel options.
13
- * @param {Object} sessionParams - Session parameters.
14
- * @returns {Object} The session object.
15
- */
16
- createSession(t, e) {
17
- return this.#e = e, this.#t = t.modelName || this.modelName, { model: this.#t, params: e };
18
- }
19
- /**
20
- * Generates content (non-streaming).
21
- * @param {Array} contents - The history + new message content.
22
- * @returns {Promise<{text: string, usage: number}>}
23
- */
24
- async generateContent(t) {
25
- const e = {
26
- systemInstruction: this.#e.systemInstruction,
27
- ...this.#e.generationConfig
28
- }, n = await this.#n.models.generateContent({
29
- model: this.#t,
30
- contents: t,
31
- config: e
32
- }), o = n.usageMetadata?.promptTokenCount || 0;
33
- return { text: n.text, usage: o };
34
- }
35
- /**
36
- * Generates content stream.
37
- * @param {Array} contents - The history + new content.
38
- * @returns {Promise<AsyncIterable>} Stream of chunks.
39
- */
40
- async generateContentStream(t) {
41
- const e = {
42
- systemInstruction: this.#e.systemInstruction,
43
- ...this.#e.generationConfig
44
- }, n = await this.#n.models.generateContentStream({
45
- model: this.#t,
46
- contents: t,
47
- config: e
48
- });
49
- return (async function* () {
50
- for await (const o of n)
51
- yield {
52
- text: () => o.text,
53
- usageMetadata: {
54
- totalTokenCount: o.usageMetadata?.totalTokenCount || 0
55
- }
56
- };
57
- })();
58
- }
59
- /**
60
- * Counts tokens.
61
- * @param {Array} contents - The content to count.
62
- * @returns {Promise<number>} Total tokens.
63
- */
64
- async countTokens(t) {
65
- const { totalTokens: e } = await this.#n.models.countTokens({
66
- model: this.#t,
67
- contents: t
68
- });
69
- return e;
70
- }
71
- }
72
- export {
73
- l as default
1
+ import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
2
+ import { GoogleGenAI as n } from "@google/genai";
3
+ //#region backends/gemini.js
4
+ var r = class extends e {
5
+ #e;
6
+ #t;
7
+ #n;
8
+ constructor(e) {
9
+ super(e.modelName || t.gemini.modelName), this.#e = new n({ apiKey: e.apiKey });
10
+ }
11
+ createSession(e, t) {
12
+ return this.#n = t, this.#t = e.modelName || this.modelName, {
13
+ model: this.#t,
14
+ params: t
15
+ };
16
+ }
17
+ async generateContent(e) {
18
+ let t = {
19
+ systemInstruction: this.#n.systemInstruction,
20
+ ...this.#n.generationConfig
21
+ }, n = await this.#e.models.generateContent({
22
+ model: this.#t,
23
+ contents: e,
24
+ config: t
25
+ }), r = n.usageMetadata?.promptTokenCount || 0;
26
+ return {
27
+ text: n.text,
28
+ usage: r
29
+ };
30
+ }
31
+ async generateContentStream(e) {
32
+ let t = {
33
+ systemInstruction: this.#n.systemInstruction,
34
+ ...this.#n.generationConfig
35
+ }, n = await this.#e.models.generateContentStream({
36
+ model: this.#t,
37
+ contents: e,
38
+ config: t
39
+ });
40
+ return (async function* () {
41
+ for await (let e of n) yield {
42
+ text: () => e.text,
43
+ usageMetadata: { totalTokenCount: e.usageMetadata?.totalTokenCount || 0 }
44
+ };
45
+ })();
46
+ }
47
+ async countTokens(e) {
48
+ let { totalTokens: t } = await this.#e.models.countTokens({
49
+ model: this.#t,
50
+ contents: e
51
+ });
52
+ return t;
53
+ }
74
54
  };
55
+ //#endregion
56
+ export { r as default };
@@ -1,217 +1,164 @@
1
- import u from "openai";
2
- import { P as m, D as l } from "../chunks/defaults-CzvdT-At.js";
3
- class y extends m {
4
- #e;
5
- constructor(t) {
6
- super(t.modelName || l.openai.modelName), this.config = t, this.openai = new u({
7
- apiKey: t.apiKey,
8
- dangerouslyAllowBrowser: !0
9
- // Required for client-side usage
10
- });
11
- }
12
- static availability(t = {}) {
13
- if (t.expectedInputs) {
14
- const s = t.expectedInputs.some(
15
- (e) => e.type === "audio"
16
- ), o = t.expectedInputs.some(
17
- (e) => e.type === "image"
18
- );
19
- if (s && o)
20
- return "unavailable";
21
- }
22
- return "available";
23
- }
24
- /**
25
- * Translates a standard JSON Schema into a backend-specific format.
26
- * OpenAI Structured Outputs require:
27
- * 1. All fields in objects to be marked as 'required'.
28
- * 2. Objects to have 'additionalProperties: false'.
29
- * 3. The root must be an 'object'.
30
- * @param {Object} schema - The standard JSON Schema.
31
- * @returns {any} The backend-specific schema.
32
- */
33
- convertSchema(t) {
34
- if (typeof t != "object" || t === null)
35
- return { schema: t, wrapped: !1 };
36
- const s = (e) => {
37
- if (e.type === "object")
38
- if (e.properties) {
39
- e.additionalProperties = !1, e.required = Object.keys(e.properties);
40
- for (const r in e.properties)
41
- s(e.properties[r]);
42
- } else
43
- e.additionalProperties = !1, e.required = [];
44
- else e.type === "array" && e.items && s(e.items);
45
- return e;
46
- }, o = JSON.parse(JSON.stringify(t));
47
- return o.type !== "object" ? {
48
- wrapped: !0,
49
- schema: {
50
- type: "object",
51
- properties: { value: o },
52
- required: ["value"],
53
- additionalProperties: !1
54
- }
55
- } : {
56
- wrapped: !1,
57
- schema: s(o)
58
- };
59
- }
60
- /**
61
- * Creates a model session and stores it.
62
- * @param {Object} options - LanguageModel options.
63
- * @param {Object} sessionParams - Parameters for the cloud or local model.
64
- * @returns {any} The created session object.
65
- */
66
- createSession(t, s) {
67
- this.#e = {
68
- model: t.modelName || this.modelName,
69
- systemInstruction: s.systemInstruction
70
- };
71
- const o = s.generationConfig || {};
72
- if (o.responseSchema) {
73
- const { schema: e, wrapped: r } = o.responseSchema;
74
- this.#e.response_format = {
75
- type: "json_schema",
76
- json_schema: {
77
- name: "response",
78
- strict: !0,
79
- schema: e
80
- }
81
- }, this.#e.response_wrapped = r;
82
- } else o.responseMimeType === "application/json" && (this.#e.response_format = { type: "json_object" });
83
- return this.#e;
84
- }
85
- #t(t) {
86
- let s = !1, o = !1;
87
- for (const e of t)
88
- if (Array.isArray(e.content))
89
- for (const r of e.content)
90
- r.type === "image_url" && (s = !0), r.type === "input_audio" && (o = !0);
91
- if (s && o)
92
- throw new Error(
93
- "OpenAI backend does not support mixing images and audio in the same session. Please start a new session."
94
- );
95
- return { hasImage: s, hasAudio: o };
96
- }
97
- #s(t) {
98
- return this.#e.model !== this.modelName ? this.#e.model : t ? `${this.modelName}-audio-preview` : this.modelName;
99
- }
100
- /**
101
- * Generates content (non-streaming).
102
- * @param {Array} contents - The history + new message content.
103
- * @returns {Promise<{text: string, usage: number}>}
104
- */
105
- async generateContent(t) {
106
- const { messages: s } = this.#o(
107
- t,
108
- this.#e.systemInstruction
109
- ), { hasAudio: o } = this.#t(s), e = this.#s(o);
110
- if (e === `${this.modelName}-audio-preview` && this.#e.response_format)
111
- throw new DOMException(
112
- `OpenAI audio model ('${e}') does not support structured outputs (responseConstraint).`,
113
- "NotSupportedError"
114
- );
115
- const r = {
116
- model: e,
117
- messages: s
118
- };
119
- this.#e.temperature > 0 && (r.temperature = this.#e.temperature), this.#e.response_format && (r.response_format = this.#e.response_format);
120
- try {
121
- const a = await this.openai.chat.completions.create(r);
122
- let i = a.choices[0].message.content;
123
- if (this.#e.response_wrapped && i)
124
- try {
125
- const c = JSON.parse(i);
126
- c && typeof c == "object" && "value" in c && (i = JSON.stringify(c.value));
127
- } catch {
128
- }
129
- const p = a.usage?.prompt_tokens || 0;
130
- return { text: i, usage: p };
131
- } catch (a) {
132
- throw console.error("OpenAI Generate Content Error:", a), a;
133
- }
134
- }
135
- /**
136
- * Generates content stream.
137
- * @param {Array} contents - The history + new content.
138
- * @returns {Promise<AsyncIterable>} Stream of chunks.
139
- */
140
- async generateContentStream(t) {
141
- const { messages: s } = this.#o(
142
- t,
143
- this.#e.systemInstruction
144
- ), { hasAudio: o } = this.#t(s), e = this.#s(o);
145
- if (e === `${this.modelName}-audio-preview` && this.#e.response_format)
146
- throw new DOMException(
147
- `OpenAI audio model ('${e}') does not support structured outputs (responseConstraint).`,
148
- "NotSupportedError"
149
- );
150
- const r = {
151
- model: e,
152
- messages: s,
153
- stream: !0
154
- };
155
- this.#e.temperature > 0 && (r.temperature = this.#e.temperature), this.#e.response_format && (r.response_format = this.#e.response_format);
156
- try {
157
- const a = await this.openai.chat.completions.create(r);
158
- return (async function* () {
159
- for await (const n of a) {
160
- let i = n.choices[0]?.delta?.content;
161
- i && (yield {
162
- text: () => i,
163
- usageMetadata: { totalTokenCount: 0 }
164
- });
165
- }
166
- })();
167
- } catch (a) {
168
- throw console.error("OpenAI Generate Content Stream Error:", a), a;
169
- }
170
- }
171
- /**
172
- * Counts tokens.
173
- * @param {Array} contents - The content to count.
174
- * @returns {Promise<number>} Total tokens.
175
- */
176
- async countTokens(t) {
177
- let s = "";
178
- if (Array.isArray(t)) {
179
- for (const o of t)
180
- if (o.parts)
181
- for (const e of o.parts)
182
- e.text ? s += e.text : e.inlineData && (s += " ".repeat(1e3));
183
- }
184
- return Math.ceil(s.length / 4);
185
- }
186
- #o(t, s) {
187
- const o = [];
188
- s && o.push({
189
- role: "system",
190
- content: s
191
- });
192
- for (const e of t) {
193
- const r = e.role === "model" ? "assistant" : "user", a = [];
194
- for (const n of e.parts)
195
- if (n.text)
196
- a.push({ type: "text", text: n.text });
197
- else if (n.inlineData) {
198
- const { data: i, mimeType: p } = n.inlineData;
199
- p.startsWith("image/") ? a.push({
200
- type: "image_url",
201
- image_url: { url: `data:${p};base64,${i}` }
202
- }) : p.startsWith("audio/") && a.push({
203
- type: "input_audio",
204
- input_audio: {
205
- data: i,
206
- format: p.split("/")[1] === "mpeg" ? "mp3" : "wav"
207
- }
208
- });
209
- }
210
- o.push({ role: r, content: a });
211
- }
212
- return { messages: o };
213
- }
214
- }
215
- export {
216
- y as default
1
+ import { n as e, t } from "../chunks/defaults-B5W7MP9T.js";
2
+ import n from "openai";
3
+ //#region backends/openai.js
4
+ var r = class extends e {
5
+ #e;
6
+ constructor(e) {
7
+ super(e.modelName || t.openai.modelName), this.config = e, this.openai = new n({
8
+ apiKey: e.apiKey,
9
+ dangerouslyAllowBrowser: !0
10
+ });
11
+ }
12
+ static availability(e = {}) {
13
+ if (e.expectedInputs) {
14
+ let t = e.expectedInputs.some((e) => e.type === "audio"), n = e.expectedInputs.some((e) => e.type === "image");
15
+ if (t && n) return "unavailable";
16
+ }
17
+ return "available";
18
+ }
19
+ convertSchema(e) {
20
+ if (typeof e != "object" || !e) return {
21
+ schema: e,
22
+ wrapped: !1
23
+ };
24
+ let t = (e) => {
25
+ if (e.type === "object") if (e.properties) {
26
+ e.additionalProperties = !1, e.required = Object.keys(e.properties);
27
+ for (let n in e.properties) t(e.properties[n]);
28
+ } else e.additionalProperties = !1, e.required = [];
29
+ else e.type === "array" && e.items && t(e.items);
30
+ return e;
31
+ }, n = JSON.parse(JSON.stringify(e));
32
+ return n.type === "object" ? {
33
+ wrapped: !1,
34
+ schema: t(n)
35
+ } : {
36
+ wrapped: !0,
37
+ schema: {
38
+ type: "object",
39
+ properties: { value: n },
40
+ required: ["value"],
41
+ additionalProperties: !1
42
+ }
43
+ };
44
+ }
45
+ createSession(e, t) {
46
+ this.#e = {
47
+ model: e.modelName || this.modelName,
48
+ systemInstruction: t.systemInstruction
49
+ };
50
+ let n = t.generationConfig || {};
51
+ if (n.responseSchema) {
52
+ let { schema: e, wrapped: t } = n.responseSchema;
53
+ this.#e.response_format = {
54
+ type: "json_schema",
55
+ json_schema: {
56
+ name: "response",
57
+ strict: !0,
58
+ schema: e
59
+ }
60
+ }, this.#e.response_wrapped = t;
61
+ } else n.responseMimeType === "application/json" && (this.#e.response_format = { type: "json_object" });
62
+ return this.#e;
63
+ }
64
+ #t(e) {
65
+ let t = !1, n = !1;
66
+ for (let r of e) if (Array.isArray(r.content)) for (let e of r.content) e.type === "image_url" && (t = !0), e.type === "input_audio" && (n = !0);
67
+ if (t && n) throw Error("OpenAI backend does not support mixing images and audio in the same session. Please start a new session.");
68
+ return {
69
+ hasImage: t,
70
+ hasAudio: n
71
+ };
72
+ }
73
+ #n(e) {
74
+ return this.#e.model === this.modelName ? e ? `${this.modelName}-audio-preview` : this.modelName : this.#e.model;
75
+ }
76
+ async generateContent(e) {
77
+ let { messages: t } = this.#r(e, this.#e.systemInstruction), { hasAudio: n } = this.#t(t), r = this.#n(n);
78
+ if (r === `${this.modelName}-audio-preview` && this.#e.response_format) throw new DOMException(`OpenAI audio model ('${r}') does not support structured outputs (responseConstraint).`, "NotSupportedError");
79
+ let i = {
80
+ model: r,
81
+ messages: t
82
+ };
83
+ this.#e.temperature > 0 && (i.temperature = this.#e.temperature), this.#e.response_format && (i.response_format = this.#e.response_format);
84
+ try {
85
+ let e = await this.openai.chat.completions.create(i), t = e.choices[0].message.content;
86
+ if (this.#e.response_wrapped && t) try {
87
+ let e = JSON.parse(t);
88
+ e && typeof e == "object" && "value" in e && (t = JSON.stringify(e.value));
89
+ } catch {}
90
+ let n = e.usage?.prompt_tokens || 0;
91
+ return {
92
+ text: t,
93
+ usage: n
94
+ };
95
+ } catch (e) {
96
+ throw console.error("OpenAI Generate Content Error:", e), e;
97
+ }
98
+ }
99
+ async generateContentStream(e) {
100
+ let { messages: t } = this.#r(e, this.#e.systemInstruction), { hasAudio: n } = this.#t(t), r = this.#n(n);
101
+ if (r === `${this.modelName}-audio-preview` && this.#e.response_format) throw new DOMException(`OpenAI audio model ('${r}') does not support structured outputs (responseConstraint).`, "NotSupportedError");
102
+ let i = {
103
+ model: r,
104
+ messages: t,
105
+ stream: !0
106
+ };
107
+ this.#e.temperature > 0 && (i.temperature = this.#e.temperature), this.#e.response_format && (i.response_format = this.#e.response_format);
108
+ try {
109
+ let e = await this.openai.chat.completions.create(i);
110
+ return (async function* () {
111
+ for await (let t of e) {
112
+ let e = t.choices[0]?.delta?.content;
113
+ e && (yield {
114
+ text: () => e,
115
+ usageMetadata: { totalTokenCount: 0 }
116
+ });
117
+ }
118
+ })();
119
+ } catch (e) {
120
+ throw console.error("OpenAI Generate Content Stream Error:", e), e;
121
+ }
122
+ }
123
+ async countTokens(e) {
124
+ let t = "";
125
+ if (Array.isArray(e)) {
126
+ for (let n of e) if (n.parts) for (let e of n.parts) e.text ? t += e.text : e.inlineData && (t += " ".repeat(1e3));
127
+ }
128
+ return Math.ceil(t.length / 4);
129
+ }
130
+ #r(e, t) {
131
+ let n = [];
132
+ t && n.push({
133
+ role: "system",
134
+ content: t
135
+ });
136
+ for (let t of e) {
137
+ let e = t.role === "model" ? "assistant" : "user", r = [];
138
+ for (let e of t.parts) if (e.text) r.push({
139
+ type: "text",
140
+ text: e.text
141
+ });
142
+ else if (e.inlineData) {
143
+ let { data: t, mimeType: n } = e.inlineData;
144
+ n.startsWith("image/") ? r.push({
145
+ type: "image_url",
146
+ image_url: { url: `data:${n};base64,${t}` }
147
+ }) : n.startsWith("audio/") && r.push({
148
+ type: "input_audio",
149
+ input_audio: {
150
+ data: t,
151
+ format: n.split("/")[1] === "mpeg" ? "mp3" : "wav"
152
+ }
153
+ });
154
+ }
155
+ n.push({
156
+ role: e,
157
+ content: r
158
+ });
159
+ }
160
+ return { messages: n };
161
+ }
217
162
  };
163
+ //#endregion
164
+ export { r as default };