@agentproto/providers-store 0.2.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 +202 -0
- package/README.md +51 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.mjs +91 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @agentproto/providers-store
|
|
2
|
+
|
|
3
|
+
The `~/.agentproto/providers.json` LLM provider API-key store (mode 0600),
|
|
4
|
+
written by `agentproto auth provider set`, and the `injectProviderKeysIntoEnv`
|
|
5
|
+
helper that lets a process boot with those keys in its environment.
|
|
6
|
+
|
|
7
|
+
Before this store existed, the only way to give a process a provider key was
|
|
8
|
+
an ambient `export FOO_API_KEY=…` in whatever shell launched it — invisible,
|
|
9
|
+
easy to forget, per-shell. Set once via the CLI, every process that calls
|
|
10
|
+
`injectProviderKeysIntoEnv` at boot picks it up.
|
|
11
|
+
|
|
12
|
+
This package is a **leaf**: node builtins + `@agentproto/model-catalog` only,
|
|
13
|
+
so any standalone process (not just the `@agentproto/runtime` daemon) can
|
|
14
|
+
depend on it without dragging in a full gateway.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { injectProviderKeysIntoEnv } from "@agentproto/providers-store"
|
|
20
|
+
|
|
21
|
+
// Call once at process boot, before anything reads provider env vars.
|
|
22
|
+
// Explicit env always wins — a var already set is never overwritten.
|
|
23
|
+
const injected = await injectProviderKeysIntoEnv(process.env)
|
|
24
|
+
if (injected.length > 0) {
|
|
25
|
+
console.error(`loaded ${injected.length} provider key(s) from store: ${injected.join(", ")}`)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Keys never leave the store except into the caller's own process env; they are
|
|
30
|
+
never logged — only provider *names*, never values, should reach output.
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
- `loadProviders()` / `setProviderKey(provider, apiKey, baseUrl?)` /
|
|
35
|
+
`removeProviderKey(provider)` / `getProviderKey(provider)` — read/write
|
|
36
|
+
`providers.json`.
|
|
37
|
+
- `injectProviderKeysIntoEnv(env?)` — injects stored keys into `env`
|
|
38
|
+
(default `process.env`), skipping any name already set. Returns the list of
|
|
39
|
+
provider names actually injected.
|
|
40
|
+
- `providerEnvVar(provider)` / `PROVIDER_ENV_VARS` — canonical provider →
|
|
41
|
+
env-var-name mapping (derived from `@agentproto/model-catalog`'s
|
|
42
|
+
`PROVIDER_KEY_ENV`, plus the non-catalog gateway providers this store also
|
|
43
|
+
fronts).
|
|
44
|
+
- `providersPath()` — resolves `~/.agentproto/providers.json`.
|
|
45
|
+
|
|
46
|
+
`@agentproto/runtime` re-exports this package's surface unchanged (including
|
|
47
|
+
the `@agentproto/runtime/providers-store` subpath) for existing consumers.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `~/.agentproto/providers.json` — the provider API-key store (mode 0600).
|
|
3
|
+
*
|
|
4
|
+
* Distinct from `credentials.json` (host-binding OAuth tokens for
|
|
5
|
+
* `serve --connect`) and from per-adapter `setup[]` tokens. This file holds
|
|
6
|
+
* the LLM/model **provider** keys (anthropic, openrouter, openai, …) that the
|
|
7
|
+
* model gateways in spawned agents read from the environment.
|
|
8
|
+
*
|
|
9
|
+
* Why a store at all: before this, the only way to give the daemon a provider
|
|
10
|
+
* key was an ambient `export FOO_API_KEY=…` in whatever shell launched
|
|
11
|
+
* `serve`. That's invisible, easy to forget, and per-shell. Storing the keys
|
|
12
|
+
* here (0600, same-user-only) lets `serve` inject them at boot — set once,
|
|
13
|
+
* works for every daemon. Explicit env still wins (see injectProviderKeysIntoEnv).
|
|
14
|
+
*
|
|
15
|
+
* Keys never leave this file except into the daemon's own process env; they
|
|
16
|
+
* are never logged. A browser-loaded localhost page can't read a 0600 file —
|
|
17
|
+
* the same defence the runtime.json bearer token relies on.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Canonical provider → environment-variable name. The spawned model gateways
|
|
21
|
+
* (Mastra in mastra-agent, hermes/opencode's routers) read the provider key
|
|
22
|
+
* from these env names. Aligned with the adapter manifests' `models.env`
|
|
23
|
+
* maps + the common SDK conventions.
|
|
24
|
+
*
|
|
25
|
+
* DERIVED from `@agentproto/model-catalog`'s {@link PROVIDER_KEY_ENV} (the
|
|
26
|
+
* single source of truth, co-located with the provider enum — DECISION 1) so
|
|
27
|
+
* the two maps that used to restate the same fact can't drift, plus the few
|
|
28
|
+
* NON-catalog gateway providers this store also fronts (`groq`,
|
|
29
|
+
* `vercel-ai-gateway`) which aren't LLM-catalog `provider` values but still
|
|
30
|
+
* carry a key here. Adding a catalog provider forces adding its key env in the
|
|
31
|
+
* catalog (the `Record<CatalogProvider, string>` is exhaustive), and it flows
|
|
32
|
+
* through to here for free.
|
|
33
|
+
*/
|
|
34
|
+
declare const PROVIDER_ENV_VARS: Readonly<Record<string, string>>;
|
|
35
|
+
type KnownProvider = keyof typeof PROVIDER_ENV_VARS;
|
|
36
|
+
interface ProviderEntry {
|
|
37
|
+
/** The provider API key (or gateway key). */
|
|
38
|
+
apiKey: string;
|
|
39
|
+
/** Optional custom base URL (self-hosted / proxy). */
|
|
40
|
+
baseUrl?: string;
|
|
41
|
+
/** Wall-clock ISO timestamp the key was last set. */
|
|
42
|
+
updatedAt: string;
|
|
43
|
+
}
|
|
44
|
+
interface ProvidersFile {
|
|
45
|
+
version: 1;
|
|
46
|
+
providers: Record<string, ProviderEntry>;
|
|
47
|
+
}
|
|
48
|
+
declare function providersPath(): string;
|
|
49
|
+
/** Resolve the env-var name for a provider (canonical map, else
|
|
50
|
+
* `<PROVIDER>_API_KEY` upper-snake fallback so new providers still work). */
|
|
51
|
+
declare function providerEnvVar(provider: string): string;
|
|
52
|
+
declare function loadProviders(): Promise<ProvidersFile>;
|
|
53
|
+
/**
|
|
54
|
+
* Read a single provider's stored api key from `providers.json`, or undefined
|
|
55
|
+
* when the provider has no entry (or the file doesn't exist). The EXPLICIT,
|
|
56
|
+
* verifiable credential source for the billing-auth resolver's api-key mode
|
|
57
|
+
* (`agentproto auth provider set <provider> <key>` writes it) — distinct from
|
|
58
|
+
* the ambient shell env, which the resolver never reads. Returns the raw key;
|
|
59
|
+
* only a non-secret fingerprint of it is ever surfaced back to a caller.
|
|
60
|
+
*/
|
|
61
|
+
declare function getProviderKey(provider: string): Promise<string | undefined>;
|
|
62
|
+
/** Set (or replace) a provider's key. Returns the env-var it maps to. */
|
|
63
|
+
declare function setProviderKey(provider: string, apiKey: string, baseUrl?: string): Promise<string>;
|
|
64
|
+
/** Remove a provider's key. Returns true if it existed. */
|
|
65
|
+
declare function removeProviderKey(provider: string): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Inject stored provider keys into a target env (default `process.env`).
|
|
68
|
+
* **Explicit env always wins** — a var already set is never overwritten, so a
|
|
69
|
+
* one-off `FOO_API_KEY=… serve` or a CI secret takes precedence over the
|
|
70
|
+
* store. Returns the list of provider names actually injected (for a boot log
|
|
71
|
+
* that names providers, never values).
|
|
72
|
+
*/
|
|
73
|
+
declare function injectProviderKeysIntoEnv(env?: NodeJS.ProcessEnv): Promise<string[]>;
|
|
74
|
+
|
|
75
|
+
export { type KnownProvider, PROVIDER_ENV_VARS, type ProviderEntry, type ProvidersFile, getProviderKey, injectProviderKeysIntoEnv, loadProviders, providerEnvVar, providersPath, removeProviderKey, setProviderKey };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readFile, mkdir, writeFile } from 'fs/promises';
|
|
2
|
+
import { homedir } from 'os';
|
|
3
|
+
import { resolve, join } from 'path';
|
|
4
|
+
import { PROVIDER_KEY_ENV } from '@agentproto/model-catalog';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @agentproto/providers-store v0.1.0
|
|
8
|
+
* The ~/.agentproto/providers.json LLM provider API-key store + env injection.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
var PROVIDER_ENV_VARS = {
|
|
12
|
+
...PROVIDER_KEY_ENV,
|
|
13
|
+
// ── Non-catalog gateway providers (not LLM-catalog `provider` enum values,
|
|
14
|
+
// so absent from PROVIDER_KEY_ENV, but still keyed through this store) ──
|
|
15
|
+
groq: "GROQ_API_KEY",
|
|
16
|
+
xai: "XAI_API_KEY",
|
|
17
|
+
// Vercel AI Gateway — a gateway provider, like openrouter, that fronts many
|
|
18
|
+
// upstream model families behind one key.
|
|
19
|
+
"vercel-ai-gateway": "AI_GATEWAY_API_KEY"
|
|
20
|
+
};
|
|
21
|
+
function emptyFile() {
|
|
22
|
+
return { version: 1, providers: {} };
|
|
23
|
+
}
|
|
24
|
+
function providersPath() {
|
|
25
|
+
return resolve(homedir(), ".agentproto", "providers.json");
|
|
26
|
+
}
|
|
27
|
+
function providerEnvVar(provider) {
|
|
28
|
+
return PROVIDER_ENV_VARS[provider] ?? `${provider.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}_API_KEY`;
|
|
29
|
+
}
|
|
30
|
+
async function loadProviders() {
|
|
31
|
+
try {
|
|
32
|
+
const raw = await readFile(providersPath(), "utf8");
|
|
33
|
+
const parsed = JSON.parse(raw);
|
|
34
|
+
if (!parsed || typeof parsed !== "object" || !("providers" in parsed) || typeof parsed.providers !== "object" || parsed.providers === null) {
|
|
35
|
+
return emptyFile();
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
version: 1,
|
|
39
|
+
providers: parsed.providers
|
|
40
|
+
};
|
|
41
|
+
} catch {
|
|
42
|
+
return emptyFile();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function getProviderKey(provider) {
|
|
46
|
+
const file = await loadProviders();
|
|
47
|
+
const entry = file.providers[provider];
|
|
48
|
+
return entry?.apiKey || void 0;
|
|
49
|
+
}
|
|
50
|
+
async function writeProviders(file) {
|
|
51
|
+
const dir = join(homedir(), ".agentproto");
|
|
52
|
+
await mkdir(dir, { recursive: true });
|
|
53
|
+
await writeFile(providersPath(), JSON.stringify(file, null, 2) + "\n", {
|
|
54
|
+
encoding: "utf8",
|
|
55
|
+
mode: 384
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async function setProviderKey(provider, apiKey, baseUrl) {
|
|
59
|
+
const file = await loadProviders();
|
|
60
|
+
file.providers[provider] = {
|
|
61
|
+
apiKey,
|
|
62
|
+
...baseUrl ? { baseUrl } : {},
|
|
63
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
64
|
+
};
|
|
65
|
+
await writeProviders(file);
|
|
66
|
+
return providerEnvVar(provider);
|
|
67
|
+
}
|
|
68
|
+
async function removeProviderKey(provider) {
|
|
69
|
+
const file = await loadProviders();
|
|
70
|
+
if (!(provider in file.providers)) return false;
|
|
71
|
+
delete file.providers[provider];
|
|
72
|
+
await writeProviders(file);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
async function injectProviderKeysIntoEnv(env = process.env) {
|
|
76
|
+
const file = await loadProviders();
|
|
77
|
+
const injected = [];
|
|
78
|
+
for (const [provider, entry] of Object.entries(file.providers)) {
|
|
79
|
+
if (!entry?.apiKey) continue;
|
|
80
|
+
const name = providerEnvVar(provider);
|
|
81
|
+
if (env[name]) continue;
|
|
82
|
+
env[name] = entry.apiKey;
|
|
83
|
+
if (entry.baseUrl) env[`${provider.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}_BASE_URL`] = entry.baseUrl;
|
|
84
|
+
injected.push(provider);
|
|
85
|
+
}
|
|
86
|
+
return injected;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { PROVIDER_ENV_VARS, getProviderKey, injectProviderKeysIntoEnv, loadProviders, providerEnvVar, providersPath, removeProviderKey, setProviderKey };
|
|
90
|
+
//# sourceMappingURL=index.mjs.map
|
|
91
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"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","file":"index.mjs","sourcesContent":["/**\n * `~/.agentproto/providers.json` — the provider API-key store (mode 0600).\n *\n * Distinct from `credentials.json` (host-binding OAuth tokens for\n * `serve --connect`) and from per-adapter `setup[]` tokens. This file holds\n * the LLM/model **provider** keys (anthropic, openrouter, openai, …) that the\n * model gateways in spawned agents read from the environment.\n *\n * Why a store at all: before this, the only way to give the daemon a provider\n * key was an ambient `export FOO_API_KEY=…` in whatever shell launched\n * `serve`. That's invisible, easy to forget, and per-shell. Storing the keys\n * here (0600, same-user-only) lets `serve` inject them at boot — set once,\n * works for every daemon. Explicit env still wins (see injectProviderKeysIntoEnv).\n *\n * Keys never leave this file except into the daemon's own process env; they\n * are never logged. A browser-loaded localhost page can't read a 0600 file —\n * the same defence the runtime.json bearer token relies on.\n */\n\nimport { mkdir, readFile, writeFile } from \"node:fs/promises\"\nimport { homedir } from \"node:os\"\nimport { join, resolve } from \"node:path\"\nimport { PROVIDER_KEY_ENV } from \"@agentproto/model-catalog\"\n\n/**\n * Canonical provider → environment-variable name. The spawned model gateways\n * (Mastra in mastra-agent, hermes/opencode's routers) read the provider key\n * from these env names. Aligned with the adapter manifests' `models.env`\n * maps + the common SDK conventions.\n *\n * DERIVED from `@agentproto/model-catalog`'s {@link PROVIDER_KEY_ENV} (the\n * single source of truth, co-located with the provider enum — DECISION 1) so\n * the two maps that used to restate the same fact can't drift, plus the few\n * NON-catalog gateway providers this store also fronts (`groq`,\n * `vercel-ai-gateway`) which aren't LLM-catalog `provider` values but still\n * carry a key here. Adding a catalog provider forces adding its key env in the\n * catalog (the `Record<CatalogProvider, string>` is exhaustive), and it flows\n * through to here for free.\n */\nexport const PROVIDER_ENV_VARS: Readonly<Record<string, string>> = {\n ...PROVIDER_KEY_ENV,\n // ── Non-catalog gateway providers (not LLM-catalog `provider` enum values,\n // so absent from PROVIDER_KEY_ENV, but still keyed through this store) ──\n groq: \"GROQ_API_KEY\",\n xai: \"XAI_API_KEY\",\n // Vercel AI Gateway — a gateway provider, like openrouter, that fronts many\n // upstream model families behind one key.\n \"vercel-ai-gateway\": \"AI_GATEWAY_API_KEY\",\n}\n\nexport type KnownProvider = keyof typeof PROVIDER_ENV_VARS\n\nexport interface ProviderEntry {\n /** The provider API key (or gateway key). */\n apiKey: string\n /** Optional custom base URL (self-hosted / proxy). */\n baseUrl?: string\n /** Wall-clock ISO timestamp the key was last set. */\n updatedAt: string\n}\n\nexport interface ProvidersFile {\n version: 1\n providers: Record<string, ProviderEntry>\n}\n\n/** Fresh empty file each call — never share the `providers` object, or a\n * later `setProviderKey` mutation leaks into every other load in-process. */\nfunction emptyFile(): ProvidersFile {\n return { version: 1, providers: {} }\n}\n\nexport function providersPath(): string {\n return resolve(homedir(), \".agentproto\", \"providers.json\")\n}\n\n/** Resolve the env-var name for a provider (canonical map, else\n * `<PROVIDER>_API_KEY` upper-snake fallback so new providers still work). */\nexport function providerEnvVar(provider: string): string {\n return (\n PROVIDER_ENV_VARS[provider] ??\n `${provider.toUpperCase().replace(/[^A-Z0-9]+/g, \"_\")}_API_KEY`\n )\n}\n\nexport async function loadProviders(): Promise<ProvidersFile> {\n try {\n const raw = await readFile(providersPath(), \"utf8\")\n const parsed = JSON.parse(raw) as unknown\n if (\n !parsed ||\n typeof parsed !== \"object\" ||\n !(\"providers\" in parsed) ||\n typeof (parsed as Record<string, unknown>).providers !== \"object\" ||\n (parsed as Record<string, unknown>).providers === null\n ) {\n return emptyFile()\n }\n return {\n version: 1,\n providers: (parsed as ProvidersFile).providers,\n }\n } catch {\n return emptyFile() // ENOENT / malformed → empty\n }\n}\n\n/**\n * Read a single provider's stored api key from `providers.json`, or undefined\n * when the provider has no entry (or the file doesn't exist). The EXPLICIT,\n * verifiable credential source for the billing-auth resolver's api-key mode\n * (`agentproto auth provider set <provider> <key>` writes it) — distinct from\n * the ambient shell env, which the resolver never reads. Returns the raw key;\n * only a non-secret fingerprint of it is ever surfaced back to a caller.\n */\nexport async function getProviderKey(provider: string): Promise<string | undefined> {\n const file = await loadProviders()\n const entry = file.providers[provider]\n return entry?.apiKey || undefined\n}\n\nasync function writeProviders(file: ProvidersFile): Promise<void> {\n const dir = join(homedir(), \".agentproto\")\n await mkdir(dir, { recursive: true })\n // mode 0600 so other local users can't read the keys.\n await writeFile(providersPath(), JSON.stringify(file, null, 2) + \"\\n\", {\n encoding: \"utf8\",\n mode: 0o600,\n })\n}\n\n/** Set (or replace) a provider's key. Returns the env-var it maps to. */\nexport async function setProviderKey(\n provider: string,\n apiKey: string,\n baseUrl?: string,\n): Promise<string> {\n const file = await loadProviders()\n file.providers[provider] = {\n apiKey,\n ...(baseUrl ? { baseUrl } : {}),\n updatedAt: new Date().toISOString(),\n }\n await writeProviders(file)\n return providerEnvVar(provider)\n}\n\n/** Remove a provider's key. Returns true if it existed. */\nexport async function removeProviderKey(provider: string): Promise<boolean> {\n const file = await loadProviders()\n if (!(provider in file.providers)) return false\n delete file.providers[provider]\n await writeProviders(file)\n return true\n}\n\n/**\n * Inject stored provider keys into a target env (default `process.env`).\n * **Explicit env always wins** — a var already set is never overwritten, so a\n * one-off `FOO_API_KEY=… serve` or a CI secret takes precedence over the\n * store. Returns the list of provider names actually injected (for a boot log\n * that names providers, never values).\n */\nexport async function injectProviderKeysIntoEnv(\n env: NodeJS.ProcessEnv = process.env,\n): Promise<string[]> {\n const file = await loadProviders()\n const injected: string[] = []\n for (const [provider, entry] of Object.entries(file.providers)) {\n if (!entry?.apiKey) continue\n const name = providerEnvVar(provider)\n if (env[name]) continue // explicit env wins\n env[name] = entry.apiKey\n if (entry.baseUrl) env[`${provider.toUpperCase().replace(/[^A-Z0-9]+/g, \"_\")}_BASE_URL`] = entry.baseUrl\n injected.push(provider)\n }\n return injected\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentproto/providers-store",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "@agentproto/providers-store — the `~/.agentproto/providers.json` LLM provider API-key store (mode 0600) and the `injectProviderKeysIntoEnv` helper that lets any process (daemon or standalone gateway) boot with keys set via `agentproto auth provider set`, without ever logging a value. Explicit env always wins.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agentproto",
|
|
7
|
+
"providers",
|
|
8
|
+
"api-key",
|
|
9
|
+
"secrets",
|
|
10
|
+
"llm",
|
|
11
|
+
"open-standard"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://agentproto.sh",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/agentproto/ts",
|
|
17
|
+
"directory": "packages/providers-store"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/agentproto/ts/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "dist/index.mjs",
|
|
25
|
+
"module": "dist/index.mjs",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"default": "./dist/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@agentproto/model-catalog": "0.3.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^25.6.2",
|
|
48
|
+
"tsup": "^8.5.1",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"vitest": "^3.2.4",
|
|
51
|
+
"@agentproto/tooling": "0.1.0-alpha.0"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=20.9.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"dev": "tsup --watch",
|
|
58
|
+
"build": "tsup",
|
|
59
|
+
"clean": "rm -rf dist",
|
|
60
|
+
"check-types": "tsc --noEmit",
|
|
61
|
+
"test": "vitest run --passWithNoTests",
|
|
62
|
+
"test:watch": "vitest"
|
|
63
|
+
}
|
|
64
|
+
}
|