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,177 @@
|
|
|
1
|
+
/** OpenAI drop-in client (client.beta.agents...) backed by compat/core. */
|
|
2
|
+
import { Agents } from "../core/agents.js";
|
|
3
|
+
import { CoreAPIError } from "../core/errors.js";
|
|
4
|
+
import { AttachmentUploadCache } from "../core/files.js";
|
|
5
|
+
import { DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT_MS, Http } from "../core/http.js";
|
|
6
|
+
import { ModelResolver } from "../core/models.js";
|
|
7
|
+
import { Sessions, SessionUpdateQueue } from "../core/sessions.js";
|
|
8
|
+
import { ToolResults, ToolResultStore } from "../core/toolResults.js";
|
|
9
|
+
import { ListenerRegistry, Turns } from "../core/turns.js";
|
|
10
|
+
import { toOpenAIError } from "./errors.js";
|
|
11
|
+
import { AgentsResource } from "./resources/agents.js";
|
|
12
|
+
import { EventsResource, ItemsResource } from "./resources/events.js";
|
|
13
|
+
import { SessionsResource } from "./resources/sessions.js";
|
|
14
|
+
import type { AgentSessionEventStream } from "./stream.js";
|
|
15
|
+
import type { AgentSession, RawResponse, RequestOptions } from "./types.js";
|
|
16
|
+
|
|
17
|
+
export interface OpenAIOptions {
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
baseURL?: string;
|
|
20
|
+
timeout?: number;
|
|
21
|
+
maxRetries?: number;
|
|
22
|
+
defaultHeaders?: Record<string, string>;
|
|
23
|
+
defaultQuery?: Record<string, string>;
|
|
24
|
+
modelMap?: Record<string, string>;
|
|
25
|
+
strictModels?: boolean;
|
|
26
|
+
allowPrivateRemoteUrls?: boolean;
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
28
|
+
_sleep?: (ms: number) => Promise<void>;
|
|
29
|
+
_pollIntervalMs?: number;
|
|
30
|
+
_resolveHost?: (host: string) => Promise<string[]>;
|
|
31
|
+
_sharedRegistry?: ListenerRegistry;
|
|
32
|
+
_sharedToolStore?: ToolResultStore;
|
|
33
|
+
_sharedSessionQueue?: SessionUpdateQueue;
|
|
34
|
+
_sharedUploadCache?: AttachmentUploadCache;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type RawResponseProxy<T> = {
|
|
38
|
+
readonly [K in keyof T]: T[K] extends (...args: infer A) => Promise<infer R>
|
|
39
|
+
? (...args: A) => Promise<RawResponse<R>>
|
|
40
|
+
: T[K] extends (...args: infer A) => infer R
|
|
41
|
+
? (...args: A) => Promise<RawResponse<R>>
|
|
42
|
+
: T[K] extends object
|
|
43
|
+
? RawResponseProxy<T[K]>
|
|
44
|
+
: T[K];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export interface RawResponseSessionsResource extends Omit<RawResponseProxy<SessionsResource>, "create"> {
|
|
48
|
+
create(
|
|
49
|
+
params: Record<string, unknown> & { stream: true },
|
|
50
|
+
options?: RequestOptions,
|
|
51
|
+
): Promise<RawResponse<AgentSessionEventStream>>;
|
|
52
|
+
create(
|
|
53
|
+
params: Record<string, unknown> & { stream?: false },
|
|
54
|
+
options?: RequestOptions,
|
|
55
|
+
): Promise<RawResponse<AgentSession>>;
|
|
56
|
+
create(
|
|
57
|
+
params?: Record<string, unknown>,
|
|
58
|
+
options?: RequestOptions,
|
|
59
|
+
): Promise<RawResponse<AgentSession | AgentSessionEventStream>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface RawResponseAgentsResource extends Omit<RawResponseProxy<AgentsResource>, "sessions"> {
|
|
63
|
+
readonly sessions: RawResponseSessionsResource;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface RawResponseClient {
|
|
67
|
+
readonly beta: {
|
|
68
|
+
readonly agents: RawResponseAgentsResource;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function makeRawProxy(target: object, http: Http): unknown {
|
|
73
|
+
return new Proxy(target, {
|
|
74
|
+
get(obj, prop, receiver) {
|
|
75
|
+
const val = Reflect.get(obj, prop, receiver);
|
|
76
|
+
if (typeof val === "function") {
|
|
77
|
+
return async (...args: unknown[]): Promise<RawResponse<unknown>> => {
|
|
78
|
+
const { data, raw } = await http.captureRawResponse(() =>
|
|
79
|
+
(val as (...a: unknown[]) => Promise<unknown>).apply(obj, args),
|
|
80
|
+
);
|
|
81
|
+
return {
|
|
82
|
+
status: raw?.status ?? 200,
|
|
83
|
+
headers: raw?.headers ?? {},
|
|
84
|
+
data,
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (val !== null && typeof val === "object") {
|
|
89
|
+
return makeRawProxy(val, http);
|
|
90
|
+
}
|
|
91
|
+
return val;
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class OpenAI {
|
|
97
|
+
readonly beta: { readonly agents: AgentsResource };
|
|
98
|
+
private readonly initOpts: OpenAIOptions;
|
|
99
|
+
private readonly http: Http;
|
|
100
|
+
private readonly listenerRegistry: ListenerRegistry;
|
|
101
|
+
private readonly toolResultStore: ToolResultStore;
|
|
102
|
+
private readonly sessionUpdateQueue: SessionUpdateQueue;
|
|
103
|
+
private readonly uploadCache: AttachmentUploadCache;
|
|
104
|
+
|
|
105
|
+
constructor(opts: OpenAIOptions = {}) {
|
|
106
|
+
this.initOpts = { ...opts };
|
|
107
|
+
try {
|
|
108
|
+
this.http = new Http(
|
|
109
|
+
{
|
|
110
|
+
apiKey: opts.apiKey,
|
|
111
|
+
baseURL: opts.baseURL,
|
|
112
|
+
timeout: opts.timeout ?? DEFAULT_TIMEOUT_MS,
|
|
113
|
+
maxRetries: opts.maxRetries ?? DEFAULT_MAX_RETRIES,
|
|
114
|
+
defaultHeaders: opts.defaultHeaders,
|
|
115
|
+
defaultQuery: opts.defaultQuery,
|
|
116
|
+
provider: "openai",
|
|
117
|
+
fetch: opts.fetch,
|
|
118
|
+
allowPrivateRemoteUrls: opts.allowPrivateRemoteUrls,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
...(opts._sleep ? { sleep: opts._sleep } : {}),
|
|
122
|
+
...(opts._resolveHost ? { resolveHost: opts._resolveHost } : {}),
|
|
123
|
+
},
|
|
124
|
+
);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
if (err instanceof CoreAPIError) throw toOpenAIError(err);
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const models = new ModelResolver(this.http, {
|
|
131
|
+
modelMap: opts.modelMap,
|
|
132
|
+
strict: opts.strictModels,
|
|
133
|
+
provider: "openai",
|
|
134
|
+
});
|
|
135
|
+
const agentsCore = new Agents(this.http, models);
|
|
136
|
+
this.sessionUpdateQueue = opts._sharedSessionQueue ?? new SessionUpdateQueue();
|
|
137
|
+
const sessionsCore = new Sessions(this.http, { provider: "openai", updateQueue: this.sessionUpdateQueue });
|
|
138
|
+
this.listenerRegistry = opts._sharedRegistry ?? new ListenerRegistry();
|
|
139
|
+
this.toolResultStore = opts._sharedToolStore ?? new ToolResultStore();
|
|
140
|
+
this.uploadCache = opts._sharedUploadCache ?? new AttachmentUploadCache();
|
|
141
|
+
const turnsCore = new Turns(this.http, sessionsCore, {
|
|
142
|
+
pollIntervalMs: opts._pollIntervalMs,
|
|
143
|
+
provider: "openai",
|
|
144
|
+
registry: this.listenerRegistry,
|
|
145
|
+
uploadCache: this.uploadCache,
|
|
146
|
+
});
|
|
147
|
+
const toolResultsCore = new ToolResults(this.http, sessionsCore, turnsCore, {
|
|
148
|
+
store: this.toolResultStore,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const eventsRes = new EventsResource(this.http, sessionsCore, turnsCore, toolResultsCore);
|
|
152
|
+
const itemsRes = new ItemsResource(this.http);
|
|
153
|
+
const sessionsRes = new SessionsResource(this.http, agentsCore, sessionsCore, turnsCore, eventsRes, itemsRes);
|
|
154
|
+
const agentsRes = new AgentsResource(this.http, agentsCore, sessionsRes);
|
|
155
|
+
this.beta = { agents: agentsRes };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get withRawResponse(): RawResponseClient {
|
|
159
|
+
return makeRawProxy(this, this.http) as RawResponseClient;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
withOptions(overrides: Partial<OpenAIOptions>): OpenAI {
|
|
163
|
+
// Share only listener registry and tool-result buffers so each options view uses its own Http credentials & config.
|
|
164
|
+
return new OpenAI({
|
|
165
|
+
...this.initOpts,
|
|
166
|
+
...overrides,
|
|
167
|
+
_sharedRegistry: this.listenerRegistry,
|
|
168
|
+
_sharedToolStore: this.toolResultStore,
|
|
169
|
+
_sharedSessionQueue: this.sessionUpdateQueue,
|
|
170
|
+
_sharedUploadCache: this.uploadCache,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
close(): void {
|
|
175
|
+
// No persistent sockets to tear down in fetch-based client; provided for Python parity.
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/** OpenAI-shaped error hierarchy backed by compat/core CoreAPIError (spec §6). */
|
|
2
|
+
import {
|
|
3
|
+
CoreAPIError,
|
|
4
|
+
CoreConnectionError,
|
|
5
|
+
CoreTimeoutError,
|
|
6
|
+
CoreUserAbortError,
|
|
7
|
+
Unsupported,
|
|
8
|
+
} from "../core/errors.js";
|
|
9
|
+
|
|
10
|
+
export interface ErrorResponseSnapshot {
|
|
11
|
+
status: number;
|
|
12
|
+
headers: Record<string, string>;
|
|
13
|
+
text: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class OpenAIError extends Error {
|
|
17
|
+
constructor(message: string) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = new.target.name;
|
|
20
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class APIError extends OpenAIError {
|
|
25
|
+
readonly status?: number;
|
|
26
|
+
readonly code?: string;
|
|
27
|
+
readonly param?: string;
|
|
28
|
+
readonly type?: string;
|
|
29
|
+
readonly requestID?: string;
|
|
30
|
+
readonly request_id?: string;
|
|
31
|
+
readonly body?: unknown;
|
|
32
|
+
readonly headers: Record<string, string>;
|
|
33
|
+
readonly response?: ErrorResponseSnapshot;
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
message: string,
|
|
37
|
+
opts: {
|
|
38
|
+
status?: number;
|
|
39
|
+
code?: string;
|
|
40
|
+
param?: string;
|
|
41
|
+
type?: string;
|
|
42
|
+
requestId?: string;
|
|
43
|
+
body?: unknown;
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
response?: ErrorResponseSnapshot;
|
|
46
|
+
} = {},
|
|
47
|
+
) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.status = opts.status;
|
|
50
|
+
this.code = opts.code;
|
|
51
|
+
this.param = opts.param;
|
|
52
|
+
this.type = opts.type;
|
|
53
|
+
this.requestID = opts.requestId;
|
|
54
|
+
this.request_id = opts.requestId;
|
|
55
|
+
this.body = opts.body;
|
|
56
|
+
this.headers = { ...(opts.headers ?? {}) };
|
|
57
|
+
if (opts.response) {
|
|
58
|
+
this.response = opts.response;
|
|
59
|
+
} else if (opts.status !== undefined) {
|
|
60
|
+
const text = typeof opts.body === "string"
|
|
61
|
+
? opts.body
|
|
62
|
+
: opts.body !== undefined && opts.body !== null
|
|
63
|
+
? JSON.stringify(opts.body)
|
|
64
|
+
: "";
|
|
65
|
+
this.response = { status: opts.status, headers: { ...this.headers }, text };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class APIConnectionError extends APIError {}
|
|
71
|
+
export class APITimeoutError extends APIConnectionError {}
|
|
72
|
+
export class APIUserAbortError extends APIError {}
|
|
73
|
+
export class APIStatusError extends APIError {}
|
|
74
|
+
export class BadRequestError extends APIStatusError {}
|
|
75
|
+
export class UnsupportedFeatureError extends BadRequestError {
|
|
76
|
+
readonly feature?: string;
|
|
77
|
+
readonly needs?: string;
|
|
78
|
+
|
|
79
|
+
constructor(
|
|
80
|
+
message: string,
|
|
81
|
+
opts: {
|
|
82
|
+
status?: number;
|
|
83
|
+
code?: string;
|
|
84
|
+
param?: string;
|
|
85
|
+
type?: string;
|
|
86
|
+
requestId?: string;
|
|
87
|
+
body?: unknown;
|
|
88
|
+
headers?: Record<string, string>;
|
|
89
|
+
feature?: string;
|
|
90
|
+
needs?: string;
|
|
91
|
+
} = {},
|
|
92
|
+
) {
|
|
93
|
+
super(message, {
|
|
94
|
+
status: 400,
|
|
95
|
+
code: "unsupported_by_sarvam",
|
|
96
|
+
type: "invalid_request_error",
|
|
97
|
+
...opts,
|
|
98
|
+
});
|
|
99
|
+
this.feature = opts.feature;
|
|
100
|
+
this.needs = opts.needs;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export class AuthenticationError extends APIStatusError {}
|
|
104
|
+
export class PermissionDeniedError extends APIStatusError {}
|
|
105
|
+
export class NotFoundError extends APIStatusError {}
|
|
106
|
+
export class ConflictError extends APIStatusError {}
|
|
107
|
+
export class UnprocessableEntityError extends APIStatusError {}
|
|
108
|
+
export class RateLimitError extends APIStatusError {}
|
|
109
|
+
export class InternalServerError extends APIStatusError {}
|
|
110
|
+
|
|
111
|
+
export function toOpenAIError(err: CoreAPIError): APIError {
|
|
112
|
+
const opts = {
|
|
113
|
+
status: err.status ?? undefined,
|
|
114
|
+
code: err.code ?? undefined,
|
|
115
|
+
param: err.param ?? undefined,
|
|
116
|
+
type: err instanceof Unsupported ? "invalid_request_error" : undefined,
|
|
117
|
+
requestId: err.requestId ?? undefined,
|
|
118
|
+
body: err.body,
|
|
119
|
+
headers: err.headers,
|
|
120
|
+
};
|
|
121
|
+
if (err instanceof CoreUserAbortError) return new APIUserAbortError(err.message, opts);
|
|
122
|
+
if (err instanceof CoreTimeoutError) return new APITimeoutError(err.message, opts);
|
|
123
|
+
if (err instanceof CoreConnectionError) return new APIConnectionError(err.message, opts);
|
|
124
|
+
if (err instanceof Unsupported) {
|
|
125
|
+
return new UnsupportedFeatureError(err.message, {
|
|
126
|
+
...opts,
|
|
127
|
+
status: err.status ?? 400,
|
|
128
|
+
feature: err.feature,
|
|
129
|
+
needs: err.needs ?? undefined,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (err.status === null) {
|
|
133
|
+
return new APIError(err.message, opts);
|
|
134
|
+
}
|
|
135
|
+
// OpenAI maps Panja 413/422 validation/size errors to 400 BadRequestError per shared errors.json.
|
|
136
|
+
const status = err.status === 413 || err.status === 422 ? 400 : err.status;
|
|
137
|
+
switch (status) {
|
|
138
|
+
case 400:
|
|
139
|
+
return new BadRequestError(err.message, { ...opts, status: 400 });
|
|
140
|
+
case 401:
|
|
141
|
+
return new AuthenticationError(err.message, { ...opts, status: 401 });
|
|
142
|
+
case 403:
|
|
143
|
+
return new PermissionDeniedError(err.message, { ...opts, status: 403 });
|
|
144
|
+
case 404:
|
|
145
|
+
return new NotFoundError(err.message, { ...opts, status: 404 });
|
|
146
|
+
case 409:
|
|
147
|
+
return new ConflictError(err.message, { ...opts, status: 409 });
|
|
148
|
+
case 429:
|
|
149
|
+
return new RateLimitError(err.message, { ...opts, status: 429 });
|
|
150
|
+
default:
|
|
151
|
+
if (status >= 500) {
|
|
152
|
+
return new InternalServerError(err.message, { ...opts, status });
|
|
153
|
+
}
|
|
154
|
+
return new APIStatusError(err.message, { ...opts, status });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function wrapCoreErrors<T>(fn: () => Promise<T>): Promise<T> {
|
|
159
|
+
try {
|
|
160
|
+
return await fn();
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if (err instanceof CoreAPIError) throw toOpenAIError(err);
|
|
163
|
+
throw err;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** OpenAI Hosted Agents API drop-in compatibility module. */
|
|
2
|
+
export { SarvamCompatWarning } from "../core/warnings.js";
|
|
3
|
+
export {
|
|
4
|
+
OpenAIError,
|
|
5
|
+
APIError,
|
|
6
|
+
APIConnectionError,
|
|
7
|
+
APITimeoutError,
|
|
8
|
+
APIUserAbortError,
|
|
9
|
+
APIStatusError,
|
|
10
|
+
BadRequestError,
|
|
11
|
+
UnsupportedFeatureError,
|
|
12
|
+
AuthenticationError,
|
|
13
|
+
PermissionDeniedError,
|
|
14
|
+
NotFoundError,
|
|
15
|
+
ConflictError,
|
|
16
|
+
UnprocessableEntityError,
|
|
17
|
+
RateLimitError,
|
|
18
|
+
InternalServerError,
|
|
19
|
+
} from "./errors.js";
|
|
20
|
+
export { NOT_GIVEN, NotGiven } from "./types.js";
|
|
21
|
+
export type {
|
|
22
|
+
OmitSentinel as Omit,
|
|
23
|
+
RequestOptions,
|
|
24
|
+
RawResponse,
|
|
25
|
+
Agent,
|
|
26
|
+
AgentDeleted,
|
|
27
|
+
AgentSession,
|
|
28
|
+
AgentSessionDeleted,
|
|
29
|
+
AgentSessionItem,
|
|
30
|
+
RequiredAction,
|
|
31
|
+
Usage,
|
|
32
|
+
TurnError,
|
|
33
|
+
AgentSessionEvent,
|
|
34
|
+
FinalResult,
|
|
35
|
+
} from "./types.js";
|
|
36
|
+
export { SyncCursorPage } from "./pages.js";
|
|
37
|
+
export { AgentSessionEventStream } from "./stream.js";
|
|
38
|
+
export {
|
|
39
|
+
OpenAI,
|
|
40
|
+
type OpenAIOptions,
|
|
41
|
+
type RawResponseClient,
|
|
42
|
+
type RawResponseAgentsResource,
|
|
43
|
+
type RawResponseSessionsResource,
|
|
44
|
+
type RawResponseProxy,
|
|
45
|
+
} from "./client.js";
|
|
46
|
+
|
|
47
|
+
import { OpenAI, type OpenAIOptions } from "./client.js";
|
|
48
|
+
import { UnsupportedFeatureError } from "./errors.js";
|
|
49
|
+
|
|
50
|
+
export interface SarvamAgentsOptions extends OpenAIOptions {
|
|
51
|
+
provider?: "openai";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function SarvamAgents(opts: SarvamAgentsOptions = {}): OpenAI {
|
|
55
|
+
const { provider = "openai", ...rest } = opts;
|
|
56
|
+
if (provider === "openai") {
|
|
57
|
+
return new OpenAI(rest);
|
|
58
|
+
}
|
|
59
|
+
throw new UnsupportedFeatureError(
|
|
60
|
+
`provider=${JSON.stringify(provider)}: not supported by Sarvam (only provider='openai' is available in this release).`,
|
|
61
|
+
{ param: "provider", feature: "compat.provider" },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default OpenAI;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** OpenAI SyncCursorPage<T> with auto-pagination via AsyncIterable (spec §2.3). */
|
|
2
|
+
|
|
3
|
+
export interface CursorPageData<T> {
|
|
4
|
+
data: T[];
|
|
5
|
+
hasMore: boolean;
|
|
6
|
+
firstId: string | null;
|
|
7
|
+
lastId: string | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class SyncCursorPage<T> implements AsyncIterable<T> {
|
|
11
|
+
readonly data: T[];
|
|
12
|
+
readonly has_more: boolean;
|
|
13
|
+
readonly first_id: string | null;
|
|
14
|
+
readonly last_id: string | null;
|
|
15
|
+
private readonly fetchNext?: (after: string) => Promise<SyncCursorPage<T>>;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
page: CursorPageData<T>,
|
|
19
|
+
fetchNext?: (after: string) => Promise<SyncCursorPage<T>>,
|
|
20
|
+
) {
|
|
21
|
+
this.data = page.data;
|
|
22
|
+
this.has_more = page.hasMore;
|
|
23
|
+
this.first_id = page.firstId;
|
|
24
|
+
this.last_id = page.lastId;
|
|
25
|
+
this.fetchNext = fetchNext;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
hasNextPage(): boolean {
|
|
29
|
+
return this.has_more && this.last_id !== null && this.fetchNext !== undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async getNextPage(): Promise<SyncCursorPage<T>> {
|
|
33
|
+
if (!this.hasNextPage() || !this.fetchNext || this.last_id === null) {
|
|
34
|
+
return new SyncCursorPage<T>({ data: [], hasMore: false, firstId: null, lastId: null });
|
|
35
|
+
}
|
|
36
|
+
return this.fetchNext(this.last_id);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async *iterPages(): AsyncGenerator<SyncCursorPage<T>, void, unknown> {
|
|
40
|
+
let page: SyncCursorPage<T> = this;
|
|
41
|
+
while (true) {
|
|
42
|
+
yield page;
|
|
43
|
+
if (!page.hasNextPage()) break;
|
|
44
|
+
page = await page.getNextPage();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<T, void, unknown> {
|
|
49
|
+
for await (const page of this.iterPages()) {
|
|
50
|
+
for (const item of page.data) {
|
|
51
|
+
yield item;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|