@kindgi/adapter-model-gemini 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +101 -2
- package/dist/cost.d.ts +42 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +40 -0
- package/dist/cost.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +63 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +168 -0
- package/dist/provider.js.map +1 -0
- package/dist/translate.d.ts +49 -0
- package/dist/translate.d.ts.map +1 -0
- package/dist/translate.js +179 -0
- package/dist/translate.js.map +1 -0
- package/package.json +48 -3
- package/src/cost.ts +73 -0
- package/src/index.ts +20 -0
- package/src/provider.ts +237 -0
- package/src/translate.ts +215 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Kindgi Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,102 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/adapter-model-gemini`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Gemini `ModelProvider` for [`@kindgi/capabilities`](../../capabilities/), on Google Cloud's Vertex AI. Wraps the official `@google/genai` client behind the framework's provider-neutral interface. One provider exposes every model in its `metadata.models[]`; `ModelCallInput.model` picks the model per call.
|
|
4
|
+
|
|
5
|
+
## Credentials
|
|
6
|
+
|
|
7
|
+
No API key. The client uses **Google Application Default Credentials**:
|
|
8
|
+
|
|
9
|
+
- on a laptop, `gcloud auth application-default login`;
|
|
10
|
+
- on Cloud Run and other Google Cloud runtimes, the attached service account;
|
|
11
|
+
- elsewhere, a service-account key (its JSON) passed as `credentials` — through a provider row's `secret_ref` when registered with a Kindgi server.
|
|
12
|
+
|
|
13
|
+
The account needs the Vertex AI User role (`roles/aiplatform.user`) in the project.
|
|
14
|
+
|
|
15
|
+
## Translation
|
|
16
|
+
|
|
17
|
+
One non-streaming `models.generateContent` call per `invoke`:
|
|
18
|
+
|
|
19
|
+
- `system` messages lift into `systemInstruction` (several join with a blank line). `user` messages become `user` turns, `assistant` messages `model` turns, and consecutive turns of one role merge.
|
|
20
|
+
- **Function calling.** Tool definitions become `functionDeclarations`, with the tool's JSON Schema as `parametersJsonSchema`. Assistant tool calls replay as `functionCall` parts. Tool results become `functionResponse` parts, named after the call they answer, with the tool's output under `response.output` (parsed when it's JSON).
|
|
21
|
+
- Tool names go over the wire as they are. Gemini accepts `<pack>.<tool>` ids: a letter or `_` first, then letters, digits and `_ . : -`, at most 64 characters. Other names are refused.
|
|
22
|
+
- **Thought signatures.** Each `functionCall` part's `thoughtSignature` comes back as `ModelToolCall.signature`, and goes out again on the same call when the conversation continues. Gemini's thinking models need it to keep their reasoning across a tool round trip.
|
|
23
|
+
- **Thinking parts** (`thought: true`) are left out of the message text. Thinking tokens count as completion tokens, since they bill as output.
|
|
24
|
+
- **Structured output.** `structuredOutput` becomes `responseMimeType: 'application/json'` plus `responseJsonSchema`.
|
|
25
|
+
- `temperature`, `maxOutputTokens` (the call's, else the model's `ModelInfo.maxOutputTokens`) and `abortSignal` are passed through.
|
|
26
|
+
- **`finishReason`:**
|
|
27
|
+
- a response with function calls → `tool-use`;
|
|
28
|
+
- `STOP` or unspecified → `stop`;
|
|
29
|
+
- `MAX_TOKENS` → `length`;
|
|
30
|
+
- the safety, recitation, blocklist and sensitive-data stops, or a blocked prompt → `content-filter`;
|
|
31
|
+
- anything else (a malformed or unexpected function call, too many tool calls, `OTHER`) → `error`.
|
|
32
|
+
|
|
33
|
+
## Cost
|
|
34
|
+
|
|
35
|
+
`costUsd` is computed from the response's usage against the picked model's `cost`:
|
|
36
|
+
|
|
37
|
+
- uncached prompt tokens at `promptUsdPer1kTokens`;
|
|
38
|
+
- cached prompt tokens at `promptUsdPer1kTokens × cachedPromptMultiplier` (default `0.25`, Gemini 2.5's implicit-cache discount);
|
|
39
|
+
- completion tokens, thinking included, at `completionUsdPer1kTokens`;
|
|
40
|
+
- `longContext: { thresholdTokens, promptUsdPer1kTokens, completionUsdPer1kTokens }`: a call whose prompt exceeds the threshold bills entirely at those rates (Gemini 2.5 Pro doubles past 200K prompt tokens).
|
|
41
|
+
|
|
42
|
+
`usage.promptTokens` includes cached tokens and built-in tool prompts; `usage.cachedTokens` reports the cached ones when there are any.
|
|
43
|
+
|
|
44
|
+
## Exports
|
|
45
|
+
|
|
46
|
+
- **`createGeminiProvider(options)`**: a `ModelProvider` whose `metadata` is `options.metadata`. `invoke()` throws when `input.model` is not one of `metadata.models[].name`.
|
|
47
|
+
- **`GeminiProviderOptions`**:
|
|
48
|
+
- `metadata`: `ProviderMetadata` whose `models[]` are `GeminiModelInfo`. Caller-supplied; the API doesn't report pricing.
|
|
49
|
+
- `vertex: { project, location }`: the Vertex AI project, and the location (`global` or a region).
|
|
50
|
+
- `credentials?: () => string | Promise<string>`: a service-account key, resolved on every call. The client is rebuilt only when the resolved key changes, so a rotated key applies on the next call. Absent: Application Default Credentials.
|
|
51
|
+
- `client?`: an injected `GeminiClient` (anything with `models.generateContent`), used as is. For tests.
|
|
52
|
+
- **`geminiAdapterFactory`** / **`GEMINI_ADAPTER_ID`** (`'@kindgi/adapter-model-gemini'`): the factory a server registers. A provider row carries:
|
|
53
|
+
- `adapter_config.project` (required);
|
|
54
|
+
- `metadata.region` as the location (`unspecified` means `global`);
|
|
55
|
+
- an optional `secret_ref` holding a service-account key.
|
|
56
|
+
- **`vertexTarget(input)`**: the project and location a registration names, as the factory reads them.
|
|
57
|
+
- **Cost helpers**: `computeCostUsd(usage, rates)`, `toFrameworkUsage(usageMetadata)`, `GeminiCostRates`, `GeminiModelInfo`, `DEFAULT_CACHED_PROMPT_MULTIPLIER`.
|
|
58
|
+
- **Translation helpers**: `toGeminiRequest(messages)`, `toGeminiFunctions(tools)`, `fromGeminiResponse(response)`, `mapFinishReason(reason)`, `checkFunctionName(name)`.
|
|
59
|
+
|
|
60
|
+
## Example
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { createGeminiProvider } from '@kindgi/adapter-model-gemini';
|
|
64
|
+
|
|
65
|
+
const gemini = createGeminiProvider({
|
|
66
|
+
vertex: { project: 'my-project', location: 'global' },
|
|
67
|
+
metadata: {
|
|
68
|
+
id: 'gemini',
|
|
69
|
+
region: 'global',
|
|
70
|
+
models: [
|
|
71
|
+
{
|
|
72
|
+
name: 'gemini-2.5-pro',
|
|
73
|
+
contextWindow: 1_048_576,
|
|
74
|
+
features: ['tool-use'],
|
|
75
|
+
maxOutputTokens: 8_192,
|
|
76
|
+
// Rates from Google's published pricing.
|
|
77
|
+
cost: {
|
|
78
|
+
promptUsdPer1kTokens: 0.00125,
|
|
79
|
+
completionUsdPer1kTokens: 0.01,
|
|
80
|
+
longContext: {
|
|
81
|
+
thresholdTokens: 200_000,
|
|
82
|
+
promptUsdPer1kTokens: 0.0025,
|
|
83
|
+
completionUsdPer1kTokens: 0.015,
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const result = await gemini.invoke({
|
|
92
|
+
model: 'gemini-2.5-pro',
|
|
93
|
+
messages: [{ role: 'user', content: 'Summarise the grievance in one sentence.' }],
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
With a Kindgi server, register the provider instead (`kindgi providers register --spec=@provider.json`): `{ metadata, adapter_id: '@kindgi/adapter-model-gemini', adapter_config: { project } }`.
|
|
98
|
+
|
|
99
|
+
## Tests
|
|
100
|
+
|
|
101
|
+
- `tests/translate.test.ts` and `tests/provider.test.ts` run against a fake client.
|
|
102
|
+
- `tests/live.test.ts` calls Vertex AI. It runs only when `GOOGLE_CLOUD_PROJECT` is set (with `GOOGLE_CLOUD_LOCATION`, default `global`, and `KINDGI_LIVE_GEMINI_MODEL`, default `gemini-2.5-flash`) and Application Default Credentials are available. It covers a plain answer and a full tool round trip that sends the thought signature back.
|
package/dist/cost.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { GenerateContentResponseUsageMetadata } from '@google/genai';
|
|
2
|
+
import type { ModelInfo, UsageCounters } from '@kindgi/capabilities';
|
|
3
|
+
/**
|
|
4
|
+
* Cached prompt tokens bill at this fraction of the prompt rate unless a
|
|
5
|
+
* model says otherwise — Gemini 2.5's implicit-cache discount (75% off).
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_CACHED_PROMPT_MULTIPLIER = 0.25;
|
|
8
|
+
/** A model's rates, per 1K tokens, with Gemini's two pricing extras. */
|
|
9
|
+
export interface GeminiCostRates {
|
|
10
|
+
readonly promptUsdPer1kTokens: number;
|
|
11
|
+
readonly completionUsdPer1kTokens: number;
|
|
12
|
+
/** Cached prompt tokens' share of the prompt rate. Default 0.25. */
|
|
13
|
+
readonly cachedPromptMultiplier?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Long-context pricing: when a call's prompt exceeds `thresholdTokens`,
|
|
16
|
+
* the whole call bills at these rates (Gemini 2.5 Pro doubles above
|
|
17
|
+
* 200K prompt tokens, for example).
|
|
18
|
+
*/
|
|
19
|
+
readonly longContext?: {
|
|
20
|
+
readonly thresholdTokens: number;
|
|
21
|
+
readonly promptUsdPer1kTokens: number;
|
|
22
|
+
readonly completionUsdPer1kTokens: number;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** `ModelInfo` whose `cost` takes Gemini's cached-token and long-context rates. */
|
|
26
|
+
export interface GeminiModelInfo extends ModelInfo {
|
|
27
|
+
readonly cost: ModelInfo['cost'] & Omit<GeminiCostRates, keyof ModelInfo['cost']>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Gemini's usage as the framework's counters. Thinking tokens bill as
|
|
31
|
+
* output, so they count as completion tokens; the prompt count includes
|
|
32
|
+
* cached tokens and the tokens of built-in tool prompts.
|
|
33
|
+
*/
|
|
34
|
+
export declare function toFrameworkUsage(usage: GenerateContentResponseUsageMetadata | undefined): UsageCounters;
|
|
35
|
+
/**
|
|
36
|
+
* A call's cost in USD: uncached prompt tokens at the prompt rate, cached
|
|
37
|
+
* ones at the cached share of it, completion (thinking included) at the
|
|
38
|
+
* completion rate — the long-context rates when the prompt is past the
|
|
39
|
+
* model's threshold.
|
|
40
|
+
*/
|
|
41
|
+
export declare function computeCostUsd(usage: UsageCounters, rates: GeminiCostRates): number;
|
|
42
|
+
//# sourceMappingURL=cost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oCAAoC,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErE;;;GAGG;AACH,eAAO,MAAM,gCAAgC,OAAO,CAAC;AAErD,wEAAwE;AACxE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,wBAAwB,EAAE,MAAM,CAAC;IAC1C,oEAAoE;IACpE,QAAQ,CAAC,sBAAsB,CAAC,EAAE,MAAM,CAAC;IACzC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE;QACrB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;QACjC,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;QACtC,QAAQ,CAAC,wBAAwB,EAAE,MAAM,CAAC;KAC3C,CAAC;CACH;AAED,mFAAmF;AACnF,MAAM,WAAW,eAAgB,SAAQ,SAAS;IAChD,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,eAAe,EAAE,MAAM,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;CACnF;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,oCAAoC,GAAG,SAAS,GACtD,aAAa,CAOf;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,eAAe,GAAG,MAAM,CAenF"}
|
package/dist/cost.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* Cached prompt tokens bill at this fraction of the prompt rate unless a
|
|
5
|
+
* model says otherwise — Gemini 2.5's implicit-cache discount (75% off).
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_CACHED_PROMPT_MULTIPLIER = 0.25;
|
|
8
|
+
/**
|
|
9
|
+
* Gemini's usage as the framework's counters. Thinking tokens bill as
|
|
10
|
+
* output, so they count as completion tokens; the prompt count includes
|
|
11
|
+
* cached tokens and the tokens of built-in tool prompts.
|
|
12
|
+
*/
|
|
13
|
+
export function toFrameworkUsage(usage) {
|
|
14
|
+
const cached = usage?.cachedContentTokenCount ?? 0;
|
|
15
|
+
return {
|
|
16
|
+
promptTokens: (usage?.promptTokenCount ?? 0) + (usage?.toolUsePromptTokenCount ?? 0),
|
|
17
|
+
completionTokens: (usage?.candidatesTokenCount ?? 0) + (usage?.thoughtsTokenCount ?? 0),
|
|
18
|
+
...(cached > 0 && { cachedTokens: cached }),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A call's cost in USD: uncached prompt tokens at the prompt rate, cached
|
|
23
|
+
* ones at the cached share of it, completion (thinking included) at the
|
|
24
|
+
* completion rate — the long-context rates when the prompt is past the
|
|
25
|
+
* model's threshold.
|
|
26
|
+
*/
|
|
27
|
+
export function computeCostUsd(usage, rates) {
|
|
28
|
+
const long = rates.longContext !== undefined && usage.promptTokens > rates.longContext.thresholdTokens
|
|
29
|
+
? rates.longContext
|
|
30
|
+
: undefined;
|
|
31
|
+
const promptRate = long?.promptUsdPer1kTokens ?? rates.promptUsdPer1kTokens;
|
|
32
|
+
const completionRate = long?.completionUsdPer1kTokens ?? rates.completionUsdPer1kTokens;
|
|
33
|
+
const cached = usage.cachedTokens ?? 0;
|
|
34
|
+
const multiplier = rates.cachedPromptMultiplier ?? DEFAULT_CACHED_PROMPT_MULTIPLIER;
|
|
35
|
+
return (((usage.promptTokens - cached) * promptRate +
|
|
36
|
+
cached * promptRate * multiplier +
|
|
37
|
+
usage.completionTokens * completionRate) /
|
|
38
|
+
1000);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=cost.js.map
|
package/dist/cost.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAKjC;;;GAGG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,IAAI,CAAC;AAyBrD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAuD;IAEvD,MAAM,MAAM,GAAG,KAAK,EAAE,uBAAuB,IAAI,CAAC,CAAC;IACnD,OAAO;QACL,YAAY,EAAE,CAAC,KAAK,EAAE,gBAAgB,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,uBAAuB,IAAI,CAAC,CAAC;QACpF,gBAAgB,EAAE,CAAC,KAAK,EAAE,oBAAoB,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,kBAAkB,IAAI,CAAC,CAAC;QACvF,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;KAC5C,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAoB,EAAE,KAAsB;IACzE,MAAM,IAAI,GACR,KAAK,CAAC,WAAW,KAAK,SAAS,IAAI,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,WAAW,CAAC,eAAe;QACvF,CAAC,CAAC,KAAK,CAAC,WAAW;QACnB,CAAC,CAAC,SAAS,CAAC;IAChB,MAAM,UAAU,GAAG,IAAI,EAAE,oBAAoB,IAAI,KAAK,CAAC,oBAAoB,CAAC;IAC5E,MAAM,cAAc,GAAG,IAAI,EAAE,wBAAwB,IAAI,KAAK,CAAC,wBAAwB,CAAC;IACxF,MAAM,MAAM,GAAG,KAAK,CAAC,YAAY,IAAI,CAAC,CAAC;IACvC,MAAM,UAAU,GAAG,KAAK,CAAC,sBAAsB,IAAI,gCAAgC,CAAC;IACpF,OAAO,CACL,CAAC,CAAC,KAAK,CAAC,YAAY,GAAG,MAAM,CAAC,GAAG,UAAU;QACzC,MAAM,GAAG,UAAU,GAAG,UAAU;QAChC,KAAK,CAAC,gBAAgB,GAAG,cAAc,CAAC;QAC1C,IAAI,CACL,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { GEMINI_ADAPTER_ID, createGeminiProvider, geminiAdapterFactory, vertexTarget, } from './provider.js';
|
|
2
|
+
export type { GeminiClient, GeminiProviderOptions } from './provider.js';
|
|
3
|
+
export { DEFAULT_CACHED_PROMPT_MULTIPLIER, computeCostUsd, toFrameworkUsage } from './cost.js';
|
|
4
|
+
export type { GeminiCostRates, GeminiModelInfo } from './cost.js';
|
|
5
|
+
export { checkFunctionName, fromGeminiResponse, mapFinishReason, toGeminiFunctions, toGeminiRequest, } from './translate.js';
|
|
6
|
+
export type { ModelProvider } from '@kindgi/capabilities';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACpB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,YAAY,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAAE,gCAAgC,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC/F,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAClE,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
export { GEMINI_ADAPTER_ID, createGeminiProvider, geminiAdapterFactory, vertexTarget, } from './provider.js';
|
|
4
|
+
export { DEFAULT_CACHED_PROMPT_MULTIPLIER, computeCostUsd, toFrameworkUsage } from './cost.js';
|
|
5
|
+
export { checkFunctionName, fromGeminiResponse, mapFinishReason, toGeminiFunctions, toGeminiRequest, } from './translate.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACpB,YAAY,GACb,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,gCAAgC,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAE/F,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,eAAe,GAChB,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type GenerateContentParameters, type GenerateContentResponse } from '@google/genai';
|
|
2
|
+
import type { AdapterFactory, ModelProvider, ProviderMetadata } from '@kindgi/capabilities';
|
|
3
|
+
import { type GeminiModelInfo } from './cost.js';
|
|
4
|
+
/** The adapter id servers register `geminiAdapterFactory` under. */
|
|
5
|
+
export declare const GEMINI_ADAPTER_ID = "@kindgi/adapter-model-gemini";
|
|
6
|
+
/** The slice of the SDK client the adapter calls. Tests pass a fake. */
|
|
7
|
+
export interface GeminiClient {
|
|
8
|
+
readonly models: {
|
|
9
|
+
generateContent(params: GenerateContentParameters): Promise<GenerateContentResponse>;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export interface GeminiProviderOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Metadata surfaced to the router. `models[]` lists every model the
|
|
15
|
+
* caller expects to invoke through this connection; per-model `cost`
|
|
16
|
+
* takes Gemini's cached-token and long-context rates.
|
|
17
|
+
*/
|
|
18
|
+
readonly metadata: Omit<ProviderMetadata, 'models'> & {
|
|
19
|
+
readonly models: readonly GeminiModelInfo[];
|
|
20
|
+
};
|
|
21
|
+
/** The Vertex AI project and location (`global`, or a region such as `us-central1`). */
|
|
22
|
+
readonly vertex: {
|
|
23
|
+
readonly project: string;
|
|
24
|
+
readonly location: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A service-account key (its JSON), resolved on every call. Absent:
|
|
28
|
+
* Google Application Default Credentials — `gcloud auth
|
|
29
|
+
* application-default login` on a laptop, the attached service account
|
|
30
|
+
* on Cloud Run and other Google Cloud runtimes.
|
|
31
|
+
*/
|
|
32
|
+
readonly credentials?: () => string | Promise<string>;
|
|
33
|
+
/** An injected client, used as is (tests). `vertex` and `credentials` are then unused. */
|
|
34
|
+
readonly client?: GeminiClient;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A Gemini `ModelProvider` on Vertex AI. Non-streaming, tool-use enabled:
|
|
38
|
+
* one `generateContent` call per `invoke`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createGeminiProvider(options: GeminiProviderOptions): ModelProvider;
|
|
41
|
+
/**
|
|
42
|
+
* A service-account key's JSON, reduced to the fields a service-account
|
|
43
|
+
* key has. google-auth-library acts on a credential's `type`: an
|
|
44
|
+
* `external_account` fetches URLs it names (with its headers) and reads
|
|
45
|
+
* files it names, `token_uri` and `universe_domain` move the token
|
|
46
|
+
* exchange. A tenant writes this secret, so only `service_account` is
|
|
47
|
+
* accepted, and Google's own token endpoint is always used.
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseServiceAccountKey(key: string): Record<string, string>;
|
|
50
|
+
/**
|
|
51
|
+
* The adapter factory servers register under `GEMINI_ADAPTER_ID`. A
|
|
52
|
+
* provider registered with it carries:
|
|
53
|
+
*
|
|
54
|
+
* - `adapter_config.project` — the Vertex AI project (required);
|
|
55
|
+
* - `metadata.region` — the Vertex location (`global`, `us-central1`,
|
|
56
|
+
* `northamerica-northeast1`, …); `'unspecified'` means `global`;
|
|
57
|
+
* - `secret_ref` (optional) — a service-account key. Without one the
|
|
58
|
+
* adapter uses Application Default Credentials.
|
|
59
|
+
*/
|
|
60
|
+
export declare const geminiAdapterFactory: AdapterFactory;
|
|
61
|
+
/** The Vertex project and location a registration names (see `geminiAdapterFactory`). */
|
|
62
|
+
export declare function vertexTarget(input: Parameters<AdapterFactory>[0]): GeminiProviderOptions['vertex'];
|
|
63
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAE7B,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,cAAc,EAGd,aAAa,EACb,gBAAgB,EACjB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,KAAK,eAAe,EAAoC,MAAM,WAAW,CAAC;AAGnF,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,iCAAiC,CAAC;AAEhE,wEAAwE;AACxE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE;QACf,eAAe,CAAC,MAAM,EAAE,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;KACtF,CAAC;CACH;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG;QACpD,QAAQ,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,CAAC;KAC7C,CAAC;IACF,wFAAwF;IACxF,QAAQ,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACzE;;;;;OAKG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,0FAA0F;IAC1F,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;CAChC;AAID;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,aAAa,CA4ElF;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiC1E;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,EAAE,cAK/B,CAAC;AAEL,yFAAyF;AACzF,wBAAgB,YAAY,CAC1B,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,GACnC,qBAAqB,CAAC,QAAQ,CAAC,CAajC"}
|