@vinaverse/server 0.0.0-stage → 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +165 -2
- package/dist/api.cjs +528 -0
- package/dist/api.cjs.map +1 -0
- package/dist/api.d.cts +453 -0
- package/dist/api.d.ts +453 -0
- package/dist/api.js +516 -0
- package/dist/api.js.map +1 -0
- package/dist/chunk-FEEJNQXV.js +64 -0
- package/dist/chunk-FEEJNQXV.js.map +1 -0
- package/dist/chunk-GTPMQKGX.js +277 -0
- package/dist/chunk-GTPMQKGX.js.map +1 -0
- package/dist/chunk-NPXO6SMZ.js +84 -0
- package/dist/chunk-NPXO6SMZ.js.map +1 -0
- package/dist/chunk-SDJFZPHJ.js +141 -0
- package/dist/chunk-SDJFZPHJ.js.map +1 -0
- package/dist/express.cjs +397 -0
- package/dist/express.cjs.map +1 -0
- package/dist/express.d.cts +43 -0
- package/dist/express.d.ts +43 -0
- package/dist/express.js +87 -0
- package/dist/express.js.map +1 -0
- package/dist/fastify.cjs +363 -0
- package/dist/fastify.cjs.map +1 -0
- package/dist/fastify.d.cts +62 -0
- package/dist/fastify.d.ts +62 -0
- package/dist/fastify.js +54 -0
- package/dist/fastify.js.map +1 -0
- package/dist/hono.cjs +421 -0
- package/dist/hono.cjs.map +1 -0
- package/dist/hono.d.cts +21 -0
- package/dist/hono.d.ts +21 -0
- package/dist/hono.js +34 -0
- package/dist/hono.js.map +1 -0
- package/dist/index.cjs +1481 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +196 -0
- package/dist/index.d.ts +196 -0
- package/dist/index.js +920 -0
- package/dist/index.js.map +1 -0
- package/dist/lambda.cjs +405 -0
- package/dist/lambda.cjs.map +1 -0
- package/dist/lambda.d.cts +53 -0
- package/dist/lambda.d.ts +53 -0
- package/dist/lambda.js +86 -0
- package/dist/lambda.js.map +1 -0
- package/dist/nonces-scwljZwl.d.cts +92 -0
- package/dist/nonces-scwljZwl.d.ts +92 -0
- package/dist/provider-BqfTvF3g.d.ts +2016 -0
- package/dist/provider-Miaa9V7I.d.cts +2016 -0
- package/dist/stores.cjs +315 -0
- package/dist/stores.cjs.map +1 -0
- package/dist/stores.d.cts +52 -0
- package/dist/stores.d.ts +52 -0
- package/dist/stores.js +306 -0
- package/dist/stores.js.map +1 -0
- package/dist/testing.cjs +135 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +29 -0
- package/dist/testing.d.ts +29 -0
- package/dist/testing.js +49 -0
- package/dist/testing.js.map +1 -0
- package/dist/vinaverse-v1.d-BVc4uyUd.d.cts +3460 -0
- package/dist/vinaverse-v1.d-BVc4uyUd.d.ts +3460 -0
- package/package.json +225 -3
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
CHANGED
|
@@ -1,3 +1,166 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @vinaverse/server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vinaverse for Node backends: call the Vinaverse API typed with a service key, verify webhooks into typed events,
|
|
4
|
+
and serve the tools the agent calls with the contract enforced, with adapters for AWS Lambda, Express, Fastify, Hono
|
|
5
|
+
and any Fetch API runtime. Runs on Node 18+, AWS Lambda and edge runtimes: it uses Web APIs only (Web Crypto, never a
|
|
6
|
+
Node-only API).
|
|
7
|
+
|
|
8
|
+
Pre-release (`0.x`): it targets the contract named in `CONTRACT`. Its types need TypeScript 5.0 or newer (the gate
|
|
9
|
+
compiles a consumer against the published declarations with 5.0). Errors it throws carry a `name`
|
|
10
|
+
(`WebCryptoUnavailableError`, `RawBodyRequiredError`): check `error.name`, since each entry point and module format
|
|
11
|
+
has its own copy of the class.
|
|
12
|
+
|
|
13
|
+
## Verify a delivery
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { verifyWebhook } from '@vinaverse/server';
|
|
17
|
+
|
|
18
|
+
const result = await verifyWebhook({
|
|
19
|
+
rawBody, // the body exactly as received: string, Uint8Array/Buffer or ArrayBuffer
|
|
20
|
+
header: request.headers['vinaverse-signature'],
|
|
21
|
+
secret: process.env.VINAVERSE_WEBHOOK_SECRET, // the whole vv_whsec_… value; two while rotating
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!result.ok) return reply(400); // result.error.code says why
|
|
25
|
+
if (result.kind === 'event') {
|
|
26
|
+
switch (result.event.type) {
|
|
27
|
+
case 'conversation.completed':
|
|
28
|
+
result.event.data.answers; // typed ConversationState
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return reply(200); // also for 'test' (a Studio test delivery) and 'unrecognized' (a newer event type)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Deliveries are at least once: deduplicate on `event.id`. Answer within 5 seconds and do the work afterwards.
|
|
36
|
+
|
|
37
|
+
## Adapters
|
|
38
|
+
|
|
39
|
+
Each answers 200 when handled, 400 when refused, 413 over `maxBodyBytes` (default 1 MiB), and lets your handler's
|
|
40
|
+
error through so Vinaverse retries.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// AWS Lambda: API Gateway REST (v1), HTTP API (v2) or a function URL; base64 bodies handled
|
|
44
|
+
import { createLambdaWebhookHandler } from '@vinaverse/server/lambda';
|
|
45
|
+
export const handler = createLambdaWebhookHandler({ secret, onEvent: async (event) => { /* … */ } });
|
|
46
|
+
|
|
47
|
+
// Express: mount before express.json() (or behind express.raw({ type: 'application/json' }))
|
|
48
|
+
import { vinaverseWebhook } from '@vinaverse/server/express';
|
|
49
|
+
app.post('/webhooks/vinaverse', vinaverseWebhook({ secret, onEvent }));
|
|
50
|
+
|
|
51
|
+
// Fastify: the plugin reads its route's body raw; the rest of the app keeps its JSON parser
|
|
52
|
+
import { vinaverseWebhookPlugin } from '@vinaverse/server/fastify';
|
|
53
|
+
await app.register(vinaverseWebhookPlugin({ path: '/webhooks/vinaverse', secret, onEvent }));
|
|
54
|
+
|
|
55
|
+
// Hono
|
|
56
|
+
import { vinaverseWebhook } from '@vinaverse/server/hono';
|
|
57
|
+
app.post('/webhooks/vinaverse', vinaverseWebhook({ secret, onEvent }));
|
|
58
|
+
|
|
59
|
+
// Next.js route handlers, Cloudflare Workers, Deno, Bun: any Request → Response runtime
|
|
60
|
+
import { createFetchWebhookHandler } from '@vinaverse/server';
|
|
61
|
+
export const POST = createFetchWebhookHandler({ secret, onEvent });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`onTest`, `onUnrecognized` and `onRejected` are optional.
|
|
65
|
+
|
|
66
|
+
## Order a conversation's states
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { applyConversationState, missingSequences } from '@vinaverse/server';
|
|
70
|
+
|
|
71
|
+
const { record, action } = applyConversationState(stored, event.data);
|
|
72
|
+
// action: 'apply' or 'stale' (an equal or lower sequence, even under a new envelope id)
|
|
73
|
+
// record: what to store: { state, seen, fullReadNeeded }. fullReadNeeded: the webhook left out the answers
|
|
74
|
+
// (over 64 KB); read GET /v1/conversations/{id}. missingSequences(record): the states still to come.
|
|
75
|
+
// Deduplicate deliveries on event.id first; ConversationReceiver does all of it in memory, for one process.
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Serve your tools
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { createToolProvider, MemoryNonceStore, MemoryIdempotencyStore, ToolRefusal } from '@vinaverse/server';
|
|
82
|
+
import { redisNonceStore, redisIdempotencyStore } from '@vinaverse/server/stores'; // shared, for several instances
|
|
83
|
+
|
|
84
|
+
const tools = createToolProvider({
|
|
85
|
+
secret: process.env.VINAVERSE_TOOL_SECRET, // the whole tool signing secret; two while rotating
|
|
86
|
+
nonces: new MemoryNonceStore(), // or redisNonceStore({ command: (args) => redis.sendCommand(args) })
|
|
87
|
+
idempotency: new MemoryIdempotencyStore(), // or the matching shared store
|
|
88
|
+
basePath: '/api/v2/ai', // the path of the tool base URL registered in Studio
|
|
89
|
+
authenticate: async (token) => (await verifyMyToken(token)) ?? throwExpired(), // your principal
|
|
90
|
+
handlers: {
|
|
91
|
+
get_flow_definition: ({ params, query }) => myFlows.definition(params.flow, query.version),
|
|
92
|
+
submit_flow: async ({ params, body, principal }) => {
|
|
93
|
+
const problem = await myFlows.check(params.flow, body);
|
|
94
|
+
if (problem) throw ToolRefusal.flowRefused(problem.code, problem.questionId);
|
|
95
|
+
await myFlows.store(principal, body); // returning nothing accepts it
|
|
96
|
+
},
|
|
97
|
+
create_handoff: ({ body, principal }) => mySupport.openTicket(principal, body),
|
|
98
|
+
},
|
|
99
|
+
leaseSeconds: 60, // above the longest a handler can run
|
|
100
|
+
onRefused: (reason) => log.warn({ reason }, 'tool call signature refused'),
|
|
101
|
+
onError: (error, operation) => log.error({ error, operation }),
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A `submit_flow` handler refuses only with `ToolRefusal.flowRefused` (any other 422 is answered as a 500), and every
|
|
106
|
+
handler validates what it acts on. Idempotent answers are kept under a SHA-256 fingerprint of the request, never the
|
|
107
|
+
request itself; `scope(principal)` keeps them apart per tenant or environment.
|
|
108
|
+
|
|
109
|
+
Then mount it: `createLambdaToolHandler(tools)` (`/lambda`), `app.use('/api/v2/ai', vinaverseTools(tools))`
|
|
110
|
+
(`/express`), `vinaverseToolsPlugin({ provider: tools, basePath })` (`/fastify`), `vinaverseTools(tools)` (`/hono`)
|
|
111
|
+
or `createFetchToolHandler(tools)`. Signatures, nonces, token presence, sizes, refusals and idempotent submissions,
|
|
112
|
+
proposals and handoffs are handled; your handlers decide what an answer or a ticket means. Details:
|
|
113
|
+
[docs/architecture/tools.md](../../docs/architecture/tools.md).
|
|
114
|
+
|
|
115
|
+
## Test your route
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { signToolCall, signWebhook } from '@vinaverse/server/testing';
|
|
119
|
+
const header = await signWebhook({ body, secret }); // the Vinaverse-Signature value Vinaverse would send
|
|
120
|
+
const toolHeader = await signToolCall({ secret, method: 'POST', target, token, body }); // a tool call's
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## The Vinaverse API (`@vinaverse/server/api`)
|
|
124
|
+
|
|
125
|
+
Typed calls to the Vinaverse API with a service key, generated from the pinned public contract.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { createApiClient, VinaverseApiError } from '@vinaverse/server/api';
|
|
129
|
+
|
|
130
|
+
const vinaverse = createApiClient({ apiKey: process.env.VINAVERSE_API_KEY }); // vv_sk_<mock|dev|live>_…
|
|
131
|
+
|
|
132
|
+
// Your app asks your backend for a session; give it the session's id, URLs and client_secret only.
|
|
133
|
+
const session = await vinaverse.sessions.create({ agent: 'intake', subject, tool_access }); // your agent's id
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
await vinaverse.moderation.createJob({ policy: 'default', items });
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof VinaverseApiError && error.code === 'invalid_request') console.log(error.errors);
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
- **Idempotency keys** are added to the calls that take one (`sessions.create`, `moderation.createJob`,
|
|
143
|
+
`media.createJob`, `documentChecks.create`, `subjects.erase`); pass `{ idempotencyKey }` to use your own.
|
|
144
|
+
- **Retries are safe:** a `429` is retried after its `Retry-After`; a `5xx`, a retryable problem, a timeout or a
|
|
145
|
+
network error is retried only for a read or a call with an idempotency key. Default 2 retries, 30 s timeout per try
|
|
146
|
+
(reading the body included), and a `Retry-After` over 30 s is not waited for (`maxRetries`, `timeout`,
|
|
147
|
+
`maxRetryDelay`, checked when the client is made).
|
|
148
|
+
- **Trying again yourself:** when a keyed call still fails (`VinaverseTimeoutError`, `VinaverseNetworkError`, or a
|
|
149
|
+
retryable `VinaverseApiError`), its error carries `idempotencyKey`. Send the call again later with
|
|
150
|
+
`{ idempotencyKey: error.idempotencyKey }`: the API does it once, however many times it was sent.
|
|
151
|
+
- **Aborts:** pass `{ signal }`; aborting stops the call at once, during a try, a body read or a wait between tries,
|
|
152
|
+
and an already aborted signal sends nothing.
|
|
153
|
+
- **Refusals throw `VinaverseApiError`**, with the problem's `status`, `code`, `retryable`, `requestId`, `retryAfter`
|
|
154
|
+
and, for `invalid_request`, the field `errors`.
|
|
155
|
+
- **Every operation** is also on `vinaverse.api`, the typed openapi-fetch client (`api.GET('/v1/usage')`).
|
|
156
|
+
- **Backend only.** The client refuses a client secret, and refuses to start in a browser or in React Native: a
|
|
157
|
+
service key there ships to everyone who loads the app. Apps use `@vinaverse/client` with the session's client secret.
|
|
158
|
+
- The base URL follows the key: `https://api.vinaverse.com` for a live key, `https://api-dev.vinaverse.com` for mock
|
|
159
|
+
and dev keys. A `baseUrl` of your own must be `https:` (or `http:` on `localhost` for tests), and redirects are
|
|
160
|
+
refused: a service key never travels in clear or follows a redirect. Every request, the raw `api` client's included,
|
|
161
|
+
goes to that origin only. In the mock environment, the `Vinaverse-Mock-Scenario` header (`headers`
|
|
162
|
+
option) provokes a `429` or a `503` to test your handling.
|
|
163
|
+
|
|
164
|
+
Details: [docs/architecture/webhooks.md](../../docs/architecture/webhooks.md),
|
|
165
|
+
[docs/architecture/tools.md](../../docs/architecture/tools.md) and
|
|
166
|
+
[docs/architecture/api-client.md](../../docs/architecture/api-client.md). Licence: Apache-2.0.
|