sarvam-agents-client 0.5.1 → 0.5.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/dist/compat/core/data.js +5 -14
- package/dist/compat/core/events.d.ts +6 -0
- package/dist/compat/core/files.d.ts +65 -0
- package/dist/compat/core/files.js +851 -0
- package/dist/compat/core/history.js +32 -7
- package/dist/compat/core/http.d.ts +52 -0
- package/dist/compat/core/http.js +309 -0
- package/dist/compat/core/turns.d.ts +29 -0
- package/dist/compat/core/turns.js +163 -12
- package/dist/compat/openai/client.d.ts +5 -0
- package/dist/compat/openai/client.js +10 -2
- package/dist/compat/openai/resources/events.js +218 -21
- package/dist/compat/openai/resources/sessions.js +33 -4
- package/dist/esm/compat/core/data.js +5 -14
- package/dist/esm/compat/core/events.d.ts +6 -0
- package/dist/esm/compat/core/files.d.ts +65 -0
- package/dist/esm/compat/core/files.js +829 -0
- package/dist/esm/compat/core/history.js +32 -7
- package/dist/esm/compat/core/http.d.ts +52 -0
- package/dist/esm/compat/core/http.js +307 -0
- package/dist/esm/compat/core/turns.d.ts +29 -0
- package/dist/esm/compat/core/turns.js +162 -12
- package/dist/esm/compat/openai/client.d.ts +5 -0
- package/dist/esm/compat/openai/client.js +10 -2
- package/dist/esm/compat/openai/resources/events.js +209 -12
- package/dist/esm/compat/openai/resources/sessions.js +33 -4
- package/package.json +1 -1
- package/src/compat/core/data.ts +5 -14
- package/src/compat/core/events.ts +7 -1
- package/src/compat/core/files.ts +913 -0
- package/src/compat/core/history.ts +32 -7
- package/src/compat/core/http.ts +338 -1
- package/src/compat/core/turns.ts +214 -13
- package/src/compat/openai/client.ts +13 -1
- package/src/compat/openai/resources/events.ts +239 -12
- package/src/compat/openai/resources/sessions.ts +39 -4
|
@@ -3,21 +3,179 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ItemsResource = exports.EventsResource = void 0;
|
|
4
4
|
exports.parseInputParts = parseInputParts;
|
|
5
5
|
/** OpenAI beta.agents.sessions.events and beta.agents.sessions.items resources. */
|
|
6
|
+
const errors_js_1 = require("../../core/errors.js");
|
|
7
|
+
const files_js_1 = require("../../core/files.js");
|
|
6
8
|
const history_js_1 = require("../../core/history.js");
|
|
7
9
|
const paging_js_1 = require("../../core/paging.js");
|
|
8
|
-
const
|
|
10
|
+
const errors_js_2 = require("../errors.js");
|
|
9
11
|
const pages_js_1 = require("../pages.js");
|
|
10
12
|
const stream_js_1 = require("../stream.js");
|
|
11
13
|
const types_js_1 = require("../types.js");
|
|
12
14
|
function isObj(v) {
|
|
13
15
|
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
14
16
|
}
|
|
17
|
+
function parseInputImage(block, path) {
|
|
18
|
+
var _a;
|
|
19
|
+
if (block["detail"] !== undefined && block["detail"] !== null) {
|
|
20
|
+
(0, types_js_1.refuseOpenAI)("input.input_image.detail", { param: `${path}.detail` });
|
|
21
|
+
}
|
|
22
|
+
let rawUrl = block["image_url"];
|
|
23
|
+
if (isObj(rawUrl)) {
|
|
24
|
+
if (rawUrl["detail"] !== undefined && rawUrl["detail"] !== null) {
|
|
25
|
+
(0, types_js_1.refuseOpenAI)("input.input_image.detail", { param: `${path}.image_url.detail` });
|
|
26
|
+
}
|
|
27
|
+
rawUrl = rawUrl["url"];
|
|
28
|
+
}
|
|
29
|
+
const rawFileId = block["file_id"];
|
|
30
|
+
const hasUrl = rawUrl !== undefined && rawUrl !== null && rawUrl !== "";
|
|
31
|
+
const hasFileId = rawFileId !== undefined && rawFileId !== null && rawFileId !== "";
|
|
32
|
+
if (hasUrl === hasFileId) {
|
|
33
|
+
throw new errors_js_2.BadRequestError(`${path} must specify exactly one of 'image_url' or 'file_id'.`, {
|
|
34
|
+
status: 400,
|
|
35
|
+
param: path,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const rawFilename = block["filename"];
|
|
39
|
+
if (rawFilename !== undefined && rawFilename !== null && typeof rawFilename !== "string") {
|
|
40
|
+
throw new errors_js_2.BadRequestError(`${path}.filename must be a string.`, { status: 400, param: `${path}.filename` });
|
|
41
|
+
}
|
|
42
|
+
const filename = typeof rawFilename === "string" ? rawFilename : undefined;
|
|
43
|
+
if (hasFileId) {
|
|
44
|
+
const fidParam = `${path}.file_id`;
|
|
45
|
+
if (typeof rawFileId !== "string" || !rawFileId.trim()) {
|
|
46
|
+
throw new errors_js_2.BadRequestError(`${path}.file_id must be a non-empty string.`, { status: 400, param: fidParam });
|
|
47
|
+
}
|
|
48
|
+
const fid = rawFileId.trim();
|
|
49
|
+
if ((0, files_js_1.isOpenAIFileId)(fid)) {
|
|
50
|
+
throw new errors_js_2.BadRequestError(`OpenAI file_id ${JSON.stringify(fid)} is not supported; pass a Sarvam artifact_id belonging to this session, ` +
|
|
51
|
+
"a base64 data: URI, or an http(s) image_url.", { status: 400, param: fidParam });
|
|
52
|
+
}
|
|
53
|
+
return { type: "file_ref", fileId: fid, filename, kind: "image", param: fidParam };
|
|
54
|
+
}
|
|
55
|
+
const urlParam = `${path}.image_url`;
|
|
56
|
+
if (typeof rawUrl !== "string" || !rawUrl.trim()) {
|
|
57
|
+
throw new errors_js_2.BadRequestError(`${path}.image_url must be a non-empty string.`, { status: 400, param: urlParam });
|
|
58
|
+
}
|
|
59
|
+
const trimmed = rawUrl.trim();
|
|
60
|
+
if (trimmed.startsWith(files_js_1.ARTIFACT_URI_PREFIX)) {
|
|
61
|
+
const aid = trimmed.slice(files_js_1.ARTIFACT_URI_PREFIX.length).trim();
|
|
62
|
+
if (!aid || (0, files_js_1.isOpenAIFileId)(aid)) {
|
|
63
|
+
throw new errors_js_2.BadRequestError(`Invalid artifact URI ${JSON.stringify(trimmed)}; pass an artifact://<artifact_id> belonging to this session.`, { status: 400, param: urlParam });
|
|
64
|
+
}
|
|
65
|
+
return { type: "file_ref", fileId: aid, filename, kind: "image", param: urlParam };
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
if (trimmed.startsWith("data:")) {
|
|
69
|
+
const { mime, data } = (0, files_js_1.parseDataUri)(trimmed, urlParam);
|
|
70
|
+
const validMime = (0, files_js_1.validateImageMime)(mime, urlParam);
|
|
71
|
+
const verifiedMime = (0, files_js_1.verifyImageBytes)(data, validMime, urlParam);
|
|
72
|
+
const fname = (0, files_js_1.sanitizeFilename)(filename, "image", verifiedMime);
|
|
73
|
+
return { type: "inline_file", data, mediaType: verifiedMime, filename: fname, kind: "image", param: urlParam };
|
|
74
|
+
}
|
|
75
|
+
const validUrl = (0, files_js_1.validateRemoteUrl)(trimmed, urlParam);
|
|
76
|
+
return { type: "remote_file", url: validUrl, kind: "image", filename, param: urlParam };
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
if (err instanceof errors_js_1.CoreAPIError) {
|
|
80
|
+
throw new errors_js_2.BadRequestError(err.message, { status: 400, param: (_a = err.param) !== null && _a !== void 0 ? _a : urlParam });
|
|
81
|
+
}
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function parseInputFile(block, path) {
|
|
86
|
+
var _a, _b, _c, _d, _e, _f;
|
|
87
|
+
const rawData = block["file_data"];
|
|
88
|
+
const rawUrl = block["file_url"];
|
|
89
|
+
const rawFileId = block["file_id"];
|
|
90
|
+
const hasData = rawData !== undefined && rawData !== null && rawData !== "";
|
|
91
|
+
const hasUrl = rawUrl !== undefined && rawUrl !== null && rawUrl !== "";
|
|
92
|
+
const hasFileId = rawFileId !== undefined && rawFileId !== null && rawFileId !== "";
|
|
93
|
+
if (Number(hasData) + Number(hasUrl) + Number(hasFileId) !== 1) {
|
|
94
|
+
throw new errors_js_2.BadRequestError(`${path} must specify exactly one of 'file_data', 'file_url', or 'file_id'.`, {
|
|
95
|
+
status: 400,
|
|
96
|
+
param: path,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const rawFilename = block["filename"];
|
|
100
|
+
if (rawFilename !== undefined && rawFilename !== null && typeof rawFilename !== "string") {
|
|
101
|
+
throw new errors_js_2.BadRequestError(`${path}.filename must be a string.`, { status: 400, param: `${path}.filename` });
|
|
102
|
+
}
|
|
103
|
+
const filename = typeof rawFilename === "string" ? rawFilename : undefined;
|
|
104
|
+
const rawMime = block["media_type"] !== undefined ? block["media_type"] : block["mime_type"];
|
|
105
|
+
if (rawMime !== undefined && rawMime !== null && typeof rawMime !== "string") {
|
|
106
|
+
throw new errors_js_2.BadRequestError(`${path}.media_type must be a string.`, { status: 400, param: `${path}.media_type` });
|
|
107
|
+
}
|
|
108
|
+
const mimeStr = typeof rawMime === "string" ? rawMime : undefined;
|
|
109
|
+
if (hasFileId) {
|
|
110
|
+
const fidParam = `${path}.file_id`;
|
|
111
|
+
if (typeof rawFileId !== "string" || !rawFileId.trim()) {
|
|
112
|
+
throw new errors_js_2.BadRequestError(`${path}.file_id must be a non-empty string.`, { status: 400, param: fidParam });
|
|
113
|
+
}
|
|
114
|
+
const fid = rawFileId.trim();
|
|
115
|
+
if ((0, files_js_1.isOpenAIFileId)(fid)) {
|
|
116
|
+
throw new errors_js_2.BadRequestError(`OpenAI file_id ${JSON.stringify(fid)} is not supported; pass a Sarvam artifact_id belonging to this session, ` +
|
|
117
|
+
"file_data (base64), or an http(s) file_url.", { status: 400, param: fidParam });
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
type: "file_ref",
|
|
121
|
+
fileId: fid,
|
|
122
|
+
mediaType: (_a = (0, files_js_1.normalizeMime)(mimeStr)) !== null && _a !== void 0 ? _a : undefined,
|
|
123
|
+
filename,
|
|
124
|
+
kind: "file",
|
|
125
|
+
param: fidParam,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (hasUrl) {
|
|
129
|
+
const urlParam = `${path}.file_url`;
|
|
130
|
+
if (typeof rawUrl !== "string" || !rawUrl.trim()) {
|
|
131
|
+
throw new errors_js_2.BadRequestError(`${path}.file_url must be a non-empty string.`, { status: 400, param: urlParam });
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const validUrl = (0, files_js_1.validateRemoteUrl)(rawUrl, urlParam);
|
|
135
|
+
const checkedMime = mimeStr ? (0, files_js_1.validateFileMime)(mimeStr, `${path}.media_type`) : undefined;
|
|
136
|
+
return { type: "remote_file", url: validUrl, kind: "file", filename, mediaType: checkedMime, param: urlParam };
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
if (err instanceof errors_js_1.CoreAPIError) {
|
|
140
|
+
throw new errors_js_2.BadRequestError(err.message, { status: 400, param: (_b = err.param) !== null && _b !== void 0 ? _b : urlParam });
|
|
141
|
+
}
|
|
142
|
+
throw err;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const dataParam = `${path}.file_data`;
|
|
146
|
+
if (typeof rawData !== "string" || !rawData.trim()) {
|
|
147
|
+
throw new errors_js_2.BadRequestError(`${path}.file_data must be a non-empty string.`, { status: 400, param: dataParam });
|
|
148
|
+
}
|
|
149
|
+
const trimmed = rawData.trim();
|
|
150
|
+
try {
|
|
151
|
+
let data;
|
|
152
|
+
let validMime;
|
|
153
|
+
if (trimmed.startsWith("data:")) {
|
|
154
|
+
const parsed = (0, files_js_1.parseDataUri)(trimmed, dataParam);
|
|
155
|
+
data = parsed.data;
|
|
156
|
+
validMime = (0, files_js_1.validateFileMime)((_c = (0, files_js_1.normalizeMime)(mimeStr)) !== null && _c !== void 0 ? _c : parsed.mime, dataParam);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
data = (0, files_js_1.decodeBase64Bytes)(trimmed, dataParam);
|
|
160
|
+
const chosen = (_e = (_d = (0, files_js_1.normalizeMime)(mimeStr)) !== null && _d !== void 0 ? _d : (0, files_js_1.inferMimeFromFilename)(filename)) !== null && _e !== void 0 ? _e : "application/octet-stream";
|
|
161
|
+
validMime = (0, files_js_1.validateFileMime)(chosen, dataParam);
|
|
162
|
+
}
|
|
163
|
+
const fname = (0, files_js_1.sanitizeFilename)(filename, "file", validMime);
|
|
164
|
+
return { type: "inline_file", data, mediaType: validMime, filename: fname, kind: "file", param: dataParam };
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
if (err instanceof errors_js_1.CoreAPIError) {
|
|
168
|
+
throw new errors_js_2.BadRequestError(err.message, { status: 400, param: (_f = err.param) !== null && _f !== void 0 ? _f : dataParam });
|
|
169
|
+
}
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
15
173
|
function parseInputParts(rawInput) {
|
|
16
174
|
var _a;
|
|
17
175
|
if (typeof rawInput === "string")
|
|
18
176
|
return [{ type: "text", text: rawInput }];
|
|
19
177
|
if (!Array.isArray(rawInput)) {
|
|
20
|
-
throw new
|
|
178
|
+
throw new errors_js_2.BadRequestError("input must be a string or an array of message objects.", { status: 400, param: "input" });
|
|
21
179
|
}
|
|
22
180
|
const parts = [];
|
|
23
181
|
for (let i = 0; i < rawInput.length; i++) {
|
|
@@ -27,7 +185,7 @@ function parseInputParts(rawInput) {
|
|
|
27
185
|
continue;
|
|
28
186
|
}
|
|
29
187
|
if (!isObj(entry)) {
|
|
30
|
-
throw new
|
|
188
|
+
throw new errors_js_2.BadRequestError(`input[${i}] must be a string or message object.`, { status: 400, param: `input[${i}]` });
|
|
31
189
|
}
|
|
32
190
|
const content = entry["content"];
|
|
33
191
|
if (typeof content === "string") {
|
|
@@ -36,10 +194,11 @@ function parseInputParts(rawInput) {
|
|
|
36
194
|
else if (Array.isArray(content)) {
|
|
37
195
|
for (let j = 0; j < content.length; j++) {
|
|
38
196
|
const block = content[j];
|
|
197
|
+
const blockPath = `input[${i}].content[${j}]`;
|
|
39
198
|
if (!isObj(block)) {
|
|
40
|
-
throw new
|
|
199
|
+
throw new errors_js_2.BadRequestError(`${blockPath} must be an object.`, {
|
|
41
200
|
status: 400,
|
|
42
|
-
param:
|
|
201
|
+
param: blockPath,
|
|
43
202
|
});
|
|
44
203
|
}
|
|
45
204
|
const btype = block["type"];
|
|
@@ -47,10 +206,10 @@ function parseInputParts(rawInput) {
|
|
|
47
206
|
parts.push({ type: "text", text: String((_a = block["text"]) !== null && _a !== void 0 ? _a : "") });
|
|
48
207
|
}
|
|
49
208
|
else if (btype === "input_image") {
|
|
50
|
-
(
|
|
209
|
+
parts.push(parseInputImage(block, blockPath));
|
|
51
210
|
}
|
|
52
211
|
else if (btype === "input_file") {
|
|
53
|
-
(
|
|
212
|
+
parts.push(parseInputFile(block, blockPath));
|
|
54
213
|
}
|
|
55
214
|
else {
|
|
56
215
|
(0, types_js_1.refuseOpenAI)(`input.${String(btype)}`, { reason: "unsupported input content part", param: "input" });
|
|
@@ -58,12 +217,26 @@ function parseInputParts(rawInput) {
|
|
|
58
217
|
}
|
|
59
218
|
}
|
|
60
219
|
else {
|
|
61
|
-
throw new
|
|
220
|
+
throw new errors_js_2.BadRequestError(`input[${i}].content must be a string or array of content parts.`, {
|
|
62
221
|
status: 400,
|
|
63
222
|
param: `input[${i}].content`,
|
|
64
223
|
});
|
|
65
224
|
}
|
|
66
225
|
}
|
|
226
|
+
let attachmentCount = 0;
|
|
227
|
+
let inlineBytes = 0;
|
|
228
|
+
for (const p of parts) {
|
|
229
|
+
if (p.type !== "text")
|
|
230
|
+
attachmentCount += 1;
|
|
231
|
+
if (p.type === "inline_file")
|
|
232
|
+
inlineBytes += p.data.byteLength;
|
|
233
|
+
}
|
|
234
|
+
if (attachmentCount > files_js_1.MAX_ATTACHMENTS_PER_MESSAGE) {
|
|
235
|
+
throw new errors_js_2.BadRequestError(`A message can contain at most ${files_js_1.MAX_ATTACHMENTS_PER_MESSAGE} attachments (got ${attachmentCount}).`, { status: 400, param: "input" });
|
|
236
|
+
}
|
|
237
|
+
if (inlineBytes > files_js_1.MAX_TOTAL_ATTACHMENT_BYTES) {
|
|
238
|
+
throw new errors_js_2.BadRequestError(`Combined attachment size exceeds the maximum of ${Math.floor(files_js_1.MAX_TOTAL_ATTACHMENT_BYTES / (1024 * 1024))} MB per message.`, { status: 400, param: "input" });
|
|
239
|
+
}
|
|
67
240
|
return parts;
|
|
68
241
|
}
|
|
69
242
|
function stringifyToolOutput(output) {
|
|
@@ -111,7 +284,7 @@ class EventsResource {
|
|
|
111
284
|
? params["idempotency_key"]
|
|
112
285
|
: options === null || options === void 0 ? void 0 : options.idempotencyKey;
|
|
113
286
|
if (!Array.isArray(params["events"])) {
|
|
114
|
-
throw new
|
|
287
|
+
throw new errors_js_2.BadRequestError("events must be an array.", { status: 400, param: "events" });
|
|
115
288
|
}
|
|
116
289
|
const rawEvents = params["events"];
|
|
117
290
|
// Validate and parse all events before issuing any HTTP request.
|
|
@@ -119,7 +292,7 @@ class EventsResource {
|
|
|
119
292
|
for (let i = 0; i < rawEvents.length; i++) {
|
|
120
293
|
const ev = rawEvents[i];
|
|
121
294
|
if (!isObj(ev)) {
|
|
122
|
-
throw new
|
|
295
|
+
throw new errors_js_2.BadRequestError(`events[${i}] must be an object.`, { status: 400, param: `events[${i}]` });
|
|
123
296
|
}
|
|
124
297
|
const etype = ev["type"];
|
|
125
298
|
if (etype === "agent.session.input.message") {
|
|
@@ -158,7 +331,7 @@ class EventsResource {
|
|
|
158
331
|
return undefined;
|
|
159
332
|
return multi ? `${baseKey}:${idx}` : baseKey;
|
|
160
333
|
};
|
|
161
|
-
await (0,
|
|
334
|
+
await (0, errors_js_2.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, async () => {
|
|
162
335
|
for (let idx = 0; idx < ops.length; idx++) {
|
|
163
336
|
const op = ops[idx];
|
|
164
337
|
const key = keyFor(idx);
|
|
@@ -206,18 +379,42 @@ class EventsResource {
|
|
|
206
379
|
}
|
|
207
380
|
exports.EventsResource = EventsResource;
|
|
208
381
|
function recordsToItems(records) {
|
|
382
|
+
var _a;
|
|
209
383
|
const items = [];
|
|
210
384
|
for (const rec of records) {
|
|
211
385
|
for (const ev of rec.events) {
|
|
212
386
|
if (ev.kind === "text_done") {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
387
|
+
if (rec.role === "user") {
|
|
388
|
+
const blocks = [];
|
|
389
|
+
const atts = (_a = ev.attachments) !== null && _a !== void 0 ? _a : [];
|
|
390
|
+
if (ev.text || atts.length === 0) {
|
|
391
|
+
blocks.push({ type: "input_text", text: ev.text });
|
|
392
|
+
}
|
|
393
|
+
for (const att of atts) {
|
|
394
|
+
if ((att.mediaType || "").toLowerCase().startsWith("image/")) {
|
|
395
|
+
blocks.push({ type: "input_image", image_url: `${files_js_1.ARTIFACT_URI_PREFIX}${att.fileId}` });
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
blocks.push({ type: "input_file", file_id: att.fileId, filename: att.filename });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
items.push({
|
|
402
|
+
id: ev.itemId,
|
|
403
|
+
type: "message",
|
|
404
|
+
role: rec.role,
|
|
405
|
+
status: "completed",
|
|
406
|
+
content: blocks,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
items.push({
|
|
411
|
+
id: ev.itemId,
|
|
412
|
+
type: "message",
|
|
413
|
+
role: rec.role,
|
|
414
|
+
status: "completed",
|
|
415
|
+
content: [{ type: "output_text", text: ev.text }],
|
|
416
|
+
});
|
|
417
|
+
}
|
|
221
418
|
}
|
|
222
419
|
else if (ev.kind === "tool_called") {
|
|
223
420
|
const itype = ev.name === "web_search" ? "web_search_call" : "sarvam.tool_call";
|
|
@@ -267,7 +464,7 @@ class ItemsResource {
|
|
|
267
464
|
const slice = descItems.slice(off, off + lim);
|
|
268
465
|
return [slice, off + lim < descItems.length];
|
|
269
466
|
}, (it) => it.id);
|
|
270
|
-
const [items, hasMore] = await (0,
|
|
467
|
+
const [items, hasMore] = await (0, errors_js_2.wrapCoreErrors)(() => pager.page(opts));
|
|
271
468
|
return new pages_js_1.SyncCursorPage({
|
|
272
469
|
data: items,
|
|
273
470
|
hasMore,
|
|
@@ -280,7 +477,7 @@ class ItemsResource {
|
|
|
280
477
|
const limit = params["limit"] !== undefined ? params["limit"] : 20;
|
|
281
478
|
const order = params["order"] !== undefined ? String(params["order"]) : "asc";
|
|
282
479
|
const after = typeof params["after"] === "string" ? params["after"] : undefined;
|
|
283
|
-
const allRecords = await (0,
|
|
480
|
+
const allRecords = await (0, errors_js_2.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => (0, history_js_1.history)(this.http, sessionId)));
|
|
284
481
|
const descItems = recordsToItems(allRecords).reverse();
|
|
285
482
|
return this.pageFromCached(descItems, { limit, order, after });
|
|
286
483
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SessionsResource = void 0;
|
|
4
4
|
exports.recordToSession = recordToSession;
|
|
5
5
|
const paging_js_1 = require("../../core/paging.js");
|
|
6
|
+
const turns_js_1 = require("../../core/turns.js");
|
|
6
7
|
const errors_js_1 = require("../errors.js");
|
|
7
8
|
const pages_js_1 = require("../pages.js");
|
|
8
9
|
const stream_js_1 = require("../stream.js");
|
|
@@ -87,7 +88,7 @@ class SessionsResource {
|
|
|
87
88
|
this.traces = new agents_js_1.RefusedResource("beta.agents.sessions.traces");
|
|
88
89
|
}
|
|
89
90
|
async create(params = {}, options) {
|
|
90
|
-
var _a;
|
|
91
|
+
var _a, _b;
|
|
91
92
|
const known = new Set([
|
|
92
93
|
"environment", "agent", "agent_id", "input", "metadata", "stream", "pin_agent_version", "sarvam_config", "idempotency_key",
|
|
93
94
|
]);
|
|
@@ -115,7 +116,15 @@ class SessionsResource {
|
|
|
115
116
|
throw new errors_js_1.BadRequestError("Pass exactly one of 'agent' or 'agent_id'.", { status: 400, param: "agent" });
|
|
116
117
|
}
|
|
117
118
|
// Validate input before deploying an inline agent or creating a conversation.
|
|
118
|
-
|
|
119
|
+
let parsedParts = hasInput ? (0, events_js_1.parseInputParts)(params["input"]) : null;
|
|
120
|
+
if (parsedParts !== null) {
|
|
121
|
+
for (const p of parsedParts) {
|
|
122
|
+
if (p.type === "file_ref") {
|
|
123
|
+
throw new errors_js_1.BadRequestError(`file_id ${JSON.stringify(p.fileId)} cannot be used on sessions.create because Panja artifacts are scoped to an ` +
|
|
124
|
+
"existing session; pass inline base64 data or an http(s) URL instead.", { status: 400, param: (_b = p.param) !== null && _b !== void 0 ? _b : "file_id" });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
119
128
|
let resolvedAgentId;
|
|
120
129
|
if (hasAgent) {
|
|
121
130
|
if (!isObj(params["agent"])) {
|
|
@@ -146,6 +155,11 @@ class SessionsResource {
|
|
|
146
155
|
}
|
|
147
156
|
resolvedAgentId = aid;
|
|
148
157
|
}
|
|
158
|
+
// Fetch any remote URLs BEFORE creating the conversation so a bad/oversized URL never orphans a session.
|
|
159
|
+
if (parsedParts !== null) {
|
|
160
|
+
const toPrefetch = parsedParts;
|
|
161
|
+
parsedParts = await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => (0, turns_js_1.prefetchRemoteParts)(this.http, toPrefetch)));
|
|
162
|
+
}
|
|
149
163
|
const meta = isObj(params["metadata"])
|
|
150
164
|
? Object.fromEntries(Object.entries(params["metadata"]).map(([k, v]) => [k, String(v)]))
|
|
151
165
|
: undefined;
|
|
@@ -166,12 +180,27 @@ class SessionsResource {
|
|
|
166
180
|
return evStream;
|
|
167
181
|
};
|
|
168
182
|
if (parsedParts !== null) {
|
|
183
|
+
let msgParts;
|
|
184
|
+
try {
|
|
185
|
+
const toResolve = parsedParts;
|
|
186
|
+
msgParts = await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => this.turns.resolveParts(sessionRec.id, toResolve)));
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
try {
|
|
190
|
+
await this.sessions.delete(sessionRec.id);
|
|
191
|
+
}
|
|
192
|
+
catch (_c) {
|
|
193
|
+
// Best-effort cleanup of empty conversation after failed upload.
|
|
194
|
+
}
|
|
195
|
+
throw err;
|
|
196
|
+
}
|
|
197
|
+
const msgId = this.http.newIdempotencyKey();
|
|
169
198
|
if (stream) {
|
|
170
|
-
const rawStream = await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => this.turns.
|
|
199
|
+
const rawStream = await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => this.turns.sendMessageParts(sessionRec.id, resolvedAgentId, msgParts, { msgId, returnStream: true })));
|
|
171
200
|
const listener = this.turns.listen(sessionRec.id, { initialStream: rawStream, reqOpts });
|
|
172
201
|
return bindSignal(new stream_js_1.AgentSessionEventStream(sessionRec.id, { listener, sessionCreated: sessionObj }));
|
|
173
202
|
}
|
|
174
|
-
await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => this.turns.
|
|
203
|
+
await (0, errors_js_1.wrapCoreErrors)(() => this.http.withRequestOptions(reqOpts, () => this.turns.sendMessageParts(sessionRec.id, resolvedAgentId, msgParts, { msgId, returnStream: false })));
|
|
175
204
|
return sessionObj;
|
|
176
205
|
}
|
|
177
206
|
if (stream) {
|
|
@@ -199,21 +199,12 @@ export const REFUSALS = [
|
|
|
199
199
|
},
|
|
200
200
|
{
|
|
201
201
|
"provider": "openai",
|
|
202
|
-
"path": "input.input_image",
|
|
202
|
+
"path": "input.input_image.detail",
|
|
203
203
|
"param": "input",
|
|
204
|
-
"reason": "
|
|
205
|
-
"hint": "
|
|
206
|
-
"needs":
|
|
207
|
-
"feature": "openai.input.image"
|
|
208
|
-
},
|
|
209
|
-
{
|
|
210
|
-
"provider": "openai",
|
|
211
|
-
"path": "input.input_file",
|
|
212
|
-
"param": "input",
|
|
213
|
-
"reason": "file input parts are not supported yet in 0.6.0",
|
|
214
|
-
"hint": "Planned for 0.6.2 once provider fixtures are confirmed.",
|
|
215
|
-
"needs": "sdk: 0.6.2 image/file input",
|
|
216
|
-
"feature": "openai.input.file"
|
|
204
|
+
"reason": "input_image.detail is not supported on agent sessions",
|
|
205
|
+
"hint": "Omit detail on input_image blocks; Sarvam ignores image detail tiers.",
|
|
206
|
+
"needs": null,
|
|
207
|
+
"feature": "openai.input.image.detail"
|
|
217
208
|
},
|
|
218
209
|
{
|
|
219
210
|
"provider": "openai",
|
|
@@ -7,6 +7,11 @@ export interface Usage {
|
|
|
7
7
|
reasoningTokens: number;
|
|
8
8
|
totalTokens: number;
|
|
9
9
|
}
|
|
10
|
+
export interface AttachmentRef {
|
|
11
|
+
fileId: string;
|
|
12
|
+
filename: string;
|
|
13
|
+
mediaType: string;
|
|
14
|
+
}
|
|
10
15
|
export type TurnEvent = {
|
|
11
16
|
kind: "turn_started";
|
|
12
17
|
responseId: string;
|
|
@@ -21,6 +26,7 @@ export type TurnEvent = {
|
|
|
21
26
|
kind: "text_done";
|
|
22
27
|
itemId: string;
|
|
23
28
|
text: string;
|
|
29
|
+
attachments?: AttachmentRef[];
|
|
24
30
|
} | {
|
|
25
31
|
kind: "reasoning_started";
|
|
26
32
|
itemId: string;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { MessagePart } from "../../models/MessagePart.js";
|
|
2
|
+
import { Http } from "./http.js";
|
|
3
|
+
/** Matches Panja's artifact_contract.limits.MAX_UPLOAD_BYTES (20 MiB). */
|
|
4
|
+
export declare const MAX_UPLOAD_BYTES: number;
|
|
5
|
+
/** Maximum number of file/image attachments in a single message (below Panja's 32-attachment cap). */
|
|
6
|
+
export declare const MAX_ATTACHMENTS_PER_MESSAGE = 20;
|
|
7
|
+
/** Maximum combined size of all attachments in a single message (50 MiB). */
|
|
8
|
+
export declare const MAX_TOTAL_ATTACHMENT_BYTES: number;
|
|
9
|
+
export declare const MAX_REDIRECTS = 5;
|
|
10
|
+
export declare const DEFAULT_REMOTE_FETCH_TIMEOUT_MS = 30000;
|
|
11
|
+
export declare const ARTIFACT_URI_PREFIX = "artifact://";
|
|
12
|
+
export declare const ACCEPTED_IMAGE_MIMES: Set<string>;
|
|
13
|
+
export interface UploadedArtifact {
|
|
14
|
+
artifactId: string;
|
|
15
|
+
filename: string;
|
|
16
|
+
mediaType: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class AttachmentUploadCache {
|
|
19
|
+
private readonly max;
|
|
20
|
+
private readonly store;
|
|
21
|
+
constructor(maxEntries?: number);
|
|
22
|
+
private keyOf;
|
|
23
|
+
get(sessionId: string, idempotencyKey: string, partIndex: number, sha256Hex: string): UploadedArtifact | undefined;
|
|
24
|
+
put(sessionId: string, idempotencyKey: string, partIndex: number, sha256Hex: string, artifact: UploadedArtifact): void;
|
|
25
|
+
evict(sessionId: string, idempotencyKey: string, partIndex: number, sha256Hex: string): void;
|
|
26
|
+
}
|
|
27
|
+
export declare function isOpenAIFileId(fileId: string): boolean;
|
|
28
|
+
export declare function normalizeMime(raw?: string | null): string | null;
|
|
29
|
+
export declare function inferMimeFromFilename(filename?: string | null): string | null;
|
|
30
|
+
export declare function sanitizeFilename(name: string | null | undefined, defaultStem: string, mediaType: string): string;
|
|
31
|
+
export declare function sniffImageMime(data: Uint8Array): string | null;
|
|
32
|
+
export declare function verifyImageBytes(data: Uint8Array, declaredMime: string | null | undefined, param?: string | null): string;
|
|
33
|
+
export declare function validateImageMime(mime: string | null | undefined, param?: string | null): string;
|
|
34
|
+
export declare function validateFileMime(mime: string | null | undefined, param?: string | null): string;
|
|
35
|
+
export declare function decodeBase64Bytes(raw: string, param?: string | null): Uint8Array;
|
|
36
|
+
export declare function parseDataUri(raw: string, param?: string | null): {
|
|
37
|
+
mime: string;
|
|
38
|
+
data: Uint8Array;
|
|
39
|
+
};
|
|
40
|
+
export declare function validateRemoteUrl(url: string, param?: string | null): string;
|
|
41
|
+
export declare function isBlockedIp(rawIp: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Validate that ``rawUrl`` does not target a private, loopback, link-local, or reserved address,
|
|
44
|
+
* and return the validated IP address to pin the socket connection to (closing the DNS rebinding
|
|
45
|
+
* TOCTOU gap), or ``null`` when ``allowPrivateRemoteUrls`` is enabled.
|
|
46
|
+
*/
|
|
47
|
+
export declare function checkRemoteHostAllowed(http: Http, rawUrl: string, param?: string | null): Promise<string | null>;
|
|
48
|
+
export declare function fetchRemoteFile(http: Http, url: string, opts: {
|
|
49
|
+
kind: "image" | "file";
|
|
50
|
+
filename?: string | null;
|
|
51
|
+
mediaType?: string | null;
|
|
52
|
+
param?: string | null;
|
|
53
|
+
maxBytes?: number;
|
|
54
|
+
}): Promise<{
|
|
55
|
+
data: Uint8Array;
|
|
56
|
+
mediaType: string;
|
|
57
|
+
filename: string;
|
|
58
|
+
}>;
|
|
59
|
+
export declare function deleteArtifactBestEffort(http: Http, sessionId: string, artifactId: string): Promise<void>;
|
|
60
|
+
export declare function uploadArtifact(http: Http, sessionId: string, data: Uint8Array, opts: {
|
|
61
|
+
filename: string;
|
|
62
|
+
mediaType: string;
|
|
63
|
+
}): Promise<UploadedArtifact>;
|
|
64
|
+
export declare function listSessionArtifacts(http: Http, sessionId: string): Promise<Map<string, UploadedArtifact>>;
|
|
65
|
+
export declare function makeFileMessagePart(artifactId: string, filename: string, mediaType: string): MessagePart;
|