@hermoso/core 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 +202 -0
- package/README.md +65 -2
- package/package.json +66 -4
- package/src/connectors.mjs +117 -0
- package/src/contract.md +24 -0
- package/src/errors.mjs +25 -0
- package/src/files.mjs +155 -0
- package/src/hooks.mjs +69 -0
- package/src/hosted.mjs +119 -0
- package/src/http.mjs +146 -0
- package/src/index.mjs +33 -0
- package/src/jobs.mjs +193 -0
- package/src/providers/adapters/_kit.mjs +224 -0
- package/src/providers/adapters/_mock-http.mjs +29 -0
- package/src/providers/adapters/alibaba.mjs +71 -0
- package/src/providers/adapters/alibaba.test.mjs +45 -0
- package/src/providers/adapters/bfl.mjs +80 -0
- package/src/providers/adapters/bfl.test.mjs +39 -0
- package/src/providers/adapters/bria.mjs +46 -0
- package/src/providers/adapters/bria.test.mjs +27 -0
- package/src/providers/adapters/byteplus.mjs +75 -0
- package/src/providers/adapters/byteplus.test.mjs +71 -0
- package/src/providers/adapters/elevenlabs.mjs +33 -0
- package/src/providers/adapters/elevenlabs.test.mjs +22 -0
- package/src/providers/adapters/fal.mjs +103 -0
- package/src/providers/adapters/fal.test.mjs +73 -0
- package/src/providers/adapters/heygen.mjs +40 -0
- package/src/providers/adapters/heygen.test.mjs +29 -0
- package/src/providers/adapters/ideogram.mjs +66 -0
- package/src/providers/adapters/ideogram.test.mjs +35 -0
- package/src/providers/adapters/index.mjs +13 -0
- package/src/providers/adapters/kling.mjs +102 -0
- package/src/providers/adapters/kling.test.mjs +59 -0
- package/src/providers/adapters/krea.mjs +37 -0
- package/src/providers/adapters/krea.test.mjs +23 -0
- package/src/providers/adapters/luma.mjs +54 -0
- package/src/providers/adapters/luma.test.mjs +28 -0
- package/src/providers/adapters/minimax.mjs +111 -0
- package/src/providers/adapters/minimax.test.mjs +57 -0
- package/src/providers/adapters/pixverse.mjs +43 -0
- package/src/providers/adapters/pixverse.test.mjs +33 -0
- package/src/providers/adapters/recraft.mjs +31 -0
- package/src/providers/adapters/recraft.test.mjs +21 -0
- package/src/providers/adapters/replicate.mjs +46 -0
- package/src/providers/adapters/replicate.test.mjs +30 -0
- package/src/providers/adapters/runway.mjs +174 -0
- package/src/providers/adapters/runway.test.mjs +40 -0
- package/src/providers/adapters/stability.mjs +34 -0
- package/src/providers/adapters/stability.test.mjs +25 -0
- package/src/providers/adapters/vidu.mjs +39 -0
- package/src/providers/adapters/vidu.test.mjs +27 -0
- package/src/providers/adapters/xai.mjs +46 -0
- package/src/providers/adapters/xai.test.mjs +29 -0
- package/src/providers/anthropic.mjs +126 -0
- package/src/providers/google.mjs +134 -0
- package/src/providers/hosted.mjs +133 -0
- package/src/providers/index.mjs +100 -0
- package/src/providers/mock.mjs +29 -0
- package/src/providers/openai.mjs +128 -0
- package/src/providers/util.mjs +277 -0
- package/src/storage.mjs +169 -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 2026 Hermoso
|
|
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
CHANGED
|
@@ -1,3 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hermoso/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The shared contract for every Hermoso package (`src/contract.md`). ESM, Node 22.5+, no required dependencies.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import { providers, hooks, setHooks, hostedClient, credentialStore, queue, blobStore, fetchSafe } from '@hermoso/core';
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Providers
|
|
10
|
+
|
|
11
|
+
One interface for creative models. The default adapter is **hosted Hermoso** (`HERMOSO_API_KEY`). Bring-your-own-key adapters call a model maker's or
|
|
12
|
+
aggregator's own public API with your key; [docs/providers.md](../../docs/providers.md) lists every one.
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
const img = await providers.image.generate({ prompt: 'a red sneaker on wet asphalt', aspect: '4:5' });
|
|
16
|
+
const vid = await providers.video.generate({ prompt: 'slow push-in', refImage: img.url, durationSeconds: 8, aspect: '9:16' });
|
|
17
|
+
const vo = await providers.voice.speak({ text: 'Built for rain.', voice: 'alloy' });
|
|
18
|
+
const out = await providers.text.complete({ system, messages: [{ role: 'user', content: 'Three hooks' }], schema });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Messages take images: `content` is a string or blocks, `{ type: 'text', text }` and `{ type: 'image', url }` or
|
|
22
|
+
`{ type: 'image', data, mimeType }`. Every text adapter sends them to the model as images.
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
await providers.text.complete({ messages: [{ role: 'user', content: [{ type: 'text', text: 'What is on this label?' }, { type: 'image', url: 'https://example.com/label.jpg' }] }] });
|
|
26
|
+
const edit = await providers.video.generate({ prompt: 'make the mug red', video: 'https://example.com/clip.mp4' }); // video-to-video (providers with videoEdit)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Media results are `{ kind, url, data, mimeType, model, provider }` (hosted answers a `url`, first-party APIs often
|
|
30
|
+
answer `data` bytes: store them with `blobStore.put`). Text results are `{ text, json, toolCalls, model, provider }`.
|
|
31
|
+
Every adapter declares `capabilities` per kind (`models`, `aspects`, `durations`, `refsMax`, `audio`, `schema`,
|
|
32
|
+
`tools`); a request outside them fails before anything is sent.
|
|
33
|
+
|
|
34
|
+
A prompt names its attached references with neutral tokens: `{ref:image:1}`, `{ref:video:1}`, `{ref:element:2}`
|
|
35
|
+
(numbered in the order the references are sent). Each provider renders them in the syntax its API reads; a provider
|
|
36
|
+
that declares no `refSyntax` gets plain words ("reference image 1"). Build one with `refToken('image', 1)`.
|
|
37
|
+
|
|
38
|
+
Choose adapters per kind with `configureProviders({ image: '<adapter id>', text: '<adapter id>' })` or env
|
|
39
|
+
(`HERMOSO_PROVIDER`, `HERMOSO_IMAGE_PROVIDER`, ...). Add your own with `registerProvider(id, factory)`.
|
|
40
|
+
`createMockProvider()` answers instantly for tests.
|
|
41
|
+
|
|
42
|
+
## Hooks
|
|
43
|
+
|
|
44
|
+
No-ops in the open build; a host injects real ones once with `setHooks`.
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
const hold = await hooks.billing.reserve({ op: 'video.generate', units: { clips: 1, seconds: 8 } });
|
|
48
|
+
if (!hold.ok) throw new Error(hold.reason);
|
|
49
|
+
try { /* work */ await hooks.billing.settle(hold.holdId, { units: { clips: 1, seconds: 8 } }); }
|
|
50
|
+
catch (e) { await hooks.billing.release(hold.holdId); throw e; }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`units` is always an object of counts named for the work (`{ images: 2 }`, `{ clips: 3, seconds: 24 }`, `{ calls: 1 }`),
|
|
54
|
+
never a price. `hooks.abuse.check({ event, account, ip, meta })` answers `{ allow }`; `hooks.audit.record(event)` records.
|
|
55
|
+
|
|
56
|
+
## The rest
|
|
57
|
+
|
|
58
|
+
- `hostedClient({ apiKey, baseUrl })`: the Hermoso REST API (`createGeneration`, `waitForJob`, `callTool`, `uploadMedia`, ...).
|
|
59
|
+
- `credentialStore`: `get/set/delete/list(account, provider)`, AES-256-GCM file by default (`HERMOSO_SECRET_KEY`).
|
|
60
|
+
- `queue`: `enqueue/claim/complete/fail` with `runAt`, retries, leases and idempotency keys; SQLite by default.
|
|
61
|
+
- `blobStore`: `put/get/url/delete`, local disk or any S3-compatible bucket (`S3_*`).
|
|
62
|
+
- `fetchSafe(url, init)`: fetch with every redirect hop checked against private and internal addresses.
|
|
63
|
+
|
|
64
|
+
Errors are `HermosoError` with a stable `code` (`not_configured`, `unsupported`, `blocked_host`, `refused`, ...).
|
|
65
|
+
|
|
66
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hermoso/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared core for the open-source Hermoso marketing platform: provider adapters (hosted Hermoso by default, or bring your own key for any model maker or aggregator), host hooks, the Hermoso REST client, an encrypted credential store, a durable job queue, blob storage and an SSRF-guarded fetch.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ai-providers",
|
|
7
|
+
"openai",
|
|
8
|
+
"anthropic",
|
|
9
|
+
"gemini",
|
|
10
|
+
"job-queue",
|
|
11
|
+
"sqlite",
|
|
12
|
+
"s3",
|
|
13
|
+
"ssrf",
|
|
14
|
+
"marketing-platform",
|
|
15
|
+
"hermoso",
|
|
16
|
+
"open-source"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/hermoso-ai/hermoso/tree/main/packages/core#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/hermoso-ai/hermoso/issues"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/hermoso-ai/hermoso.git",
|
|
25
|
+
"directory": "packages/core"
|
|
26
|
+
},
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"author": "Hermoso <hello@hermoso.ai>",
|
|
29
|
+
"type": "module",
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22.5"
|
|
32
|
+
},
|
|
33
|
+
"main": "./src/index.mjs",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./src/index.mjs",
|
|
36
|
+
"./hooks": "./src/hooks.mjs",
|
|
37
|
+
"./providers": "./src/providers/index.mjs",
|
|
38
|
+
"./hosted": "./src/hosted.mjs",
|
|
39
|
+
"./connectors": "./src/connectors.mjs",
|
|
40
|
+
"./jobs": "./src/jobs.mjs",
|
|
41
|
+
"./storage": "./src/storage.mjs",
|
|
42
|
+
"./files": "./src/files.mjs",
|
|
43
|
+
"./http": "./src/http.mjs",
|
|
44
|
+
"./errors": "./src/errors.mjs"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"src",
|
|
48
|
+
"README.md",
|
|
49
|
+
"LICENSE"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"test": "node --test test/*.test.mjs"
|
|
53
|
+
},
|
|
54
|
+
"optionalDependencies": {
|
|
55
|
+
"@anthropic-ai/sdk": "^0.100.1"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"better-sqlite3": ">=11"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"better-sqlite3": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import fsSync from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { HermosoError } from './errors.mjs';
|
|
7
|
+
|
|
8
|
+
export const defaultDataDir = () => process.env.HERMOSO_DATA_DIR || path.join(os.homedir(), '.hermoso-open');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The account a call uses when it names none: HERMOSO_ACCOUNT, else "default". A self-hosted install is usually one
|
|
12
|
+
* workspace, so "omit for the default" means this id everywhere (publishing, ads, analytics, commerce).
|
|
13
|
+
*/
|
|
14
|
+
export const DEFAULT_ACCOUNT = 'default';
|
|
15
|
+
export const defaultAccountId = (env = process.env) => (env.HERMOSO_ACCOUNT && String(env.HERMOSO_ACCOUNT).trim()) || DEFAULT_ACCOUNT;
|
|
16
|
+
|
|
17
|
+
function keyFrom(secret) {
|
|
18
|
+
const s = String(secret).trim();
|
|
19
|
+
if (/^[0-9a-f]{64}$/i.test(s)) return Buffer.from(s, 'hex');
|
|
20
|
+
const b = Buffer.from(s, 'base64');
|
|
21
|
+
if (b.length === 32 && /^[A-Za-z0-9+/=_-]+$/.test(s)) return b;
|
|
22
|
+
return crypto.scryptSync(s, 'hermoso-credential-store', 32);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function loadOrCreateKeyFile(file) {
|
|
26
|
+
try { return Buffer.from(fsSync.readFileSync(file, 'utf8').trim(), 'hex'); } catch (e) { if (e.code !== 'ENOENT') throw e; }
|
|
27
|
+
fsSync.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
28
|
+
const key = crypto.randomBytes(32);
|
|
29
|
+
fsSync.writeFileSync(file, key.toString('hex') + '\n', { mode: 0o600, flag: 'wx' });
|
|
30
|
+
return key;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const validId = (s, what) => {
|
|
34
|
+
if (what === 'account' && (s == null || s === '')) return defaultAccountId();
|
|
35
|
+
if (typeof s !== 'string' || !s || s.length > 200) throw new HermosoError(`${/^[aeiou]/i.test(what) ? 'An' : 'A'} ${what} id is required.`, { code: 'bad_request', param: what });
|
|
36
|
+
return s;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function encrypt(key, plaintext) {
|
|
40
|
+
const iv = crypto.randomBytes(12);
|
|
41
|
+
const c = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
42
|
+
const data = Buffer.concat([c.update(plaintext, 'utf8'), c.final()]);
|
|
43
|
+
return { v: 1, alg: 'aes-256-gcm', iv: iv.toString('base64'), tag: c.getAuthTag().toString('base64'), data: data.toString('base64') };
|
|
44
|
+
}
|
|
45
|
+
export function decrypt(key, box) {
|
|
46
|
+
const d = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(box.iv, 'base64'));
|
|
47
|
+
d.setAuthTag(Buffer.from(box.tag, 'base64'));
|
|
48
|
+
return Buffer.concat([d.update(Buffer.from(box.data, 'base64')), d.final()]).toString('utf8');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The default store: one encrypted file.
|
|
53
|
+
* @param {object} [o]
|
|
54
|
+
* @param {string} [o.file] default <dataDir>/credentials.enc
|
|
55
|
+
* @param {string} [o.secret] default HERMOSO_SECRET_KEY, else a key file at <dataDir>/secret.key
|
|
56
|
+
*/
|
|
57
|
+
export function fileCredentialStore({ dir = defaultDataDir(), file = path.join(dir, 'credentials.enc'), secret = process.env.HERMOSO_SECRET_KEY, keyFile = path.join(dir, 'secret.key') } = {}) {
|
|
58
|
+
let key = null;
|
|
59
|
+
const getKey = () => (key ||= secret ? keyFrom(secret) : loadOrCreateKeyFile(keyFile));
|
|
60
|
+
let chain = Promise.resolve();
|
|
61
|
+
const serial = (fn) => { const p = chain.then(fn, fn); chain = p.catch(() => {}); return p; };
|
|
62
|
+
|
|
63
|
+
async function read() {
|
|
64
|
+
let raw;
|
|
65
|
+
try { raw = await fs.readFile(file, 'utf8'); } catch (e) { if (e.code === 'ENOENT') return {}; throw e; }
|
|
66
|
+
try { return JSON.parse(decrypt(getKey(), JSON.parse(raw))); } catch (e) {
|
|
67
|
+
throw new HermosoError('The credential store could not be decrypted: the key (HERMOSO_SECRET_KEY or secret.key) does not match the file.', { code: 'decrypt_failed', cause: e });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function write(all) {
|
|
71
|
+
await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
72
|
+
const tmp = `${file}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
73
|
+
await fs.writeFile(tmp, JSON.stringify(encrypt(getKey(), JSON.stringify(all))), { mode: 0o600 });
|
|
74
|
+
await fs.rename(tmp, file);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
kind: 'file',
|
|
78
|
+
get: (account, provider) => serial(async () => (await read())[validId(account, 'account')]?.[validId(provider, 'provider')] ?? null),
|
|
79
|
+
set: (account, provider, value) => serial(async () => {
|
|
80
|
+
const all = await read(); (all[validId(account, 'account')] ||= {})[validId(provider, 'provider')] = value; await write(all);
|
|
81
|
+
}),
|
|
82
|
+
delete: (account, provider) => serial(async () => {
|
|
83
|
+
const a = validId(account, 'account'), p = validId(provider, 'provider');
|
|
84
|
+
const all = await read(); const had = !!all[a]?.[p];
|
|
85
|
+
if (had) { delete all[a][p]; if (!Object.keys(all[a]).length) delete all[a]; await write(all); }
|
|
86
|
+
return had;
|
|
87
|
+
}),
|
|
88
|
+
list: (account) => serial(async () => Object.keys((await read())[validId(account, 'account')] || {})),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** In-memory store (tests, or a host that keeps credentials elsewhere and syncs them in). */
|
|
93
|
+
export function memoryCredentialStore() {
|
|
94
|
+
const m = new Map();
|
|
95
|
+
const k = (a, p) => `${validId(a, 'account')}\u0000${validId(p, 'provider')}`;
|
|
96
|
+
return {
|
|
97
|
+
kind: 'memory',
|
|
98
|
+
async get(a, p) { const v = m.get(k(a, p)); return v === undefined ? null : structuredClone(v); },
|
|
99
|
+
async set(a, p, v) { m.set(k(a, p), structuredClone(v)); },
|
|
100
|
+
async delete(a, p) { return m.delete(k(a, p)); },
|
|
101
|
+
async list(a) { const pre = `${validId(a, 'account')}\u0000`; return [...m.keys()].filter((x) => x.startsWith(pre)).map((x) => x.slice(pre.length)); },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let current = null;
|
|
106
|
+
/** The process-wide store (lazily the file store). Replace it with setCredentialStore(yourStore). */
|
|
107
|
+
export const credentialStore = {
|
|
108
|
+
get: (...a) => (current ||= fileCredentialStore()).get(...a),
|
|
109
|
+
set: (...a) => (current ||= fileCredentialStore()).set(...a),
|
|
110
|
+
delete: (...a) => (current ||= fileCredentialStore()).delete(...a),
|
|
111
|
+
list: (...a) => (current ||= fileCredentialStore()).list(...a),
|
|
112
|
+
};
|
|
113
|
+
export function setCredentialStore(store) {
|
|
114
|
+
for (const f of ['get', 'set', 'delete', 'list']) if (typeof store?.[f] !== 'function') throw new TypeError(`setCredentialStore: store.${f} must be a function`);
|
|
115
|
+
current = store;
|
|
116
|
+
return credentialStore;
|
|
117
|
+
}
|
package/src/contract.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# The @hermoso/core contract
|
|
2
|
+
|
|
3
|
+
Every Hermoso package builds on this contract. A change to it updates every package in the same change.
|
|
4
|
+
|
|
5
|
+
All packages are ESM, Node 22.5+, Apache-2.0. Shared pieces are imported only from `@hermoso/core`.
|
|
6
|
+
|
|
7
|
+
- `hooks`: hosted-only concerns, no-ops in the open build:
|
|
8
|
+
- `hooks.billing.reserve({op, units, meta}) -> {ok:true, holdId:null}` / `hooks.billing.settle(holdId, {units, usage?})` / `hooks.billing.release(holdId)`
|
|
9
|
+
- `units` is always an object of counts named for the work: `{images: 2}`, `{clips: 3, seconds: 24}`, `{searches: 4, profileReads: 12}`, `{calls: 1}`. Never a price. A bare number is read as `{calls: n}` (`normalizeUnits`).
|
|
10
|
+
- `hooks.abuse.check({event, account, ip, meta}) -> {allow:true}`
|
|
11
|
+
- `hooks.audit.record(event)` (no-op)
|
|
12
|
+
- `setHooks(partial)` lets a host inject its own implementations (no-ops by default).
|
|
13
|
+
- `providers`: creative model access behind one interface:
|
|
14
|
+
- `providers.image.generate({prompt, refs, aspect, model})`, `providers.video.generate({prompt, refImage, video?, durationSeconds, aspect, audio, model})`, `providers.voice.speak({text, voice})`, `providers.text.complete({system, messages, schema, tools?})`
|
|
15
|
+
- `video` (optional) is a source clip for video-to-video (a URL or bytes); only a provider declaring `videoEdit` takes one (`videoRemix`: OpenAI remixes its own video by id).
|
|
16
|
+
- message `content` is a string or an array of blocks: `{type:'text', text}` and `{type:'image', url}` / `{type:'image', data, mimeType}` (maker-native image blocks are accepted too). Every text adapter passes images through as images, never as JSON text.
|
|
17
|
+
- each provider declares `capabilities` (models, durations, aspects, refsMax, audio, videoEdit, images).
|
|
18
|
+
- a prompt names attached references with neutral tokens, `{ref:<kind>:<n>}` (kind `image`, `video`, `audio` or `element`; n from 1, in the order the references are sent). The `providers` facade renders them with the serving provider's `refSyntax` (a `REF_STYLES` name or `(kind, n) => string`) right before the call; without one they become plain words ("reference image 1"). Packages write only the tokens, never one model's prompt grammar.
|
|
19
|
+
- adapters: `hosted` (HERMOSO_API_KEY -> https://app.hermoso.ai) is the only default. Bring-your-own-key adapters exist for model makers (every file in `providers/` and `providers/adapters/`) and for aggregators, all presented equally: one registry, one description shape, no ranking or ordering beyond the alphabet. Vendor names live only in adapter files and `docs/providers.md`; nothing anywhere ranks the adapters or recommends one over another.
|
|
20
|
+
- `hosted`: `hostedClient({apiKey, baseUrl})` for the Hermoso REST API (default backend for creative, Launch deploy, and one-click platform connections where supported).
|
|
21
|
+
- `connectors`: `credentialStore` interface (get/set/delete per account+provider), default: local encrypted file.
|
|
22
|
+
- `jobs`: `queue` interface (enqueue/claim/complete/fail, runAt), default: in-process + SQLite.
|
|
23
|
+
- `storage`: `blobStore` interface (put/get/url), default: local disk; S3/R2 adapter.
|
|
24
|
+
- `http`: `fetchSafe` (SSRF-guarded fetch, per-hop public-URL check).
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class HermosoError extends Error {
|
|
2
|
+
/**
|
|
3
|
+
* @param {string} message a sentence a person can act on
|
|
4
|
+
* @param {object} [o]
|
|
5
|
+
* @param {string} [o.code] stable machine code, e.g. 'not_configured', 'unsupported', 'blocked_host'
|
|
6
|
+
* @param {number} [o.status] HTTP status when the error came from (or maps to) one
|
|
7
|
+
* @param {string} [o.type] the Hermoso API error family when it came from the hosted API
|
|
8
|
+
* @param {string} [o.param] the offending field
|
|
9
|
+
* @param {string} [o.provider] which provider adapter raised it
|
|
10
|
+
* @param {boolean} [o.retryable]
|
|
11
|
+
*/
|
|
12
|
+
constructor(message, { code = 'error', status, type, param, provider, retryable, requestId, cause, details } = {}) {
|
|
13
|
+
super(message, cause ? { cause } : undefined);
|
|
14
|
+
this.name = 'HermosoError';
|
|
15
|
+
this.code = code;
|
|
16
|
+
if (status !== undefined) this.status = status;
|
|
17
|
+
if (type) this.type = type;
|
|
18
|
+
if (param) this.param = param;
|
|
19
|
+
if (provider) this.provider = provider;
|
|
20
|
+
if (retryable !== undefined) this.retryable = retryable;
|
|
21
|
+
if (requestId) this.requestId = requestId;
|
|
22
|
+
if (details) this.details = details;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const isHermosoError = (e) => e instanceof HermosoError;
|