sarvam-agents-client 0.5.0 → 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/agents.d.ts +53 -0
- package/dist/compat/core/agents.js +338 -0
- package/dist/compat/core/data.d.ts +10 -0
- package/dist/compat/core/data.js +275 -0
- package/dist/compat/core/errors.d.ts +37 -0
- package/dist/compat/core/errors.js +155 -0
- package/dist/compat/core/events.d.ts +107 -0
- package/dist/compat/core/events.js +160 -0
- package/dist/compat/core/files.d.ts +65 -0
- package/dist/compat/core/files.js +851 -0
- package/dist/compat/core/history.d.ts +9 -0
- package/dist/compat/core/history.js +112 -0
- package/dist/compat/core/http.d.ts +112 -0
- package/dist/compat/core/http.js +753 -0
- package/dist/compat/core/ids.d.ts +4 -0
- package/dist/compat/core/ids.js +36 -0
- package/dist/compat/core/index.d.ts +14 -0
- package/dist/compat/core/index.js +30 -0
- package/dist/compat/core/models.d.ts +20 -0
- package/dist/compat/core/models.js +75 -0
- package/dist/compat/core/paging.d.ts +14 -0
- package/dist/compat/core/paging.js +124 -0
- package/dist/compat/core/refusals.d.ts +9 -0
- package/dist/compat/core/refusals.js +25 -0
- package/dist/compat/core/sessions.d.ts +65 -0
- package/dist/compat/core/sessions.js +317 -0
- package/dist/compat/core/toolResults.d.ts +43 -0
- package/dist/compat/core/toolResults.js +208 -0
- package/dist/compat/core/turns.d.ts +104 -0
- package/dist/compat/core/turns.js +504 -0
- package/dist/compat/core/warnings.d.ts +6 -0
- package/dist/compat/core/warnings.js +26 -0
- package/dist/compat/index.d.ts +3 -0
- package/dist/compat/index.js +9 -0
- package/dist/compat/openai/client.d.ts +62 -0
- package/dist/compat/openai/client.js +108 -0
- package/dist/compat/openai/errors.d.ts +72 -0
- package/dist/compat/openai/errors.js +150 -0
- package/dist/compat/openai/index.d.ts +14 -0
- package/dist/compat/openai/index.js +42 -0
- package/dist/compat/openai/pages.d.ts +19 -0
- package/dist/compat/openai/pages.js +39 -0
- package/dist/compat/openai/resources/agents.d.ts +36 -0
- package/dist/compat/openai/resources/agents.js +549 -0
- package/dist/compat/openai/resources/events.d.ts +23 -0
- package/dist/compat/openai/resources/events.js +485 -0
- package/dist/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/compat/openai/resources/sessions.js +283 -0
- package/dist/compat/openai/stream.d.ts +40 -0
- package/dist/compat/openai/stream.js +585 -0
- package/dist/compat/openai/types.d.ts +125 -0
- package/dist/compat/openai/types.js +98 -0
- package/dist/compose/agent.d.ts +2 -0
- package/dist/compose/agent.js +27 -0
- package/dist/compose/functionTool.d.ts +2 -0
- package/dist/compose/functionTool.js +46 -0
- package/dist/compose/output.d.ts +2 -0
- package/dist/compose/output.js +15 -7
- package/dist/esm/compat/core/agents.d.ts +53 -0
- package/dist/esm/compat/core/agents.js +334 -0
- package/dist/esm/compat/core/data.d.ts +10 -0
- package/dist/esm/compat/core/data.js +272 -0
- package/dist/esm/compat/core/errors.d.ts +37 -0
- package/dist/esm/compat/core/errors.js +146 -0
- package/dist/esm/compat/core/events.d.ts +107 -0
- package/dist/esm/compat/core/events.js +156 -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.d.ts +9 -0
- package/dist/esm/compat/core/history.js +109 -0
- package/dist/esm/compat/core/http.d.ts +112 -0
- package/dist/esm/compat/core/http.js +746 -0
- package/dist/esm/compat/core/ids.d.ts +4 -0
- package/dist/esm/compat/core/ids.js +30 -0
- package/dist/esm/compat/core/index.d.ts +14 -0
- package/dist/esm/compat/core/index.js +14 -0
- package/dist/esm/compat/core/models.d.ts +20 -0
- package/dist/esm/compat/core/models.js +71 -0
- package/dist/esm/compat/core/paging.d.ts +14 -0
- package/dist/esm/compat/core/paging.js +118 -0
- package/dist/esm/compat/core/refusals.d.ts +9 -0
- package/dist/esm/compat/core/refusals.js +21 -0
- package/dist/esm/compat/core/sessions.d.ts +65 -0
- package/dist/esm/compat/core/sessions.js +312 -0
- package/dist/esm/compat/core/toolResults.d.ts +43 -0
- package/dist/esm/compat/core/toolResults.js +203 -0
- package/dist/esm/compat/core/turns.d.ts +104 -0
- package/dist/esm/compat/core/turns.js +497 -0
- package/dist/esm/compat/core/warnings.d.ts +6 -0
- package/dist/esm/compat/core/warnings.js +20 -0
- package/dist/esm/compat/index.d.ts +3 -0
- package/dist/esm/compat/index.js +3 -0
- package/dist/esm/compat/openai/client.d.ts +62 -0
- package/dist/esm/compat/openai/client.js +104 -0
- package/dist/esm/compat/openai/errors.d.ts +72 -0
- package/dist/esm/compat/openai/errors.js +130 -0
- package/dist/esm/compat/openai/index.d.ts +14 -0
- package/dist/esm/compat/openai/index.js +17 -0
- package/dist/esm/compat/openai/pages.d.ts +19 -0
- package/dist/esm/compat/openai/pages.js +35 -0
- package/dist/esm/compat/openai/resources/agents.d.ts +36 -0
- package/dist/esm/compat/openai/resources/agents.js +540 -0
- package/dist/esm/compat/openai/resources/events.d.ts +23 -0
- package/dist/esm/compat/openai/resources/events.js +479 -0
- package/dist/esm/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/esm/compat/openai/resources/sessions.js +278 -0
- package/dist/esm/compat/openai/stream.d.ts +40 -0
- package/dist/esm/compat/openai/stream.js +579 -0
- package/dist/esm/compat/openai/types.d.ts +125 -0
- package/dist/esm/compat/openai/types.js +91 -0
- package/dist/esm/compose/agent.d.ts +2 -0
- package/dist/esm/compose/agent.js +28 -2
- package/dist/esm/compose/functionTool.d.ts +2 -0
- package/dist/esm/compose/functionTool.js +45 -0
- package/dist/esm/compose/output.d.ts +2 -0
- package/dist/esm/compose/output.js +14 -7
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/package.json +53 -11
- package/src/compat/core/agents.ts +437 -0
- package/src/compat/core/data.ts +283 -0
- package/src/compat/core/errors.ts +162 -0
- package/src/compat/core/events.ts +198 -0
- package/src/compat/core/files.ts +913 -0
- package/src/compat/core/history.ts +116 -0
- package/src/compat/core/http.ts +827 -0
- package/src/compat/core/ids.ts +35 -0
- package/src/compat/core/index.ts +14 -0
- package/src/compat/core/models.ts +85 -0
- package/src/compat/core/paging.ts +119 -0
- package/src/compat/core/refusals.ts +31 -0
- package/src/compat/core/sessions.ts +358 -0
- package/src/compat/core/toolResults.ts +235 -0
- package/src/compat/core/turns.ts +622 -0
- package/src/compat/core/warnings.ts +23 -0
- package/src/compat/index.ts +3 -0
- package/src/compat/openai/client.ts +177 -0
- package/src/compat/openai/errors.ts +165 -0
- package/src/compat/openai/index.ts +65 -0
- package/src/compat/openai/pages.ts +55 -0
- package/src/compat/openai/resources/agents.ts +603 -0
- package/src/compat/openai/resources/events.ts +523 -0
- package/src/compat/openai/resources/sessions.ts +369 -0
- package/src/compat/openai/stream.ts +599 -0
- package/src/compat/openai/types.ts +217 -0
- package/src/compose/agent.ts +30 -3
- package/src/compose/functionTool.ts +44 -0
- package/src/compose/output.ts +14 -8
- package/src/index.ts +3 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Neutral error types for sarvam-agents-client/compat/core. */
|
|
2
|
+
export interface CoreErrorOptions {
|
|
3
|
+
status?: number | null;
|
|
4
|
+
code?: string | null;
|
|
5
|
+
param?: string | null;
|
|
6
|
+
requestId?: string | null;
|
|
7
|
+
body?: unknown;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
export declare class CoreAPIError extends Error {
|
|
11
|
+
readonly status: number | null;
|
|
12
|
+
readonly code: string | null;
|
|
13
|
+
readonly param: string | null;
|
|
14
|
+
readonly requestId: string | null;
|
|
15
|
+
readonly body: unknown;
|
|
16
|
+
readonly headers: Record<string, string>;
|
|
17
|
+
constructor(message: string, opts?: CoreErrorOptions);
|
|
18
|
+
}
|
|
19
|
+
export declare class CoreConnectionError extends CoreAPIError {
|
|
20
|
+
constructor(message: string, opts?: CoreErrorOptions);
|
|
21
|
+
}
|
|
22
|
+
export declare class CoreTimeoutError extends CoreConnectionError {
|
|
23
|
+
constructor(message: string, opts?: CoreErrorOptions);
|
|
24
|
+
}
|
|
25
|
+
export declare class CoreUserAbortError extends CoreAPIError {
|
|
26
|
+
constructor(message?: string, opts?: CoreErrorOptions);
|
|
27
|
+
}
|
|
28
|
+
export declare class Unsupported extends CoreAPIError {
|
|
29
|
+
readonly feature: string;
|
|
30
|
+
readonly needs: string | null;
|
|
31
|
+
constructor(message: string, opts: {
|
|
32
|
+
param: string | null;
|
|
33
|
+
feature: string;
|
|
34
|
+
needs?: string | null;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export declare function mapPanjaError(status: number, rawBody: unknown, headers?: Record<string, string>): CoreAPIError;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Neutral error types for sarvam-agents-client/compat/core. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Unsupported = exports.CoreUserAbortError = exports.CoreTimeoutError = exports.CoreConnectionError = exports.CoreAPIError = void 0;
|
|
5
|
+
exports.mapPanjaError = mapPanjaError;
|
|
6
|
+
class CoreAPIError extends Error {
|
|
7
|
+
constructor(message, opts = {}) {
|
|
8
|
+
var _a, _b, _c, _d, _e, _f;
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "CoreAPIError";
|
|
11
|
+
this.status = (_a = opts.status) !== null && _a !== void 0 ? _a : null;
|
|
12
|
+
this.code = (_b = opts.code) !== null && _b !== void 0 ? _b : null;
|
|
13
|
+
this.param = (_c = opts.param) !== null && _c !== void 0 ? _c : null;
|
|
14
|
+
this.requestId = (_d = opts.requestId) !== null && _d !== void 0 ? _d : null;
|
|
15
|
+
this.body = (_e = opts.body) !== null && _e !== void 0 ? _e : null;
|
|
16
|
+
this.headers = { ...((_f = opts.headers) !== null && _f !== void 0 ? _f : {}) };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.CoreAPIError = CoreAPIError;
|
|
20
|
+
class CoreConnectionError extends CoreAPIError {
|
|
21
|
+
constructor(message, opts = {}) {
|
|
22
|
+
super(message, opts);
|
|
23
|
+
this.name = "CoreConnectionError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.CoreConnectionError = CoreConnectionError;
|
|
27
|
+
class CoreTimeoutError extends CoreConnectionError {
|
|
28
|
+
constructor(message, opts = {}) {
|
|
29
|
+
super(message, opts);
|
|
30
|
+
this.name = "CoreTimeoutError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.CoreTimeoutError = CoreTimeoutError;
|
|
34
|
+
class CoreUserAbortError extends CoreAPIError {
|
|
35
|
+
constructor(message = "Request was aborted.", opts = {}) {
|
|
36
|
+
super(message, { code: "user_abort", ...opts });
|
|
37
|
+
this.name = "CoreUserAbortError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.CoreUserAbortError = CoreUserAbortError;
|
|
41
|
+
class Unsupported extends CoreAPIError {
|
|
42
|
+
constructor(message, opts) {
|
|
43
|
+
var _a;
|
|
44
|
+
const body = {
|
|
45
|
+
error: {
|
|
46
|
+
message,
|
|
47
|
+
type: "invalid_request_error",
|
|
48
|
+
param: opts.param,
|
|
49
|
+
code: "unsupported_by_sarvam",
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
super(message, {
|
|
53
|
+
status: 400,
|
|
54
|
+
code: "unsupported_by_sarvam",
|
|
55
|
+
param: opts.param,
|
|
56
|
+
requestId: null,
|
|
57
|
+
body,
|
|
58
|
+
headers: {},
|
|
59
|
+
});
|
|
60
|
+
this.name = "Unsupported";
|
|
61
|
+
this.feature = opts.feature;
|
|
62
|
+
this.needs = (_a = opts.needs) !== null && _a !== void 0 ? _a : null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Unsupported = Unsupported;
|
|
66
|
+
function getHeader(headers, name) {
|
|
67
|
+
const low = name.toLowerCase();
|
|
68
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
69
|
+
if (k.toLowerCase() === low)
|
|
70
|
+
return v;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function parseBody(raw) {
|
|
75
|
+
if (raw === null || raw === undefined)
|
|
76
|
+
return null;
|
|
77
|
+
if (typeof raw === "object")
|
|
78
|
+
return raw;
|
|
79
|
+
if (typeof raw === "string" && raw.trim().length > 0) {
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(raw);
|
|
82
|
+
}
|
|
83
|
+
catch (_a) {
|
|
84
|
+
return raw;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function mapPanjaError(status, rawBody, headers = {}) {
|
|
90
|
+
const requestId = getHeader(headers, "x-request-id");
|
|
91
|
+
const body = parseBody(rawBody);
|
|
92
|
+
let code = null;
|
|
93
|
+
let param = null;
|
|
94
|
+
let message = `HTTP ${status}`;
|
|
95
|
+
if (body && typeof body === "object" && !Array.isArray(body)) {
|
|
96
|
+
const obj = body;
|
|
97
|
+
const err = obj["error"];
|
|
98
|
+
const detail = obj["detail"];
|
|
99
|
+
if (err && typeof err === "object" && !Array.isArray(err)) {
|
|
100
|
+
const e = err;
|
|
101
|
+
if (typeof e["code"] === "string")
|
|
102
|
+
code = e["code"];
|
|
103
|
+
if (typeof e["message"] === "string" && e["message"])
|
|
104
|
+
message = e["message"];
|
|
105
|
+
const details = e["details"];
|
|
106
|
+
if (Array.isArray(details) && details.length > 0 && details[0] && typeof details[0] === "object") {
|
|
107
|
+
const first = details[0];
|
|
108
|
+
if (first["reason"])
|
|
109
|
+
code = String(first["reason"]);
|
|
110
|
+
if (first["param"])
|
|
111
|
+
param = String(first["param"]);
|
|
112
|
+
else if (first["path"])
|
|
113
|
+
param = String(first["path"]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (Array.isArray(detail) && detail.length > 0 && detail[0] && typeof detail[0] === "object") {
|
|
117
|
+
const first = detail[0];
|
|
118
|
+
const loc = first["loc"];
|
|
119
|
+
if (Array.isArray(loc)) {
|
|
120
|
+
const parts = loc.map(String).filter((p) => p !== "body" && p !== "query" && p !== "path");
|
|
121
|
+
param = parts.length > 0 ? parts.join(".") : null;
|
|
122
|
+
}
|
|
123
|
+
if (typeof first["msg"] === "string" && first["msg"])
|
|
124
|
+
message = first["msg"];
|
|
125
|
+
code = "invalid_request_error";
|
|
126
|
+
}
|
|
127
|
+
else if (detail && typeof detail === "object" && !Array.isArray(detail)) {
|
|
128
|
+
const d = detail;
|
|
129
|
+
if (d["code"])
|
|
130
|
+
code = String(d["code"]);
|
|
131
|
+
else if (d["reason"])
|
|
132
|
+
code = String(d["reason"]);
|
|
133
|
+
if (typeof d["message"] === "string" && d["message"])
|
|
134
|
+
message = d["message"];
|
|
135
|
+
if (d["param"])
|
|
136
|
+
param = String(d["param"]);
|
|
137
|
+
}
|
|
138
|
+
else if (typeof detail === "string" && detail) {
|
|
139
|
+
message = detail;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (typeof body === "string" && body) {
|
|
143
|
+
message = body;
|
|
144
|
+
}
|
|
145
|
+
if (status === 422 && code === null)
|
|
146
|
+
code = "invalid_request_error";
|
|
147
|
+
return new CoreAPIError(message, {
|
|
148
|
+
status,
|
|
149
|
+
code,
|
|
150
|
+
param,
|
|
151
|
+
requestId,
|
|
152
|
+
body,
|
|
153
|
+
headers,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** Neutral turn event model built from Panja /v1/responses UI-message chunks. */
|
|
2
|
+
import { type FunctionCallRequest } from "../../compose/events.js";
|
|
3
|
+
export interface Usage {
|
|
4
|
+
inputTokens: number;
|
|
5
|
+
cachedTokens: number;
|
|
6
|
+
outputTokens: number;
|
|
7
|
+
reasoningTokens: number;
|
|
8
|
+
totalTokens: number;
|
|
9
|
+
}
|
|
10
|
+
export interface AttachmentRef {
|
|
11
|
+
fileId: string;
|
|
12
|
+
filename: string;
|
|
13
|
+
mediaType: string;
|
|
14
|
+
}
|
|
15
|
+
export type TurnEvent = {
|
|
16
|
+
kind: "turn_started";
|
|
17
|
+
responseId: string;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "text_started";
|
|
20
|
+
itemId: string;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "text_delta";
|
|
23
|
+
itemId: string;
|
|
24
|
+
delta: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "text_done";
|
|
27
|
+
itemId: string;
|
|
28
|
+
text: string;
|
|
29
|
+
attachments?: AttachmentRef[];
|
|
30
|
+
} | {
|
|
31
|
+
kind: "reasoning_started";
|
|
32
|
+
itemId: string;
|
|
33
|
+
} | {
|
|
34
|
+
kind: "reasoning_delta";
|
|
35
|
+
itemId: string;
|
|
36
|
+
delta: string;
|
|
37
|
+
} | {
|
|
38
|
+
kind: "reasoning_done";
|
|
39
|
+
itemId: string;
|
|
40
|
+
text: string;
|
|
41
|
+
} | {
|
|
42
|
+
kind: "tool_called";
|
|
43
|
+
itemId: string;
|
|
44
|
+
callId: string;
|
|
45
|
+
name: string;
|
|
46
|
+
input: unknown;
|
|
47
|
+
toolKind: "builtin" | "app";
|
|
48
|
+
} | {
|
|
49
|
+
kind: "tool_output";
|
|
50
|
+
itemId: string;
|
|
51
|
+
callId: string;
|
|
52
|
+
output: unknown;
|
|
53
|
+
isSandbox: boolean;
|
|
54
|
+
} | {
|
|
55
|
+
kind: "function_calls_requested";
|
|
56
|
+
requestId: string;
|
|
57
|
+
calls: FunctionCallRequest[];
|
|
58
|
+
itemIds: string[];
|
|
59
|
+
} | {
|
|
60
|
+
kind: "clarify_requested";
|
|
61
|
+
requestId: string;
|
|
62
|
+
questions: Record<string, unknown>[];
|
|
63
|
+
} | {
|
|
64
|
+
kind: "output_ready";
|
|
65
|
+
output: unknown;
|
|
66
|
+
skipped?: string;
|
|
67
|
+
mode?: string;
|
|
68
|
+
} | {
|
|
69
|
+
kind: "output_mismatch";
|
|
70
|
+
errors: string[];
|
|
71
|
+
rawText: string;
|
|
72
|
+
} | {
|
|
73
|
+
kind: "turn_limit_reached";
|
|
74
|
+
capKind: string;
|
|
75
|
+
limit?: string;
|
|
76
|
+
value?: number;
|
|
77
|
+
} | {
|
|
78
|
+
kind: "file_shared";
|
|
79
|
+
data: Record<string, unknown>;
|
|
80
|
+
} | {
|
|
81
|
+
kind: "stopped";
|
|
82
|
+
reason: string;
|
|
83
|
+
} | {
|
|
84
|
+
kind: "turn_failed";
|
|
85
|
+
code: string;
|
|
86
|
+
message: string;
|
|
87
|
+
} | {
|
|
88
|
+
kind: "turn_finished";
|
|
89
|
+
usage: Usage;
|
|
90
|
+
} | {
|
|
91
|
+
kind: "unknown";
|
|
92
|
+
raw: Record<string, unknown>;
|
|
93
|
+
};
|
|
94
|
+
export declare class TurnEvents {
|
|
95
|
+
responseId: string;
|
|
96
|
+
private partIndex;
|
|
97
|
+
private textItemId;
|
|
98
|
+
private textBuf;
|
|
99
|
+
private reasoningItemId;
|
|
100
|
+
private reasoningBuf;
|
|
101
|
+
private toolNames;
|
|
102
|
+
private toolItems;
|
|
103
|
+
constructor(responseId?: string);
|
|
104
|
+
private nextItemId;
|
|
105
|
+
private closeReasoning;
|
|
106
|
+
feed(chunk: unknown): TurnEvent[];
|
|
107
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TurnEvents = void 0;
|
|
4
|
+
/** Neutral turn event model built from Panja /v1/responses UI-message chunks. */
|
|
5
|
+
const events_js_1 = require("../../compose/events.js");
|
|
6
|
+
const ids_js_1 = require("./ids.js");
|
|
7
|
+
const SANDBOX_TOOLS = new Set(["bash", "code_execute", "run_program"]);
|
|
8
|
+
const BUILTIN_TOOLS = new Set([
|
|
9
|
+
"web_search", "web_fetch", "bash", "code_execute", "run_program", "read_file",
|
|
10
|
+
"write_file", "patch", "search_files", "todo", "cron", "clarify", "delegate_task",
|
|
11
|
+
"share_file", "list_artifacts", "skill_view",
|
|
12
|
+
]);
|
|
13
|
+
function isObj(v) {
|
|
14
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
15
|
+
}
|
|
16
|
+
function parseUsage(chunk) {
|
|
17
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
18
|
+
const meta = chunk["messageMetadata"];
|
|
19
|
+
const raw = isObj(meta) && isObj(meta["usage"]) ? meta["usage"] : null;
|
|
20
|
+
if (!raw)
|
|
21
|
+
return { inputTokens: 0, cachedTokens: 0, outputTokens: 0, reasoningTokens: 0, totalTokens: 0 };
|
|
22
|
+
const inputTokens = Number((_b = (_a = raw["inputTokens"]) !== null && _a !== void 0 ? _a : raw["input_tokens"]) !== null && _b !== void 0 ? _b : 0);
|
|
23
|
+
const outputTokens = Number((_d = (_c = raw["outputTokens"]) !== null && _c !== void 0 ? _c : raw["output_tokens"]) !== null && _d !== void 0 ? _d : 0);
|
|
24
|
+
const cachedTokens = Number((_f = (_e = raw["cacheReadTokens"]) !== null && _e !== void 0 ? _e : raw["cached_tokens"]) !== null && _f !== void 0 ? _f : 0);
|
|
25
|
+
const reasoningTokens = Number((_h = (_g = raw["reasoningTokens"]) !== null && _g !== void 0 ? _g : raw["reasoning_tokens"]) !== null && _h !== void 0 ? _h : 0);
|
|
26
|
+
const totalTokens = Number((_k = (_j = raw["totalTokens"]) !== null && _j !== void 0 ? _j : raw["total_tokens"]) !== null && _k !== void 0 ? _k : inputTokens + outputTokens);
|
|
27
|
+
return { inputTokens, cachedTokens, outputTokens, reasoningTokens, totalTokens };
|
|
28
|
+
}
|
|
29
|
+
class TurnEvents {
|
|
30
|
+
constructor(responseId = "") {
|
|
31
|
+
this.partIndex = 0;
|
|
32
|
+
this.textItemId = null;
|
|
33
|
+
this.textBuf = [];
|
|
34
|
+
this.reasoningItemId = null;
|
|
35
|
+
this.reasoningBuf = [];
|
|
36
|
+
this.toolNames = new Map();
|
|
37
|
+
this.toolItems = new Map();
|
|
38
|
+
this.responseId = responseId;
|
|
39
|
+
}
|
|
40
|
+
nextItemId() {
|
|
41
|
+
const idx = this.partIndex++;
|
|
42
|
+
return (0, ids_js_1.itemId)(this.responseId || "turn", idx);
|
|
43
|
+
}
|
|
44
|
+
closeReasoning(out) {
|
|
45
|
+
if (this.reasoningItemId !== null) {
|
|
46
|
+
out.push({ kind: "reasoning_done", itemId: this.reasoningItemId, text: this.reasoningBuf.join("") });
|
|
47
|
+
this.reasoningItemId = null;
|
|
48
|
+
this.reasoningBuf = [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
feed(chunk) {
|
|
52
|
+
var _a, _b, _c;
|
|
53
|
+
const out = [];
|
|
54
|
+
if (!isObj(chunk)) {
|
|
55
|
+
this.closeReasoning(out);
|
|
56
|
+
out.push({ kind: "unknown", raw: { value: chunk } });
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
const type = chunk["type"];
|
|
60
|
+
if (type === "start") {
|
|
61
|
+
const msgId = typeof chunk["messageId"] === "string" && chunk["messageId"] ? chunk["messageId"] : this.responseId;
|
|
62
|
+
if (msgId)
|
|
63
|
+
this.responseId = msgId;
|
|
64
|
+
this.partIndex = 0;
|
|
65
|
+
out.push({ kind: "turn_started", responseId: this.responseId });
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
if (type === "data-file-shared") {
|
|
69
|
+
this.closeReasoning(out);
|
|
70
|
+
const data = isObj(chunk["data"]) ? { ...chunk["data"] } : {};
|
|
71
|
+
out.push({ kind: "file_shared", data });
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
const ev = (0, events_js_1.fromUiChunk)(chunk);
|
|
75
|
+
if (ev === null)
|
|
76
|
+
return out;
|
|
77
|
+
if (ev.kind === "reasoning_delta") {
|
|
78
|
+
if (this.reasoningItemId === null) {
|
|
79
|
+
this.reasoningItemId = this.nextItemId();
|
|
80
|
+
out.push({ kind: "reasoning_started", itemId: this.reasoningItemId });
|
|
81
|
+
}
|
|
82
|
+
this.reasoningBuf.push(ev.delta);
|
|
83
|
+
out.push({ kind: "reasoning_delta", itemId: this.reasoningItemId, delta: ev.delta });
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
this.closeReasoning(out);
|
|
87
|
+
switch (ev.kind) {
|
|
88
|
+
case "text_start": {
|
|
89
|
+
const iid = this.nextItemId();
|
|
90
|
+
this.textItemId = iid;
|
|
91
|
+
this.textBuf = [];
|
|
92
|
+
out.push({ kind: "text_started", itemId: iid });
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case "text_delta": {
|
|
96
|
+
if (this.textItemId === null) {
|
|
97
|
+
this.textItemId = this.nextItemId();
|
|
98
|
+
this.textBuf = [];
|
|
99
|
+
out.push({ kind: "text_started", itemId: this.textItemId });
|
|
100
|
+
}
|
|
101
|
+
this.textBuf.push(ev.delta);
|
|
102
|
+
out.push({ kind: "text_delta", itemId: this.textItemId, delta: ev.delta });
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "text_end": {
|
|
106
|
+
const iid = (_a = this.textItemId) !== null && _a !== void 0 ? _a : this.nextItemId();
|
|
107
|
+
const text = this.textBuf.join("");
|
|
108
|
+
this.textItemId = null;
|
|
109
|
+
this.textBuf = [];
|
|
110
|
+
out.push({ kind: "text_done", itemId: iid, text });
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "tool_called": {
|
|
114
|
+
const iid = this.nextItemId();
|
|
115
|
+
this.toolNames.set(ev.callId, ev.name);
|
|
116
|
+
this.toolItems.set(ev.callId, iid);
|
|
117
|
+
const toolKind = BUILTIN_TOOLS.has(ev.name) ? "builtin" : "app";
|
|
118
|
+
out.push({ kind: "tool_called", itemId: iid, callId: ev.callId, name: ev.name, input: ev.arguments, toolKind });
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
case "tool_output": {
|
|
122
|
+
const iid = (_b = this.toolItems.get(ev.callId)) !== null && _b !== void 0 ? _b : this.nextItemId();
|
|
123
|
+
const isSandbox = SANDBOX_TOOLS.has((_c = this.toolNames.get(ev.callId)) !== null && _c !== void 0 ? _c : "");
|
|
124
|
+
out.push({ kind: "tool_output", itemId: iid, callId: ev.callId, output: ev.output, isSandbox });
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case "function_calls_requested": {
|
|
128
|
+
const itemIds = ev.calls.map(() => this.nextItemId());
|
|
129
|
+
out.push({ kind: "function_calls_requested", requestId: ev.requestId, calls: [...ev.calls], itemIds });
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case "clarify_requested":
|
|
133
|
+
out.push({ kind: "clarify_requested", requestId: ev.requestId, questions: ev.questions });
|
|
134
|
+
break;
|
|
135
|
+
case "output":
|
|
136
|
+
out.push({ kind: "output_ready", output: ev.output, skipped: ev.skipped, mode: ev.mode });
|
|
137
|
+
break;
|
|
138
|
+
case "output_error":
|
|
139
|
+
out.push({ kind: "output_mismatch", errors: [...ev.errors], rawText: ev.rawText });
|
|
140
|
+
break;
|
|
141
|
+
case "turn_limit":
|
|
142
|
+
out.push({ kind: "turn_limit_reached", capKind: ev.capKind, limit: ev.limit, value: ev.value });
|
|
143
|
+
break;
|
|
144
|
+
case "stopped":
|
|
145
|
+
out.push({ kind: "stopped", reason: ev.reason });
|
|
146
|
+
break;
|
|
147
|
+
case "turn_failed":
|
|
148
|
+
out.push({ kind: "turn_failed", code: ev.code, message: ev.message });
|
|
149
|
+
break;
|
|
150
|
+
case "turn_finished":
|
|
151
|
+
out.push({ kind: "turn_finished", usage: parseUsage(chunk) });
|
|
152
|
+
break;
|
|
153
|
+
case "unknown":
|
|
154
|
+
out.push({ kind: "unknown", raw: isObj(ev.chunk) ? ev.chunk : { value: ev.chunk } });
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.TurnEvents = TurnEvents;
|
|
@@ -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;
|