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,9 @@
|
|
|
1
|
+
import { type TurnEvent } from "./events.js";
|
|
2
|
+
import { Http } from "./http.js";
|
|
3
|
+
export interface TurnRecord {
|
|
4
|
+
responseId: string;
|
|
5
|
+
role: string;
|
|
6
|
+
events: TurnEvent[];
|
|
7
|
+
createdAt: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function history(http: Http, sessionId: string): Promise<TurnRecord[]>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.history = history;
|
|
4
|
+
/** Rebuild neutral turn history from stored Panja conversation messages with stable IDs. */
|
|
5
|
+
const ConversationsApi_js_1 = require("../../apis/ConversationsApi.js");
|
|
6
|
+
const MessagesApi_js_1 = require("../../apis/MessagesApi.js");
|
|
7
|
+
const events_js_1 = require("../../compose/events.js");
|
|
8
|
+
const events_js_2 = require("./events.js");
|
|
9
|
+
const ids_js_1 = require("./ids.js");
|
|
10
|
+
function userTextAndAttachments(parts) {
|
|
11
|
+
var _a, _b, _c, _d;
|
|
12
|
+
if (!Array.isArray(parts))
|
|
13
|
+
return { text: "", attachments: [] };
|
|
14
|
+
let out = "";
|
|
15
|
+
const attachments = [];
|
|
16
|
+
for (const p of parts) {
|
|
17
|
+
if (!p || typeof p !== "object")
|
|
18
|
+
continue;
|
|
19
|
+
const obj = p;
|
|
20
|
+
const ptype = obj["type"];
|
|
21
|
+
if (ptype === "text") {
|
|
22
|
+
out += String((_a = obj["text"]) !== null && _a !== void 0 ? _a : "");
|
|
23
|
+
}
|
|
24
|
+
else if (ptype === "file") {
|
|
25
|
+
const meta = ((_b = obj["providerMetadata"]) !== null && _b !== void 0 ? _b : obj["provider_metadata"]);
|
|
26
|
+
const custom = meta && typeof meta === "object" ? meta["custom"] : undefined;
|
|
27
|
+
let aid = custom && typeof custom === "object" && typeof custom["artifactId"] === "string"
|
|
28
|
+
? custom["artifactId"]
|
|
29
|
+
: "";
|
|
30
|
+
if (!aid && typeof obj["url"] === "string" && obj["url"].startsWith("artifact://")) {
|
|
31
|
+
aid = obj["url"].slice("artifact://".length);
|
|
32
|
+
}
|
|
33
|
+
if (aid) {
|
|
34
|
+
const fname = String((custom && typeof custom === "object" && custom["filename"]) || obj["filename"] || aid);
|
|
35
|
+
const mtype = String((_d = (_c = obj["mediaType"]) !== null && _c !== void 0 ? _c : obj["media_type"]) !== null && _d !== void 0 ? _d : "application/octet-stream");
|
|
36
|
+
attachments.push({ fileId: aid, filename: fname, mediaType: mtype });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { text: out, attachments };
|
|
41
|
+
}
|
|
42
|
+
function msgTimestamp(msg, fallback) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
let raw = (_a = msg["createdAt"]) !== null && _a !== void 0 ? _a : msg["created_at"];
|
|
45
|
+
if (raw === undefined && msg["metadata"] && typeof msg["metadata"] === "object") {
|
|
46
|
+
const m = msg["metadata"];
|
|
47
|
+
raw = (_b = m["createdAt"]) !== null && _b !== void 0 ? _b : m["created_at"];
|
|
48
|
+
}
|
|
49
|
+
if (raw instanceof Date)
|
|
50
|
+
return raw.toISOString();
|
|
51
|
+
if (typeof raw === "string" && raw)
|
|
52
|
+
return raw;
|
|
53
|
+
return fallback;
|
|
54
|
+
}
|
|
55
|
+
async function history(http, sessionId) {
|
|
56
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
57
|
+
const convApi = new ConversationsApi_js_1.ConversationsApi(http.sdkConfig);
|
|
58
|
+
const msgApi = new MessagesApi_js_1.MessagesApi(http.sdkConfig);
|
|
59
|
+
const conv = await http.call(() => convApi.getConversation({ conversationId: sessionId }), { idempotent: true });
|
|
60
|
+
const ts = conv.createdAt instanceof Date
|
|
61
|
+
? conv.createdAt.toISOString()
|
|
62
|
+
: String((_a = conv.createdAt) !== null && _a !== void 0 ? _a : "1970-01-01T00:00:00Z");
|
|
63
|
+
let allMessages = [...((_b = conv.messages) !== null && _b !== void 0 ? _b : [])];
|
|
64
|
+
const seenCursors = new Set();
|
|
65
|
+
let hasMore = Boolean(conv.hasMore);
|
|
66
|
+
let before = (_c = conv.nextBeforeCursor) !== null && _c !== void 0 ? _c : null;
|
|
67
|
+
while (hasMore && before && !seenCursors.has(before)) {
|
|
68
|
+
const curBefore = before;
|
|
69
|
+
seenCursors.add(curBefore);
|
|
70
|
+
const older = await http.call(() => msgApi.listMessages({ conversationId: sessionId, before: curBefore }), { idempotent: true });
|
|
71
|
+
const olderMsgs = (_d = older.data) !== null && _d !== void 0 ? _d : [];
|
|
72
|
+
if (olderMsgs.length === 0)
|
|
73
|
+
break;
|
|
74
|
+
allMessages = [...olderMsgs, ...allMessages];
|
|
75
|
+
hasMore = Boolean(older.hasMore);
|
|
76
|
+
before = (_e = older.nextBeforeCursor) !== null && _e !== void 0 ? _e : null;
|
|
77
|
+
}
|
|
78
|
+
const records = [];
|
|
79
|
+
const seenIds = new Set();
|
|
80
|
+
for (const rawMsg of allMessages) {
|
|
81
|
+
const msg = rawMsg;
|
|
82
|
+
const msgId = String((_f = msg["id"]) !== null && _f !== void 0 ? _f : "");
|
|
83
|
+
if (msgId && seenIds.has(msgId))
|
|
84
|
+
continue;
|
|
85
|
+
if (msgId)
|
|
86
|
+
seenIds.add(msgId);
|
|
87
|
+
const role = String((_g = msg["role"]) !== null && _g !== void 0 ? _g : "assistant");
|
|
88
|
+
const msgTs = msgTimestamp(msg, ts);
|
|
89
|
+
if (role === "user") {
|
|
90
|
+
const { text, attachments } = userTextAndAttachments(msg["parts"]);
|
|
91
|
+
records.push({
|
|
92
|
+
responseId: msgId,
|
|
93
|
+
role: "user",
|
|
94
|
+
events: [
|
|
95
|
+
attachments.length > 0
|
|
96
|
+
? { kind: "text_done", itemId: (0, ids_js_1.itemId)(msgId, 0), text, attachments }
|
|
97
|
+
: { kind: "text_done", itemId: (0, ids_js_1.itemId)(msgId, 0), text },
|
|
98
|
+
],
|
|
99
|
+
createdAt: msgTs,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const machine = new events_js_2.TurnEvents(msgId);
|
|
104
|
+
const events = [];
|
|
105
|
+
for (const chunk of (0, events_js_1.chunksFromUiMessage)(msg)) {
|
|
106
|
+
events.push(...machine.feed(chunk));
|
|
107
|
+
}
|
|
108
|
+
records.push({ responseId: msgId, role, events, createdAt: msgTs });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return records;
|
|
112
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as httpMod from "node:http";
|
|
2
|
+
import * as httpsMod from "node:https";
|
|
3
|
+
import { Configuration, type FetchAPI } from "../../runtime.js";
|
|
4
|
+
export declare const DEFAULT_BASE_URL = "https://chat.sarvamclaw.sarvam.ai";
|
|
5
|
+
export declare const DEFAULT_TIMEOUT_MS = 600000;
|
|
6
|
+
export declare const DEFAULT_MAX_RETRIES = 2;
|
|
7
|
+
export interface ClientConfigInput {
|
|
8
|
+
apiKey?: string | null;
|
|
9
|
+
baseURL?: string | null;
|
|
10
|
+
timeout?: number | null;
|
|
11
|
+
maxRetries?: number;
|
|
12
|
+
defaultHeaders?: Record<string, string>;
|
|
13
|
+
defaultQuery?: Record<string, string>;
|
|
14
|
+
fetch?: FetchAPI;
|
|
15
|
+
provider?: string;
|
|
16
|
+
allowPrivateRemoteUrls?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface ResolvedClientConfig {
|
|
19
|
+
apiKey: string;
|
|
20
|
+
baseURL: string;
|
|
21
|
+
timeout: number;
|
|
22
|
+
maxRetries: number;
|
|
23
|
+
defaultHeaders: Record<string, string>;
|
|
24
|
+
defaultQuery: Record<string, string>;
|
|
25
|
+
fetch?: FetchAPI;
|
|
26
|
+
provider: string;
|
|
27
|
+
allowPrivateRemoteUrls: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface PerCallRequestOptions {
|
|
30
|
+
timeout?: number;
|
|
31
|
+
maxRetries?: number;
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
query?: Record<string, string>;
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
idempotencyKey?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface RawResponseSnapshot {
|
|
38
|
+
status: number;
|
|
39
|
+
headers: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
export declare function resolveClientConfig(input?: ClientConfigInput): ResolvedClientConfig;
|
|
42
|
+
export interface PinnedNodeTransport {
|
|
43
|
+
httpRequest?: typeof httpMod.request;
|
|
44
|
+
httpsRequest?: typeof httpsMod.request;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a Node `net.LookupFunction` that always resolves directly to ``pinnedIp`` without
|
|
48
|
+
* issuing a second DNS lookup, supporting both `{ all: true }` (Node 20+ autoSelectFamily)
|
|
49
|
+
* and single-address callback forms.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createPinnedLookup(pinnedIp: string): (_hostname: string, options: number | {
|
|
52
|
+
all?: boolean;
|
|
53
|
+
} | ((err: NodeJS.ErrnoException | null, address: string | Array<{
|
|
54
|
+
address: string;
|
|
55
|
+
family: number;
|
|
56
|
+
}>, family?: number) => void), callback?: (err: NodeJS.ErrnoException | null, address: string | Array<{
|
|
57
|
+
address: string;
|
|
58
|
+
family: number;
|
|
59
|
+
}>, family?: number) => void) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Issue an HTTP/HTTPS request pinned to ``pinnedIp`` via Node's stdlib ``node:http`` / ``node:https``
|
|
62
|
+
* while preserving the original ``Host`` header, TLS SNI (``servername``), and certificate hostname
|
|
63
|
+
* verification (closing the DNS rebinding TOCTOU gap).
|
|
64
|
+
*/
|
|
65
|
+
export declare function pinnedNodeFetch(url: string, init: RequestInit, pinnedIp: string, signal?: AbortSignal, transport?: PinnedNodeTransport): Promise<Response>;
|
|
66
|
+
export declare class Http {
|
|
67
|
+
readonly config: ResolvedClientConfig;
|
|
68
|
+
readonly sdkConfig: Configuration;
|
|
69
|
+
private readonly baseFetch;
|
|
70
|
+
private readonly sleepFn;
|
|
71
|
+
private readonly jitterFn;
|
|
72
|
+
private readonly resolveHostFn?;
|
|
73
|
+
private readonly reqStore;
|
|
74
|
+
constructor(input: ClientConfigInput, opts?: {
|
|
75
|
+
sleep?: (ms: number) => Promise<void>;
|
|
76
|
+
jitter?: (ms: number) => number;
|
|
77
|
+
resolveHost?: (host: string) => Promise<string[]>;
|
|
78
|
+
});
|
|
79
|
+
newIdempotencyKey(): string;
|
|
80
|
+
resolveHost(host: string): Promise<string[]>;
|
|
81
|
+
effectiveRemoteTimeoutMs(maxMs: number): number;
|
|
82
|
+
/**
|
|
83
|
+
* Issue a raw HTTP request WITHOUT Sarvam Authorization, X-Panja-Origin, defaultHeaders, defaultQuery,
|
|
84
|
+
* or Idempotency-Key. Honors per-call AbortSignal and timeout (optionally capped by maxTimeoutMs or
|
|
85
|
+
* pinned by timeoutOverrideMs), and returns the raw unbuffered Response plus a cleanup handle and
|
|
86
|
+
* deadline race promise so callers can stream and cap response bodies safely.
|
|
87
|
+
*
|
|
88
|
+
* When ``opts.pinnedIp`` is supplied and no custom ``fetch`` override was provided, connects
|
|
89
|
+
* directly to ``opts.pinnedIp`` via ``node:http``/``node:https`` with original ``Host`` and TLS
|
|
90
|
+
* ``servername`` to eliminate the DNS rebinding TOCTOU window.
|
|
91
|
+
*/
|
|
92
|
+
externalFetch(url: string, init?: RequestInit, opts?: {
|
|
93
|
+
maxTimeoutMs?: number;
|
|
94
|
+
timeoutOverrideMs?: number;
|
|
95
|
+
pinnedIp?: string | null;
|
|
96
|
+
}): Promise<{
|
|
97
|
+
response: Response;
|
|
98
|
+
racePromise: Promise<never> | null;
|
|
99
|
+
cleanup: () => void;
|
|
100
|
+
}>;
|
|
101
|
+
withRequestOptions<T>(opts: PerCallRequestOptions | undefined, fn: () => Promise<T>): Promise<T>;
|
|
102
|
+
captureRawResponse<T>(fn: () => Promise<T>): Promise<{
|
|
103
|
+
data: T;
|
|
104
|
+
raw: RawResponseSnapshot | null;
|
|
105
|
+
}>;
|
|
106
|
+
call<T>(fn: () => Promise<T>, opts?: {
|
|
107
|
+
idempotent?: boolean;
|
|
108
|
+
maxRetries?: number;
|
|
109
|
+
idempotencyKey?: string;
|
|
110
|
+
}): Promise<T>;
|
|
111
|
+
private toCoreError;
|
|
112
|
+
}
|