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,369 @@
|
|
|
1
|
+
/** OpenAI beta.agents.sessions resource (spec §3.3). */
|
|
2
|
+
import { Agents } from "../../core/agents.js";
|
|
3
|
+
import { Http, type PerCallRequestOptions } from "../../core/http.js";
|
|
4
|
+
import { CursorPager } from "../../core/paging.js";
|
|
5
|
+
import { Sessions, type SessionRecord } from "../../core/sessions.js";
|
|
6
|
+
import { prefetchRemoteParts, Turns } from "../../core/turns.js";
|
|
7
|
+
import { BadRequestError, wrapCoreErrors } from "../errors.js";
|
|
8
|
+
import { SyncCursorPage } from "../pages.js";
|
|
9
|
+
import { AgentSessionEventStream } from "../stream.js";
|
|
10
|
+
import {
|
|
11
|
+
checkExtraParams,
|
|
12
|
+
isoToEpoch,
|
|
13
|
+
NotGiven,
|
|
14
|
+
refuseOpenAI,
|
|
15
|
+
type AgentSession,
|
|
16
|
+
type AgentSessionDeleted,
|
|
17
|
+
type RequestOptions,
|
|
18
|
+
type RequiredAction,
|
|
19
|
+
} from "../types.js";
|
|
20
|
+
import { makePathOfComposeField, openaiToSpec, RefusedResource } from "./agents.js";
|
|
21
|
+
import { EventsResource, ItemsResource, parseInputParts } from "./events.js";
|
|
22
|
+
|
|
23
|
+
const ENV_KEYS = new Set(["type", "vault_ids", "spend_control"]);
|
|
24
|
+
|
|
25
|
+
function isObj(v: unknown): v is Record<string, unknown> {
|
|
26
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isGiven(v: unknown): boolean {
|
|
30
|
+
return v !== undefined && !(v instanceof NotGiven);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function recordToSession(rec: SessionRecord): AgentSession {
|
|
34
|
+
const actions: RequiredAction[] = [];
|
|
35
|
+
for (const step of rec.required) {
|
|
36
|
+
if (step.kind === "function") {
|
|
37
|
+
for (const c of step.calls) {
|
|
38
|
+
actions.push({
|
|
39
|
+
type: "function",
|
|
40
|
+
turn_id: step.responseId || null,
|
|
41
|
+
call_id: c.callId,
|
|
42
|
+
name: c.name,
|
|
43
|
+
arguments: JSON.stringify(c.arguments),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
} else if (step.kind === "clarify") {
|
|
47
|
+
actions.push({
|
|
48
|
+
type: "sarvam.clarify",
|
|
49
|
+
turn_id: step.responseId || null,
|
|
50
|
+
request_id: step.requestId,
|
|
51
|
+
questions: step.questions.slice(),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
id: rec.id,
|
|
57
|
+
object: "agent.session",
|
|
58
|
+
agent_id: rec.agentId || null,
|
|
59
|
+
status: rec.status,
|
|
60
|
+
required_actions: actions,
|
|
61
|
+
metadata: { ...rec.metadata },
|
|
62
|
+
created_at: isoToEpoch(rec.createdAt),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function checkEnvironment(environment: unknown): boolean {
|
|
67
|
+
if (!isObj(environment)) {
|
|
68
|
+
throw new BadRequestError("environment must be an object with 'type'.", { status: 400, param: "environment" });
|
|
69
|
+
}
|
|
70
|
+
for (const k of Object.keys(environment)) {
|
|
71
|
+
if (!ENV_KEYS.has(k)) {
|
|
72
|
+
refuseOpenAI(`environment.${k}`, { reason: `unknown environment key ${JSON.stringify(k)}`, param: `environment.${k}` });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (environment["vault_ids"] !== undefined && environment["vault_ids"] !== null) {
|
|
76
|
+
refuseOpenAI("environment.vault_ids");
|
|
77
|
+
}
|
|
78
|
+
if (environment["spend_control"] !== undefined && environment["spend_control"] !== null) {
|
|
79
|
+
refuseOpenAI("environment.spend_control");
|
|
80
|
+
}
|
|
81
|
+
const etype = environment["type"];
|
|
82
|
+
if (etype === "openai_hosted" || etype === "sarvam_hosted") return true;
|
|
83
|
+
if (etype === "none") return false;
|
|
84
|
+
if (etype === "self_hosted") refuseOpenAI("environment.type=self_hosted");
|
|
85
|
+
refuseOpenAI(`environment.type=${String(etype)}`, {
|
|
86
|
+
reason: `unsupported environment type ${JSON.stringify(etype)}`,
|
|
87
|
+
param: "environment.type",
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class SessionsResource {
|
|
92
|
+
readonly events: EventsResource;
|
|
93
|
+
readonly items: ItemsResource;
|
|
94
|
+
readonly traces: RefusedResource;
|
|
95
|
+
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly http: Http,
|
|
98
|
+
private readonly agents: Agents,
|
|
99
|
+
private readonly sessions: Sessions,
|
|
100
|
+
private readonly turns: Turns,
|
|
101
|
+
events: EventsResource,
|
|
102
|
+
items: ItemsResource,
|
|
103
|
+
) {
|
|
104
|
+
this.events = events;
|
|
105
|
+
this.items = items;
|
|
106
|
+
this.traces = new RefusedResource("beta.agents.sessions.traces");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async create(params: Record<string, unknown> & { stream: true }, options?: RequestOptions): Promise<AgentSessionEventStream>;
|
|
110
|
+
async create(params: Record<string, unknown> & { stream?: false }, options?: RequestOptions): Promise<AgentSession>;
|
|
111
|
+
async create(params?: Record<string, unknown>, options?: RequestOptions): Promise<AgentSession | AgentSessionEventStream>;
|
|
112
|
+
async create(params: Record<string, unknown> = {}, options?: RequestOptions): Promise<AgentSession | AgentSessionEventStream> {
|
|
113
|
+
const known = new Set([
|
|
114
|
+
"environment", "agent", "agent_id", "input", "metadata", "stream", "pin_agent_version", "sarvam_config", "idempotency_key",
|
|
115
|
+
]);
|
|
116
|
+
const { sarvamConfig: extraSarvam, reqOpts } = checkExtraParams(params, known, options);
|
|
117
|
+
|
|
118
|
+
const hasIdemHeader = [
|
|
119
|
+
...Object.keys(this.http.config.defaultHeaders),
|
|
120
|
+
...Object.keys(reqOpts.headers ?? {}),
|
|
121
|
+
].some((k) => k.toLowerCase() === "idempotency-key");
|
|
122
|
+
if (
|
|
123
|
+
options?.idempotencyKey != null ||
|
|
124
|
+
(isGiven(params["idempotency_key"]) && params["idempotency_key"] !== null) ||
|
|
125
|
+
hasIdemHeader
|
|
126
|
+
) {
|
|
127
|
+
refuseOpenAI("sessions.create.idempotency_key");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (isGiven(params["pin_agent_version"]) && params["pin_agent_version"]) {
|
|
131
|
+
refuseOpenAI("sessions.create.pin_agent_version");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const sandboxOn = checkEnvironment(params["environment"]);
|
|
135
|
+
const hasInput = isGiven(params["input"]) && params["input"] !== null;
|
|
136
|
+
if (!sandboxOn && !hasInput) {
|
|
137
|
+
throw new BadRequestError("environment={'type': 'none'} requires 'input'.", { status: 400, param: "input" });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const hasAgent = isGiven(params["agent"]) && params["agent"] !== null;
|
|
141
|
+
const hasAgentId = isGiven(params["agent_id"]) && params["agent_id"] !== null;
|
|
142
|
+
if (hasAgent === hasAgentId) {
|
|
143
|
+
throw new BadRequestError("Pass exactly one of 'agent' or 'agent_id'.", { status: 400, param: "agent" });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Validate input before deploying an inline agent or creating a conversation.
|
|
147
|
+
let parsedParts = hasInput ? parseInputParts(params["input"]) : null;
|
|
148
|
+
if (parsedParts !== null) {
|
|
149
|
+
for (const p of parsedParts) {
|
|
150
|
+
if (p.type === "file_ref") {
|
|
151
|
+
throw new BadRequestError(
|
|
152
|
+
`file_id ${JSON.stringify(p.fileId)} cannot be used on sessions.create because Panja artifacts are scoped to an ` +
|
|
153
|
+
"existing session; pass inline base64 data or an http(s) URL instead.",
|
|
154
|
+
{ status: 400, param: p.param ?? "file_id" },
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let resolvedAgentId: string;
|
|
161
|
+
if (hasAgent) {
|
|
162
|
+
if (!isObj(params["agent"])) {
|
|
163
|
+
throw new BadRequestError("'agent' must be an object.", { status: 400, param: "agent" });
|
|
164
|
+
}
|
|
165
|
+
const mergedAgent: Record<string, unknown> = { ...params["agent"] };
|
|
166
|
+
if (isObj(params["sarvam_config"])) {
|
|
167
|
+
mergedAgent["sarvam_config"] = { ...(isObj(mergedAgent["sarvam_config"]) ? mergedAgent["sarvam_config"] : {}), ...params["sarvam_config"] };
|
|
168
|
+
}
|
|
169
|
+
const spec = openaiToSpec(mergedAgent, sandboxOn, extraSarvam);
|
|
170
|
+
const rawTools = Array.isArray(mergedAgent["tools"]) ? mergedAgent["tools"] : [];
|
|
171
|
+
const fnIdx: number[] = [];
|
|
172
|
+
for (let i = 0; i < rawTools.length; i++) {
|
|
173
|
+
const t = rawTools[i];
|
|
174
|
+
if (isObj(t) && t["type"] === "function") fnIdx.push(i);
|
|
175
|
+
}
|
|
176
|
+
const agentRec = await wrapCoreErrors(() =>
|
|
177
|
+
this.http.withRequestOptions(reqOpts, () =>
|
|
178
|
+
this.agents.ensureInline(spec, { pathOf: makePathOfComposeField(fnIdx) }),
|
|
179
|
+
),
|
|
180
|
+
);
|
|
181
|
+
resolvedAgentId = agentRec.id;
|
|
182
|
+
} else {
|
|
183
|
+
const aid = String(params["agent_id"]);
|
|
184
|
+
const agentRec = await wrapCoreErrors(() =>
|
|
185
|
+
this.http.withRequestOptions(reqOpts, () => this.agents.retrieve(aid)),
|
|
186
|
+
);
|
|
187
|
+
if (agentRec.spec.builtins && "sandbox" in agentRec.spec.builtins) {
|
|
188
|
+
if (Boolean(agentRec.spec.builtins["sandbox"]) !== sandboxOn) {
|
|
189
|
+
refuseOpenAI("environment.contradicts_agent");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
resolvedAgentId = aid;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Fetch any remote URLs BEFORE creating the conversation so a bad/oversized URL never orphans a session.
|
|
196
|
+
if (parsedParts !== null) {
|
|
197
|
+
const toPrefetch = parsedParts;
|
|
198
|
+
parsedParts = await wrapCoreErrors(() =>
|
|
199
|
+
this.http.withRequestOptions(reqOpts, () => prefetchRemoteParts(this.http, toPrefetch)),
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const meta = isObj(params["metadata"])
|
|
204
|
+
? Object.fromEntries(Object.entries(params["metadata"]).map(([k, v]) => [k, String(v)]))
|
|
205
|
+
: undefined;
|
|
206
|
+
|
|
207
|
+
const sessionRec = await wrapCoreErrors(() =>
|
|
208
|
+
this.http.withRequestOptions(reqOpts, () =>
|
|
209
|
+
this.sessions.create({
|
|
210
|
+
agentId: resolvedAgentId,
|
|
211
|
+
metadata: meta,
|
|
212
|
+
provider: "openai",
|
|
213
|
+
}),
|
|
214
|
+
),
|
|
215
|
+
);
|
|
216
|
+
const sessionObj = recordToSession(sessionRec);
|
|
217
|
+
const stream = Boolean(params["stream"]);
|
|
218
|
+
|
|
219
|
+
const bindSignal = (evStream: AgentSessionEventStream): AgentSessionEventStream => {
|
|
220
|
+
if (options?.signal) {
|
|
221
|
+
if (options.signal.aborted) evStream.controller.abort(options.signal.reason);
|
|
222
|
+
else options.signal.addEventListener("abort", () => evStream.controller.abort(options.signal!.reason), { once: true });
|
|
223
|
+
}
|
|
224
|
+
return evStream;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
if (parsedParts !== null) {
|
|
228
|
+
let msgParts;
|
|
229
|
+
try {
|
|
230
|
+
const toResolve = parsedParts;
|
|
231
|
+
msgParts = await wrapCoreErrors(() =>
|
|
232
|
+
this.http.withRequestOptions(reqOpts, () => this.turns.resolveParts(sessionRec.id, toResolve)),
|
|
233
|
+
);
|
|
234
|
+
} catch (err) {
|
|
235
|
+
try {
|
|
236
|
+
await this.sessions.delete(sessionRec.id);
|
|
237
|
+
} catch {
|
|
238
|
+
// Best-effort cleanup of empty conversation after failed upload.
|
|
239
|
+
}
|
|
240
|
+
throw err;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const msgId = this.http.newIdempotencyKey();
|
|
244
|
+
if (stream) {
|
|
245
|
+
const rawStream = await wrapCoreErrors(() =>
|
|
246
|
+
this.http.withRequestOptions(reqOpts, () =>
|
|
247
|
+
this.turns.sendMessageParts(sessionRec.id, resolvedAgentId, msgParts, { msgId, returnStream: true }),
|
|
248
|
+
),
|
|
249
|
+
);
|
|
250
|
+
const listener = this.turns.listen(sessionRec.id, { initialStream: rawStream!, reqOpts });
|
|
251
|
+
return bindSignal(new AgentSessionEventStream(sessionRec.id, { listener, sessionCreated: sessionObj }));
|
|
252
|
+
}
|
|
253
|
+
await wrapCoreErrors(() =>
|
|
254
|
+
this.http.withRequestOptions(reqOpts, () =>
|
|
255
|
+
this.turns.sendMessageParts(sessionRec.id, resolvedAgentId, msgParts, { msgId, returnStream: false }),
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
return sessionObj;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (stream) {
|
|
262
|
+
const listener = this.turns.listen(sessionRec.id, { reqOpts });
|
|
263
|
+
return bindSignal(new AgentSessionEventStream(sessionRec.id, { listener, sessionCreated: sessionObj }));
|
|
264
|
+
}
|
|
265
|
+
return sessionObj;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async retrieve(sessionId: string, params: Record<string, unknown> = {}, options?: RequestOptions): Promise<AgentSession> {
|
|
269
|
+
const { reqOpts } = checkExtraParams(params, new Set(), options);
|
|
270
|
+
const rec = await wrapCoreErrors(() =>
|
|
271
|
+
this.http.withRequestOptions(reqOpts, () => this.sessions.retrieve(sessionId)),
|
|
272
|
+
);
|
|
273
|
+
return recordToSession(rec);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async update(sessionId: string, params: Record<string, unknown> = {}, options?: RequestOptions): Promise<AgentSession> {
|
|
277
|
+
const { reqOpts } = checkExtraParams(params, new Set(["metadata", "model", "reasoning", "tools", "agent"]), options);
|
|
278
|
+
if (isGiven(params["model"]) && params["model"] !== null) refuseOpenAI("sessions.update.model");
|
|
279
|
+
if (isGiven(params["reasoning"]) && params["reasoning"] !== null) refuseOpenAI("sessions.update.reasoning");
|
|
280
|
+
if (isGiven(params["tools"]) && params["tools"] !== null) refuseOpenAI("sessions.update.tools");
|
|
281
|
+
if (isGiven(params["agent"]) && params["agent"] !== null) refuseOpenAI("sessions.update.agent");
|
|
282
|
+
const meta = isObj(params["metadata"]) ? (params["metadata"] as Record<string, string | null>) : undefined;
|
|
283
|
+
const rec = await wrapCoreErrors(() =>
|
|
284
|
+
this.http.withRequestOptions(reqOpts, () => this.sessions.update(sessionId, { metadata: meta })),
|
|
285
|
+
);
|
|
286
|
+
return recordToSession(rec);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private async listByCursor(opts: {
|
|
290
|
+
panjaCursor?: string;
|
|
291
|
+
agentId?: string;
|
|
292
|
+
limit: number;
|
|
293
|
+
metadata?: Record<string, string>;
|
|
294
|
+
reqOpts: PerCallRequestOptions;
|
|
295
|
+
buffered?: SessionRecord[];
|
|
296
|
+
}): Promise<SyncCursorPage<AgentSession>> {
|
|
297
|
+
const res = await wrapCoreErrors(() =>
|
|
298
|
+
this.http.withRequestOptions(opts.reqOpts, () =>
|
|
299
|
+
this.sessions.listCursor({
|
|
300
|
+
cursor: opts.panjaCursor,
|
|
301
|
+
limit: opts.limit,
|
|
302
|
+
agentId: opts.agentId,
|
|
303
|
+
metadata: opts.metadata,
|
|
304
|
+
buffered: opts.buffered,
|
|
305
|
+
}),
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
const items = res.items.map(recordToSession);
|
|
309
|
+
const nc = res.nextCursor;
|
|
310
|
+
const lo = res.leftover;
|
|
311
|
+
return new SyncCursorPage<AgentSession>(
|
|
312
|
+
{
|
|
313
|
+
data: items,
|
|
314
|
+
hasMore: res.hasMore,
|
|
315
|
+
firstId: items.length > 0 ? items[0].id : null,
|
|
316
|
+
lastId: items.length > 0 ? items[items.length - 1].id : null,
|
|
317
|
+
},
|
|
318
|
+
res.hasMore
|
|
319
|
+
? () => this.listByCursor({ ...opts, panjaCursor: nc ?? undefined, buffered: lo })
|
|
320
|
+
: undefined,
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async list(params: Record<string, unknown> = {}, options?: RequestOptions): Promise<SyncCursorPage<AgentSession>> {
|
|
325
|
+
const { reqOpts } = checkExtraParams(params, new Set(["after", "agent_id", "limit", "metadata", "order"]), options);
|
|
326
|
+
const agentId = typeof params["agent_id"] === "string" ? params["agent_id"] : undefined;
|
|
327
|
+
const metadata = isObj(params["metadata"]) ? (params["metadata"] as Record<string, string>) : undefined;
|
|
328
|
+
const limit = params["limit"] !== undefined ? (params["limit"] as number) : 20;
|
|
329
|
+
const order = params["order"] !== undefined ? String(params["order"]) : "desc";
|
|
330
|
+
const after = typeof params["after"] === "string" ? params["after"] : undefined;
|
|
331
|
+
|
|
332
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100) {
|
|
333
|
+
throw new BadRequestError("limit must be an integer between 1 and 100", { status: 400, param: "limit" });
|
|
334
|
+
}
|
|
335
|
+
if (order !== "asc" && order !== "desc") {
|
|
336
|
+
throw new BadRequestError("order must be 'asc' or 'desc'", { status: 400, param: "order" });
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (order === "desc" && after === undefined) {
|
|
340
|
+
return this.listByCursor({ agentId, limit, metadata, reqOpts });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const pager = new CursorPager<SessionRecord>(
|
|
344
|
+
(off, lim) => this.sessions.list(off, lim, { agentId, metadata }),
|
|
345
|
+
(r) => r.id,
|
|
346
|
+
);
|
|
347
|
+
const [records, hasMore] = await wrapCoreErrors(() =>
|
|
348
|
+
this.http.withRequestOptions(reqOpts, () => pager.page({ limit, order, after })),
|
|
349
|
+
);
|
|
350
|
+
const items = records.map(recordToSession);
|
|
351
|
+
return new SyncCursorPage<AgentSession>(
|
|
352
|
+
{
|
|
353
|
+
data: items,
|
|
354
|
+
hasMore,
|
|
355
|
+
firstId: items.length > 0 ? items[0].id : null,
|
|
356
|
+
lastId: items.length > 0 ? items[items.length - 1].id : null,
|
|
357
|
+
},
|
|
358
|
+
(nextAfter) => this.list({ ...params, after: nextAfter }, options),
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async delete(sessionId: string, params: Record<string, unknown> = {}, options?: RequestOptions): Promise<AgentSessionDeleted> {
|
|
363
|
+
const { reqOpts } = checkExtraParams(params, new Set(), options);
|
|
364
|
+
await wrapCoreErrors(() =>
|
|
365
|
+
this.http.withRequestOptions(reqOpts, () => this.sessions.delete(sessionId)),
|
|
366
|
+
);
|
|
367
|
+
return { id: sessionId, object: "agent.session.deleted", deleted: true };
|
|
368
|
+
}
|
|
369
|
+
}
|