@cuylabs/agent-runtime-dapr 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +260 -0
- package/dist/chunk-2CEICSJH.js +809 -0
- package/dist/chunk-2FMHOZLU.js +1997 -0
- package/dist/chunk-A34CHK2E.js +283 -0
- package/dist/chunk-DILON56B.js +668 -0
- package/dist/execution/index.d.ts +126 -0
- package/dist/execution/index.js +17 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +27 -0
- package/dist/index-CKTP36vE.d.ts +533 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +65 -0
- package/dist/store-pRLGfYhN.d.ts +97 -0
- package/dist/workflow/index.d.ts +216 -0
- package/dist/workflow/index.js +23 -0
- package/dist/workflow-bridge-C8Z1yr0Y.d.ts +83 -0
- package/package.json +99 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var _otelClient = null;
|
|
3
|
+
async function tryGetOtelPropagation() {
|
|
4
|
+
if (_otelClient === false) return null;
|
|
5
|
+
if (_otelClient) return _otelClient;
|
|
6
|
+
try {
|
|
7
|
+
_otelClient = await import("@opentelemetry/api");
|
|
8
|
+
return _otelClient;
|
|
9
|
+
} catch {
|
|
10
|
+
_otelClient = false;
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var DEFAULT_DAPR_HTTP_ENDPOINT = "http://127.0.0.1:3500";
|
|
15
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 15e3;
|
|
16
|
+
var DEFAULT_MAX_REQUEST_RETRIES = 2;
|
|
17
|
+
var DEFAULT_VERIFY_SIDECAR_ON_START = true;
|
|
18
|
+
var DaprHttpError = class extends Error {
|
|
19
|
+
status;
|
|
20
|
+
constructor(status, message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "DaprHttpError";
|
|
23
|
+
this.status = status;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function trimTrailingSlash(input) {
|
|
27
|
+
return input.replace(/\/+$/, "");
|
|
28
|
+
}
|
|
29
|
+
function ensureNonEmpty(input, label) {
|
|
30
|
+
const trimmed = input.trim();
|
|
31
|
+
if (!trimmed) {
|
|
32
|
+
throw new Error(`${label} must not be empty`);
|
|
33
|
+
}
|
|
34
|
+
return trimmed;
|
|
35
|
+
}
|
|
36
|
+
function ensurePositiveInt(input, label) {
|
|
37
|
+
if (!Number.isFinite(input)) {
|
|
38
|
+
throw new Error(`${label} must be a finite number`);
|
|
39
|
+
}
|
|
40
|
+
const normalized = Math.floor(input);
|
|
41
|
+
if (normalized <= 0) {
|
|
42
|
+
throw new Error(`${label} must be greater than zero`);
|
|
43
|
+
}
|
|
44
|
+
return normalized;
|
|
45
|
+
}
|
|
46
|
+
function ensureNonNegativeInt(input, label) {
|
|
47
|
+
if (!Number.isFinite(input)) {
|
|
48
|
+
throw new Error(`${label} must be a finite number`);
|
|
49
|
+
}
|
|
50
|
+
const normalized = Math.floor(input);
|
|
51
|
+
if (normalized < 0) {
|
|
52
|
+
throw new Error(`${label} must be zero or greater`);
|
|
53
|
+
}
|
|
54
|
+
return normalized;
|
|
55
|
+
}
|
|
56
|
+
function parseJson(text) {
|
|
57
|
+
if (!text) {
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
return JSON.parse(text);
|
|
61
|
+
}
|
|
62
|
+
function isRetryableStatusCode(status) {
|
|
63
|
+
return [408, 425, 429, 500, 502, 503, 504].includes(status);
|
|
64
|
+
}
|
|
65
|
+
function isAbortError(error) {
|
|
66
|
+
return error instanceof Error && error.name === "AbortError";
|
|
67
|
+
}
|
|
68
|
+
function isRetryableError(error) {
|
|
69
|
+
if (isAbortError(error)) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (error instanceof TypeError) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function sleep(ms) {
|
|
78
|
+
return new Promise((resolve) => {
|
|
79
|
+
setTimeout(resolve, ms);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function isDaprConflictError(error) {
|
|
83
|
+
return error instanceof DaprHttpError && error.status === 409;
|
|
84
|
+
}
|
|
85
|
+
var DaprSidecarClient = class {
|
|
86
|
+
stateStoreName;
|
|
87
|
+
daprHttpEndpoint;
|
|
88
|
+
apiToken;
|
|
89
|
+
fetchImpl;
|
|
90
|
+
requestTimeoutMs;
|
|
91
|
+
maxRequestRetries;
|
|
92
|
+
verifySidecarOnStart;
|
|
93
|
+
constructor(options) {
|
|
94
|
+
this.daprHttpEndpoint = trimTrailingSlash(
|
|
95
|
+
options.daprHttpEndpoint ?? DEFAULT_DAPR_HTTP_ENDPOINT
|
|
96
|
+
);
|
|
97
|
+
this.stateStoreName = options.stateStoreName ? ensureNonEmpty(options.stateStoreName, "stateStoreName") : void 0;
|
|
98
|
+
this.apiToken = options.apiToken?.trim() || void 0;
|
|
99
|
+
this.requestTimeoutMs = ensurePositiveInt(
|
|
100
|
+
options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
101
|
+
"requestTimeoutMs"
|
|
102
|
+
);
|
|
103
|
+
this.maxRequestRetries = ensureNonNegativeInt(
|
|
104
|
+
options.maxRequestRetries ?? DEFAULT_MAX_REQUEST_RETRIES,
|
|
105
|
+
"maxRequestRetries"
|
|
106
|
+
);
|
|
107
|
+
this.verifySidecarOnStart = options.verifySidecarOnStart ?? DEFAULT_VERIFY_SIDECAR_ON_START;
|
|
108
|
+
if (options.fetch) {
|
|
109
|
+
this.fetchImpl = options.fetch;
|
|
110
|
+
} else if (typeof globalThis.fetch === "function") {
|
|
111
|
+
this.fetchImpl = globalThis.fetch.bind(globalThis);
|
|
112
|
+
} else {
|
|
113
|
+
throw new Error(
|
|
114
|
+
"No fetch implementation available. Provide options.fetch."
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async verifySidecar() {
|
|
119
|
+
if (!this.verifySidecarOnStart) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
await this.request("/v1.0/metadata", { method: "GET" }, [200]);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Failed to verify Dapr sidecar on start at ${this.daprHttpEndpoint}: ${detail}`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async getStateEntry(key) {
|
|
132
|
+
const stateStoreName = this.requireStateStoreName();
|
|
133
|
+
const response = await this.request(
|
|
134
|
+
`/v1.0/state/${encodeURIComponent(stateStoreName)}/${encodeURIComponent(key)}`,
|
|
135
|
+
{ method: "GET" },
|
|
136
|
+
[200, 204, 404]
|
|
137
|
+
);
|
|
138
|
+
if (response.status === 404 || response.status === 204) {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
const text = await response.text();
|
|
142
|
+
if (!text.trim()) {
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
value: parseJson(text),
|
|
147
|
+
etag: response.headers.get("etag")?.trim() || response.headers.get("ETag")?.trim() || void 0
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
async getState(key) {
|
|
151
|
+
return (await this.getStateEntry(key)).value;
|
|
152
|
+
}
|
|
153
|
+
async saveState(key, value, options = {}) {
|
|
154
|
+
const stateStoreName = this.requireStateStoreName();
|
|
155
|
+
const payload = [
|
|
156
|
+
{
|
|
157
|
+
key,
|
|
158
|
+
value,
|
|
159
|
+
...options.etag ? { etag: options.etag } : {},
|
|
160
|
+
...options.concurrency ? { options: { concurrency: options.concurrency } } : {}
|
|
161
|
+
}
|
|
162
|
+
];
|
|
163
|
+
await this.request(
|
|
164
|
+
`/v1.0/state/${encodeURIComponent(stateStoreName)}`,
|
|
165
|
+
{
|
|
166
|
+
method: "POST",
|
|
167
|
+
body: JSON.stringify(payload)
|
|
168
|
+
},
|
|
169
|
+
[200, 201, 204],
|
|
170
|
+
true
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
async deleteState(key) {
|
|
174
|
+
const stateStoreName = this.requireStateStoreName();
|
|
175
|
+
await this.request(
|
|
176
|
+
`/v1.0/state/${encodeURIComponent(stateStoreName)}/${encodeURIComponent(key)}`,
|
|
177
|
+
{ method: "DELETE" },
|
|
178
|
+
[200, 204, 404]
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
async queryState(query) {
|
|
182
|
+
const stateStoreName = this.requireStateStoreName();
|
|
183
|
+
return await this.requestJson(
|
|
184
|
+
`/v1.0-alpha1/state/${encodeURIComponent(stateStoreName)}/query`,
|
|
185
|
+
{
|
|
186
|
+
method: "POST",
|
|
187
|
+
body: JSON.stringify(query)
|
|
188
|
+
},
|
|
189
|
+
[200, 204]
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
async requestJson(path, init, allowedStatusCodes) {
|
|
193
|
+
const response = await this.request(path, init, allowedStatusCodes, true);
|
|
194
|
+
const text = await response.text();
|
|
195
|
+
if (!text.trim()) {
|
|
196
|
+
return void 0;
|
|
197
|
+
}
|
|
198
|
+
return parseJson(text);
|
|
199
|
+
}
|
|
200
|
+
async request(path, init, allowedStatusCodes, withJsonContentType = false) {
|
|
201
|
+
const headers = new Headers(init.headers);
|
|
202
|
+
if (withJsonContentType && !headers.has("content-type")) {
|
|
203
|
+
headers.set("content-type", "application/json");
|
|
204
|
+
}
|
|
205
|
+
if (this.apiToken && !headers.has("dapr-api-token")) {
|
|
206
|
+
headers.set("dapr-api-token", this.apiToken);
|
|
207
|
+
}
|
|
208
|
+
const otelMod = await tryGetOtelPropagation();
|
|
209
|
+
if (otelMod) {
|
|
210
|
+
otelMod.propagation.inject(otelMod.context.active(), headers, {
|
|
211
|
+
set(carrier, key, value) {
|
|
212
|
+
carrier.set(key, value);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const method = (init.method ?? "GET").toUpperCase();
|
|
217
|
+
for (let attempt = 0; attempt <= this.maxRequestRetries; attempt += 1) {
|
|
218
|
+
const timeoutController = new AbortController();
|
|
219
|
+
const timeout = setTimeout(() => {
|
|
220
|
+
timeoutController.abort();
|
|
221
|
+
}, this.requestTimeoutMs);
|
|
222
|
+
timeout.unref?.();
|
|
223
|
+
try {
|
|
224
|
+
const response = await this.fetchImpl(
|
|
225
|
+
`${this.daprHttpEndpoint}${path}`,
|
|
226
|
+
{
|
|
227
|
+
...init,
|
|
228
|
+
method,
|
|
229
|
+
headers,
|
|
230
|
+
signal: timeoutController.signal
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
if (allowedStatusCodes.includes(response.status)) {
|
|
234
|
+
return response;
|
|
235
|
+
}
|
|
236
|
+
const body = await response.text().catch(() => "");
|
|
237
|
+
const retryable = isRetryableStatusCode(response.status);
|
|
238
|
+
if (retryable && attempt < this.maxRequestRetries) {
|
|
239
|
+
await sleep(100 * 2 ** attempt);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
throw new DaprHttpError(
|
|
243
|
+
response.status,
|
|
244
|
+
`Dapr request failed (${response.status}) ${method} ${path}${body ? `: ${body}` : ""}`
|
|
245
|
+
);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (isAbortError(error)) {
|
|
248
|
+
if (attempt < this.maxRequestRetries) {
|
|
249
|
+
await sleep(100 * 2 ** attempt);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
throw new Error(
|
|
253
|
+
`Dapr request timed out after ${this.requestTimeoutMs}ms: ${method} ${path}`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
if (isRetryableError(error) && attempt < this.maxRequestRetries) {
|
|
257
|
+
await sleep(100 * 2 ** attempt);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (error instanceof Error) {
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
throw new Error(String(error));
|
|
264
|
+
} finally {
|
|
265
|
+
clearTimeout(timeout);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
throw new Error(`Dapr request failed after retries: ${method} ${path}`);
|
|
269
|
+
}
|
|
270
|
+
requireStateStoreName() {
|
|
271
|
+
if (!this.stateStoreName) {
|
|
272
|
+
throw new Error(
|
|
273
|
+
"This DaprSidecarClient instance was created without a stateStoreName."
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
return this.stateStoreName;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
export {
|
|
281
|
+
isDaprConflictError,
|
|
282
|
+
DaprSidecarClient
|
|
283
|
+
};
|