@cuylabs/agent-foundry-agentserver-responses 4.9.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 +115 -0
- package/dist/chunk-24VMS6GY.js +513 -0
- package/dist/chunk-DGMWFFNQ.js +473 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +1163 -0
- package/dist/store/index.d.ts +134 -0
- package/dist/store/index.js +14 -0
- package/dist/streaming/index.d.ts +27 -0
- package/dist/streaming/index.js +14 -0
- package/dist/types-Dvs2F85g.d.ts +254 -0
- package/docs/README.md +72 -0
- package/docs/agent-core-bridge.md +48 -0
- package/docs/hosting.md +72 -0
- package/docs/protocol.md +60 -0
- package/docs/python-parity.md +106 -0
- package/docs/store.md +88 -0
- package/package.json +74 -0
- package/samples/README.md +30 -0
- package/samples/sample_01_getting_started.ts +16 -0
- package/samples/sample_02_streaming_text.ts +23 -0
- package/samples/sample_03_raw_events.ts +58 -0
- package/samples/sample_04_foundry_storage.ts +26 -0
- package/samples/sample_05_agent_core_bridge.ts +15 -0
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// src/store/foundry-errors.ts
|
|
2
|
+
var FoundryStorageError = class extends Error {
|
|
3
|
+
constructor(message, status, code = "foundry_storage_error", responseBody) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.responseBody = responseBody;
|
|
8
|
+
this.name = "FoundryStorageError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var FoundryStorageNotFoundError = class extends FoundryStorageError {
|
|
12
|
+
constructor(message, responseBody) {
|
|
13
|
+
super(message, 404, "not_found", responseBody);
|
|
14
|
+
this.name = "FoundryStorageNotFoundError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
async function throwIfStorageError(response) {
|
|
18
|
+
if (response.ok) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const body = await parseErrorBody(response);
|
|
22
|
+
const message = messageFromBody(body) ?? `Foundry storage request failed with HTTP ${response.status}`;
|
|
23
|
+
if (response.status === 404) {
|
|
24
|
+
throw new FoundryStorageNotFoundError(message, body);
|
|
25
|
+
}
|
|
26
|
+
throw new FoundryStorageError(
|
|
27
|
+
message,
|
|
28
|
+
response.status,
|
|
29
|
+
codeFromBody(body) ?? "foundry_storage_error",
|
|
30
|
+
body
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
async function parseErrorBody(response) {
|
|
34
|
+
const text = await response.text().catch(() => "");
|
|
35
|
+
if (!text) {
|
|
36
|
+
return void 0;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(text);
|
|
40
|
+
} catch {
|
|
41
|
+
return text;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function messageFromBody(body) {
|
|
45
|
+
if (isRecord(body)) {
|
|
46
|
+
const error = body.error;
|
|
47
|
+
if (isRecord(error) && typeof error.message === "string") {
|
|
48
|
+
return error.message;
|
|
49
|
+
}
|
|
50
|
+
if (typeof body.message === "string") {
|
|
51
|
+
return body.message;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return typeof body === "string" ? body : void 0;
|
|
55
|
+
}
|
|
56
|
+
function codeFromBody(body) {
|
|
57
|
+
if (isRecord(body)) {
|
|
58
|
+
const error = body.error;
|
|
59
|
+
if (isRecord(error) && typeof error.code === "string") {
|
|
60
|
+
return error.code;
|
|
61
|
+
}
|
|
62
|
+
if (typeof body.code === "string") {
|
|
63
|
+
return body.code;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
function isRecord(value) {
|
|
69
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/store/foundry-provider.ts
|
|
73
|
+
import {
|
|
74
|
+
CHAT_ISOLATION_HEADER,
|
|
75
|
+
USER_ISOLATION_HEADER
|
|
76
|
+
} from "@cuylabs/agent-foundry-agentserver-core";
|
|
77
|
+
|
|
78
|
+
// src/package-version.ts
|
|
79
|
+
var AGENTSERVER_RESPONSES_PACKAGE_VERSION = "4.8.1";
|
|
80
|
+
|
|
81
|
+
// src/store/foundry-settings.ts
|
|
82
|
+
var STORAGE_API_VERSION = "v1";
|
|
83
|
+
var FoundryStorageSettings = class _FoundryStorageSettings {
|
|
84
|
+
storageBaseUrl;
|
|
85
|
+
constructor(options) {
|
|
86
|
+
this.storageBaseUrl = normalizeBaseUrl(options.storageBaseUrl);
|
|
87
|
+
}
|
|
88
|
+
static fromEnv(env = process.env) {
|
|
89
|
+
const endpoint = env.FOUNDRY_PROJECT_ENDPOINT?.trim();
|
|
90
|
+
if (!endpoint) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"FOUNDRY_PROJECT_ENDPOINT is required for Foundry response storage."
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return _FoundryStorageSettings.fromEndpoint(endpoint);
|
|
96
|
+
}
|
|
97
|
+
static fromEndpoint(endpoint) {
|
|
98
|
+
const normalized = endpoint.trim();
|
|
99
|
+
if (!normalized) {
|
|
100
|
+
throw new Error("endpoint must be a non-empty string");
|
|
101
|
+
}
|
|
102
|
+
const parsed = new URL(normalized);
|
|
103
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
104
|
+
throw new Error("endpoint must be an absolute http or https URL");
|
|
105
|
+
}
|
|
106
|
+
return new _FoundryStorageSettings({
|
|
107
|
+
storageBaseUrl: `${parsed.origin}${parsed.pathname.replace(/\/?$/, "/")}storage/`
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
static resolve(options = {}) {
|
|
111
|
+
if (options.storageBaseUrl) {
|
|
112
|
+
return new _FoundryStorageSettings({
|
|
113
|
+
storageBaseUrl: options.storageBaseUrl
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (options.projectEndpoint) {
|
|
117
|
+
return _FoundryStorageSettings.fromEndpoint(options.projectEndpoint);
|
|
118
|
+
}
|
|
119
|
+
return _FoundryStorageSettings.fromEnv(options.env);
|
|
120
|
+
}
|
|
121
|
+
buildUrl(path, query = {}) {
|
|
122
|
+
const normalizedPath = path.replace(/^\/+/, "");
|
|
123
|
+
const url = new URL(normalizedPath, this.storageBaseUrl);
|
|
124
|
+
url.searchParams.set("api-version", STORAGE_API_VERSION);
|
|
125
|
+
for (const [key, value] of Object.entries(query)) {
|
|
126
|
+
if (value !== void 0) {
|
|
127
|
+
url.searchParams.set(key, value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return url.toString();
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
function normalizeBaseUrl(value) {
|
|
134
|
+
const normalized = value.trim();
|
|
135
|
+
if (!normalized) {
|
|
136
|
+
throw new Error("storageBaseUrl must be a non-empty string");
|
|
137
|
+
}
|
|
138
|
+
const parsed = new URL(normalized);
|
|
139
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
140
|
+
throw new Error("storageBaseUrl must be an absolute http or https URL");
|
|
141
|
+
}
|
|
142
|
+
return parsed.toString().replace(/\/?$/, "/");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/store/foundry-provider.ts
|
|
146
|
+
var FOUNDRY_TOKEN_SCOPE = "https://ai.azure.com/.default";
|
|
147
|
+
var JSON_CONTENT_TYPE = "application/json; charset=utf-8";
|
|
148
|
+
var FoundryStorageProvider = class {
|
|
149
|
+
credential;
|
|
150
|
+
fetchImpl;
|
|
151
|
+
settings;
|
|
152
|
+
getServerVersion;
|
|
153
|
+
constructor(options) {
|
|
154
|
+
this.credential = options.credential;
|
|
155
|
+
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
156
|
+
this.settings = FoundryStorageSettings.resolve(options);
|
|
157
|
+
this.getServerVersion = options.getServerVersion;
|
|
158
|
+
if (!this.fetchImpl) {
|
|
159
|
+
throw new Error("global fetch is unavailable; provide options.fetch");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async createResponse(response, inputItems, options = {}) {
|
|
163
|
+
await this.request("responses", {
|
|
164
|
+
body: {
|
|
165
|
+
response,
|
|
166
|
+
input_items: inputItems,
|
|
167
|
+
history_item_ids: [...options.historyItemIds ?? []]
|
|
168
|
+
},
|
|
169
|
+
isolation: options.isolation,
|
|
170
|
+
method: "POST"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
async getResponse(responseId, options = {}) {
|
|
174
|
+
try {
|
|
175
|
+
return await this.request(
|
|
176
|
+
`responses/${encodeURIComponent(responseId)}`,
|
|
177
|
+
{
|
|
178
|
+
isolation: options.isolation,
|
|
179
|
+
method: "GET"
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error instanceof FoundryStorageNotFoundError) {
|
|
184
|
+
return void 0;
|
|
185
|
+
}
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async updateResponse(response, options = {}) {
|
|
190
|
+
await this.request(`responses/${encodeURIComponent(response.id)}`, {
|
|
191
|
+
body: response,
|
|
192
|
+
isolation: options.isolation,
|
|
193
|
+
method: "POST"
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
async deleteResponse(responseId, options = {}) {
|
|
197
|
+
try {
|
|
198
|
+
await this.request(`responses/${encodeURIComponent(responseId)}`, {
|
|
199
|
+
isolation: options.isolation,
|
|
200
|
+
method: "DELETE"
|
|
201
|
+
});
|
|
202
|
+
return true;
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (error instanceof FoundryStorageNotFoundError) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async getInputItems(responseId, options = {}) {
|
|
211
|
+
try {
|
|
212
|
+
const result = await this.request(
|
|
213
|
+
`responses/${encodeURIComponent(responseId)}/input_items`,
|
|
214
|
+
{
|
|
215
|
+
isolation: options.isolation,
|
|
216
|
+
method: "GET",
|
|
217
|
+
query: {
|
|
218
|
+
after: options.after,
|
|
219
|
+
before: options.before,
|
|
220
|
+
limit: options.limit !== void 0 ? String(options.limit) : void 0,
|
|
221
|
+
order: options.ascending === true ? "asc" : "desc"
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
return Array.isArray(result.data) ? result.data : [];
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error instanceof FoundryStorageNotFoundError) {
|
|
228
|
+
return void 0;
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async getItems(itemIds, options = {}) {
|
|
234
|
+
return await this.request(
|
|
235
|
+
"items/batch/retrieve",
|
|
236
|
+
{
|
|
237
|
+
body: { item_ids: [...itemIds] },
|
|
238
|
+
isolation: options.isolation,
|
|
239
|
+
method: "POST"
|
|
240
|
+
}
|
|
241
|
+
).then((items) => items.map((item) => item ?? void 0));
|
|
242
|
+
}
|
|
243
|
+
async getHistoryItemIds(previousResponseId, conversationId, limit, options = {}) {
|
|
244
|
+
return await this.request("history/item_ids", {
|
|
245
|
+
isolation: options.isolation,
|
|
246
|
+
method: "GET",
|
|
247
|
+
query: {
|
|
248
|
+
conversation_id: conversationId,
|
|
249
|
+
limit: String(limit),
|
|
250
|
+
previous_response_id: previousResponseId
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async request(path, options) {
|
|
255
|
+
const token = await this.credential.getToken(FOUNDRY_TOKEN_SCOPE);
|
|
256
|
+
if (!token?.token) {
|
|
257
|
+
throw new Error("credential did not return an access token");
|
|
258
|
+
}
|
|
259
|
+
const response = await this.fetchImpl(
|
|
260
|
+
this.settings.buildUrl(path, options.query),
|
|
261
|
+
{
|
|
262
|
+
body: options.body === void 0 ? void 0 : JSON.stringify(options.body),
|
|
263
|
+
headers: this.headers(token.token, options),
|
|
264
|
+
method: options.method
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
await throwIfStorageError(response);
|
|
268
|
+
if (response.status === 204) {
|
|
269
|
+
return void 0;
|
|
270
|
+
}
|
|
271
|
+
const text = await response.text();
|
|
272
|
+
return text ? JSON.parse(text) : void 0;
|
|
273
|
+
}
|
|
274
|
+
headers(token, options) {
|
|
275
|
+
const headers = {
|
|
276
|
+
authorization: `Bearer ${token}`,
|
|
277
|
+
"user-agent": this.getServerVersion?.() ?? `ai-agentserver-responses/${AGENTSERVER_RESPONSES_PACKAGE_VERSION}`
|
|
278
|
+
};
|
|
279
|
+
if (options.body !== void 0) {
|
|
280
|
+
headers["content-type"] = JSON_CONTENT_TYPE;
|
|
281
|
+
}
|
|
282
|
+
if (options.isolation?.userKey !== void 0) {
|
|
283
|
+
headers[USER_ISOLATION_HEADER] = options.isolation.userKey;
|
|
284
|
+
}
|
|
285
|
+
if (options.isolation?.chatKey !== void 0) {
|
|
286
|
+
headers[CHAT_ISOLATION_HEADER] = options.isolation.chatKey;
|
|
287
|
+
}
|
|
288
|
+
return headers;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// src/store/memory.ts
|
|
293
|
+
var InMemoryResponseProvider = class {
|
|
294
|
+
responses = /* @__PURE__ */ new Map();
|
|
295
|
+
inputItems = /* @__PURE__ */ new Map();
|
|
296
|
+
historyItemIds = /* @__PURE__ */ new Map();
|
|
297
|
+
items = /* @__PURE__ */ new Map();
|
|
298
|
+
streamEvents = /* @__PURE__ */ new Map();
|
|
299
|
+
conversationResponses = /* @__PURE__ */ new Map();
|
|
300
|
+
deleted = /* @__PURE__ */ new Set();
|
|
301
|
+
async createResponse(response, inputItems, options = {}) {
|
|
302
|
+
const responseKey = scopedKey(response.id, options.isolation);
|
|
303
|
+
this.responses.set(responseKey, clone(response));
|
|
304
|
+
this.inputItems.set(responseKey, clone(inputItems));
|
|
305
|
+
this.historyItemIds.set(responseKey, [...options.historyItemIds ?? []]);
|
|
306
|
+
for (const item of [...inputItems, ...response.output]) {
|
|
307
|
+
this.items.set(scopedKey(item.id, options.isolation), clone(item));
|
|
308
|
+
}
|
|
309
|
+
const conversationId = response.conversation?.id;
|
|
310
|
+
if (conversationId) {
|
|
311
|
+
const conversationKey = scopedKey(conversationId, options.isolation);
|
|
312
|
+
const responseIds = this.conversationResponses.get(conversationKey) ?? [];
|
|
313
|
+
if (!responseIds.includes(response.id)) {
|
|
314
|
+
responseIds.push(response.id);
|
|
315
|
+
}
|
|
316
|
+
this.conversationResponses.set(conversationKey, responseIds);
|
|
317
|
+
}
|
|
318
|
+
this.deleted.delete(responseKey);
|
|
319
|
+
}
|
|
320
|
+
async getResponse(responseId, options = {}) {
|
|
321
|
+
return clone(this.responses.get(scopedKey(responseId, options.isolation)));
|
|
322
|
+
}
|
|
323
|
+
async updateResponse(response, options = {}) {
|
|
324
|
+
const responseKey = scopedKey(response.id, options.isolation);
|
|
325
|
+
this.responses.set(responseKey, clone(response));
|
|
326
|
+
for (const item of response.output) {
|
|
327
|
+
this.items.set(scopedKey(item.id, options.isolation), clone(item));
|
|
328
|
+
}
|
|
329
|
+
const conversationId = response.conversation?.id;
|
|
330
|
+
if (conversationId) {
|
|
331
|
+
const conversationKey = scopedKey(conversationId, options.isolation);
|
|
332
|
+
const responseIds = this.conversationResponses.get(conversationKey) ?? [];
|
|
333
|
+
if (!responseIds.includes(response.id)) {
|
|
334
|
+
responseIds.push(response.id);
|
|
335
|
+
}
|
|
336
|
+
this.conversationResponses.set(conversationKey, responseIds);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async deleteResponse(responseId, options = {}) {
|
|
340
|
+
const responseKey = scopedKey(responseId, options.isolation);
|
|
341
|
+
const existed = this.responses.delete(responseKey);
|
|
342
|
+
this.inputItems.delete(responseKey);
|
|
343
|
+
this.historyItemIds.delete(responseKey);
|
|
344
|
+
this.streamEvents.delete(responseKey);
|
|
345
|
+
if (existed) {
|
|
346
|
+
this.deleted.add(responseKey);
|
|
347
|
+
}
|
|
348
|
+
return existed;
|
|
349
|
+
}
|
|
350
|
+
async getItems(itemIds, options = {}) {
|
|
351
|
+
return itemIds.map(
|
|
352
|
+
(itemId) => clone(this.items.get(scopedKey(itemId, options.isolation)))
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
async getInputItems(responseId, options = {}) {
|
|
356
|
+
const responseKey = scopedKey(responseId, options.isolation);
|
|
357
|
+
if (!this.responses.has(responseKey)) {
|
|
358
|
+
return void 0;
|
|
359
|
+
}
|
|
360
|
+
const itemIds = [
|
|
361
|
+
...this.historyItemIds.get(responseKey) ?? [],
|
|
362
|
+
...(this.inputItems.get(responseKey) ?? []).map((item) => item.id)
|
|
363
|
+
];
|
|
364
|
+
const orderedIds = options.ascending === true ? itemIds : [...itemIds].reverse();
|
|
365
|
+
const afterIndex = options.after !== void 0 ? orderedIds.indexOf(options.after) : -1;
|
|
366
|
+
const afterFiltered = afterIndex >= 0 ? orderedIds.slice(afterIndex + 1) : orderedIds;
|
|
367
|
+
const beforeIndex = options.before !== void 0 ? afterFiltered.indexOf(options.before) : -1;
|
|
368
|
+
const cursorFiltered = beforeIndex >= 0 ? afterFiltered.slice(0, beforeIndex) : afterFiltered;
|
|
369
|
+
const limit = clampLimit(options.limit);
|
|
370
|
+
return cursorFiltered.slice(0, limit).map((itemId) => this.items.get(scopedKey(itemId, options.isolation))).filter((item) => item !== void 0).map((item) => clone(item));
|
|
371
|
+
}
|
|
372
|
+
async getHistoryItemIds(previousResponseId, conversationId, limit, options = {}) {
|
|
373
|
+
const resolved = [];
|
|
374
|
+
if (previousResponseId) {
|
|
375
|
+
this.collectPreviousResponseHistoryItemIds(
|
|
376
|
+
previousResponseId,
|
|
377
|
+
options.isolation,
|
|
378
|
+
resolved
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
if (conversationId) {
|
|
382
|
+
const conversationKey = scopedKey(conversationId, options.isolation);
|
|
383
|
+
const responseIds = this.conversationResponses.get(conversationKey) ?? [];
|
|
384
|
+
for (const responseId of responseIds) {
|
|
385
|
+
this.collectResponseItemIds(responseId, options.isolation, resolved);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return resolved.slice(0, Math.max(0, limit));
|
|
389
|
+
}
|
|
390
|
+
async getHistoryItems(previousResponseId, conversationId, limit, options = {}) {
|
|
391
|
+
const itemIds = await this.getHistoryItemIds(
|
|
392
|
+
previousResponseId,
|
|
393
|
+
conversationId,
|
|
394
|
+
limit,
|
|
395
|
+
options
|
|
396
|
+
);
|
|
397
|
+
const items = await this.getItems(itemIds, options);
|
|
398
|
+
return items.filter((item) => item !== void 0);
|
|
399
|
+
}
|
|
400
|
+
async appendStreamEvent(responseId, event, options = {}) {
|
|
401
|
+
const responseKey = scopedKey(responseId, options.isolation);
|
|
402
|
+
const events = this.streamEvents.get(responseKey) ?? [];
|
|
403
|
+
events.push(clone(event));
|
|
404
|
+
this.streamEvents.set(responseKey, events);
|
|
405
|
+
}
|
|
406
|
+
async getStreamEvents(responseId, options = {}) {
|
|
407
|
+
return clone(
|
|
408
|
+
this.streamEvents.get(scopedKey(responseId, options.isolation))
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
async saveStreamEvents(responseId, events, options = {}) {
|
|
412
|
+
this.streamEvents.set(
|
|
413
|
+
scopedKey(responseId, options.isolation),
|
|
414
|
+
clone(events)
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
async deleteStreamEvents(responseId, options = {}) {
|
|
418
|
+
this.streamEvents.delete(scopedKey(responseId, options.isolation));
|
|
419
|
+
}
|
|
420
|
+
isDeleted(responseId, isolation) {
|
|
421
|
+
return this.deleted.has(scopedKey(responseId, isolation));
|
|
422
|
+
}
|
|
423
|
+
collectPreviousResponseHistoryItemIds(responseId, isolation, output) {
|
|
424
|
+
const response = this.responses.get(scopedKey(responseId, isolation));
|
|
425
|
+
if (!response) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (response.previous_response_id) {
|
|
429
|
+
this.collectPreviousResponseHistoryItemIds(
|
|
430
|
+
response.previous_response_id,
|
|
431
|
+
isolation,
|
|
432
|
+
output
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
this.collectResponseItemIds(responseId, isolation, output);
|
|
436
|
+
}
|
|
437
|
+
collectResponseItemIds(responseId, isolation, output) {
|
|
438
|
+
const responseKey = scopedKey(responseId, isolation);
|
|
439
|
+
const response = this.responses.get(responseKey);
|
|
440
|
+
if (!response) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
output.push(...this.historyItemIds.get(responseKey) ?? []);
|
|
444
|
+
output.push(
|
|
445
|
+
...(this.inputItems.get(responseKey) ?? []).map((item) => item.id)
|
|
446
|
+
);
|
|
447
|
+
output.push(...response.output.map((item) => item.id));
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
function scopedKey(id, isolation) {
|
|
451
|
+
return `${isolation?.chatKey ?? ""}|${isolation?.userKey ?? ""}|${id}`;
|
|
452
|
+
}
|
|
453
|
+
function clone(value) {
|
|
454
|
+
if (value === void 0) {
|
|
455
|
+
return value;
|
|
456
|
+
}
|
|
457
|
+
return JSON.parse(JSON.stringify(value));
|
|
458
|
+
}
|
|
459
|
+
function clampLimit(value) {
|
|
460
|
+
if (!Number.isInteger(value) || value === void 0) {
|
|
461
|
+
return 20;
|
|
462
|
+
}
|
|
463
|
+
return Math.max(1, Math.min(100, value));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export {
|
|
467
|
+
AGENTSERVER_RESPONSES_PACKAGE_VERSION,
|
|
468
|
+
InMemoryResponseProvider,
|
|
469
|
+
FoundryStorageError,
|
|
470
|
+
FoundryStorageNotFoundError,
|
|
471
|
+
FoundryStorageSettings,
|
|
472
|
+
FoundryStorageProvider
|
|
473
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Express } from 'express';
|
|
2
|
+
import { R as ResponsesServerOptions, a as ResponsesServer, C as CreateResponse, b as ResponseObject, O as OutputItem, A as AgentReference, c as ResponseInput, I as InputItem } from './types-Dvs2F85g.js';
|
|
3
|
+
export { d as ConversationReference, E as ExpressResponse, F as FunctionCallOutputItem, e as ItemMessage, f as ItemReference, J as JsonValue, M as MessageContent, g as MessageContentInputText, h as MessageContentOutputText, i as MessageContentRefusal, j as ResponseContext, k as ResponseError, l as ResponseHandler, m as ResponseHandlerContext, n as ResponseHandlerResult, o as ResponseModeFlags, p as ResponseProvider, q as ResponseStatus, r as ResponseStreamEvent, s as ResponseStreamProvider, t as ResponseUsage, u as ResponsesHandler, v as ResponsesHandlerFactory, w as ResponsesHandlerInput, x as ResponsesRequest, y as ResponsesServerLogger } from './types-Dvs2F85g.js';
|
|
4
|
+
export { AccessToken, FoundryStorageError, FoundryStorageNotFoundError, FoundryStorageProvider, FoundryStorageProviderOptions, FoundryStorageSettings, FoundryStorageSettingsOptions, InMemoryResponseProvider, TokenCredential } from './store/index.js';
|
|
5
|
+
export { SseEncoderState, TextResponse, TextSource, createSseEncoderState, encodeKeepAliveComment, encodeSseEvent, startResponsesSseResponse } from './streaming/index.js';
|
|
6
|
+
import 'node:http';
|
|
7
|
+
import '@cuylabs/agent-foundry-agentserver-core';
|
|
8
|
+
|
|
9
|
+
declare function createResponsesApp(options: ResponsesServerOptions): Express;
|
|
10
|
+
declare function runResponsesServer(options: ResponsesServerOptions): Promise<ResponsesServer>;
|
|
11
|
+
|
|
12
|
+
declare const AGENTSERVER_RESPONSES_PACKAGE_VERSION = "4.8.1";
|
|
13
|
+
|
|
14
|
+
declare function newId(prefix: string, partitionKeyHint?: string): string;
|
|
15
|
+
declare function newResponseId(partitionKeyHint?: string): string;
|
|
16
|
+
declare function newMessageItemId(partitionKeyHint?: string): string;
|
|
17
|
+
declare function newOutputMessageItemId(partitionKeyHint?: string): string;
|
|
18
|
+
declare function extractPartitionKey(idValue: string): string;
|
|
19
|
+
declare function isValidId(idValue: string | undefined, allowedPrefixes?: readonly string[]): {
|
|
20
|
+
valid: boolean;
|
|
21
|
+
error?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
declare const RESPONSE_ID_HEADER: "x-agent-response-id";
|
|
25
|
+
declare const SESSION_ID_HEADER: "x-agent-session-id";
|
|
26
|
+
declare function normalizeCreateResponse(value: unknown): CreateResponse;
|
|
27
|
+
declare function resolveConversationId(request: Pick<CreateResponse, "conversation">): string | undefined;
|
|
28
|
+
declare function resolveResponseId(request: CreateResponse, headers: {
|
|
29
|
+
get(name: string): string | undefined;
|
|
30
|
+
}): string;
|
|
31
|
+
declare function validateResponseId(responseId: string): void;
|
|
32
|
+
declare function resolveAgentReference(value: unknown): AgentReference | undefined;
|
|
33
|
+
declare function resolveSessionId(options: {
|
|
34
|
+
request: CreateResponse;
|
|
35
|
+
envSessionId?: string;
|
|
36
|
+
agentReference?: AgentReference;
|
|
37
|
+
}): string;
|
|
38
|
+
declare function deriveSessionId(options: {
|
|
39
|
+
conversationId?: string;
|
|
40
|
+
previousResponseId?: string;
|
|
41
|
+
agentReference?: AgentReference;
|
|
42
|
+
}): string;
|
|
43
|
+
declare function getInputExpanded(request: Pick<CreateResponse, "input">, responseId: string): OutputItem[];
|
|
44
|
+
declare function normalizeResponseInput(input: ResponseInput | undefined): InputItem[];
|
|
45
|
+
declare function toOutputItem(item: InputItem, partitionHint: string): OutputItem;
|
|
46
|
+
declare function extractTextFromItems(items: readonly OutputItem[]): string;
|
|
47
|
+
declare function createResponseObject(options: {
|
|
48
|
+
id: string;
|
|
49
|
+
request: CreateResponse;
|
|
50
|
+
status: ResponseObject["status"];
|
|
51
|
+
output?: OutputItem[];
|
|
52
|
+
error?: ResponseObject["error"];
|
|
53
|
+
createdAt?: number;
|
|
54
|
+
}): ResponseObject;
|
|
55
|
+
|
|
56
|
+
declare function buildResponsesOpenApiSpec(options?: {
|
|
57
|
+
appName?: string;
|
|
58
|
+
}): Record<string, unknown>;
|
|
59
|
+
|
|
60
|
+
export { AGENTSERVER_RESPONSES_PACKAGE_VERSION, AgentReference, CreateResponse, InputItem, OutputItem, RESPONSE_ID_HEADER, ResponseInput, ResponseObject, ResponsesServer, ResponsesServerOptions, SESSION_ID_HEADER, buildResponsesOpenApiSpec, createResponseObject, createResponsesApp, deriveSessionId, extractPartitionKey, extractTextFromItems, getInputExpanded, isValidId, newId, newMessageItemId, newOutputMessageItemId, newResponseId, normalizeCreateResponse, normalizeResponseInput, resolveAgentReference, resolveConversationId, resolveResponseId, resolveSessionId, runResponsesServer, toOutputItem, validateResponseId };
|