sarvam-agents-client 0.5.0 → 0.5.1
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 +284 -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 +101 -0
- package/dist/compat/core/events.js +160 -0
- package/dist/compat/core/history.d.ts +9 -0
- package/dist/compat/core/history.js +87 -0
- package/dist/compat/core/http.d.ts +60 -0
- package/dist/compat/core/http.js +444 -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 +75 -0
- package/dist/compat/core/turns.js +353 -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 +57 -0
- package/dist/compat/openai/client.js +100 -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 +288 -0
- package/dist/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/compat/openai/resources/sessions.js +254 -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 +281 -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 +101 -0
- package/dist/esm/compat/core/events.js +156 -0
- package/dist/esm/compat/core/history.d.ts +9 -0
- package/dist/esm/compat/core/history.js +84 -0
- package/dist/esm/compat/core/http.d.ts +60 -0
- package/dist/esm/compat/core/http.js +439 -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 +75 -0
- package/dist/esm/compat/core/turns.js +347 -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 +57 -0
- package/dist/esm/compat/openai/client.js +96 -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 +282 -0
- package/dist/esm/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/esm/compat/openai/resources/sessions.js +249 -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 +292 -0
- package/src/compat/core/errors.ts +162 -0
- package/src/compat/core/events.ts +192 -0
- package/src/compat/core/history.ts +91 -0
- package/src/compat/core/http.ts +490 -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 +421 -0
- package/src/compat/core/warnings.ts +23 -0
- package/src/compat/index.ts +3 -0
- package/src/compat/openai/client.ts +165 -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 +296 -0
- package/src/compat/openai/resources/sessions.ts +334 -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,53 @@
|
|
|
1
|
+
import { Http } from "./http.js";
|
|
2
|
+
import { ModelResolver } from "./models.js";
|
|
3
|
+
export interface AgentSpec {
|
|
4
|
+
name: string;
|
|
5
|
+
instructions: string;
|
|
6
|
+
model?: string | null;
|
|
7
|
+
reasoningEffort?: string | null;
|
|
8
|
+
reasoningSummary?: string | null;
|
|
9
|
+
toolChoice?: string | null;
|
|
10
|
+
builtins?: Record<string, unknown> | null;
|
|
11
|
+
functions: Record<string, unknown>[];
|
|
12
|
+
outputSchema?: Record<string, unknown> | null;
|
|
13
|
+
outputSchemaName?: string | null;
|
|
14
|
+
metadata: Record<string, string>;
|
|
15
|
+
description?: string | null;
|
|
16
|
+
sarvam: Record<string, unknown>;
|
|
17
|
+
provider: string;
|
|
18
|
+
requestedModel?: string | null;
|
|
19
|
+
multiAgentEnabled?: boolean | null;
|
|
20
|
+
archived?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface AgentRecord {
|
|
23
|
+
id: string;
|
|
24
|
+
spec: AgentSpec;
|
|
25
|
+
version: number | null;
|
|
26
|
+
createdAt: string;
|
|
27
|
+
archived: boolean;
|
|
28
|
+
sarvamModel: string | null;
|
|
29
|
+
rawSettings: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
export declare class Agents {
|
|
32
|
+
private readonly http;
|
|
33
|
+
private readonly models;
|
|
34
|
+
private readonly transport;
|
|
35
|
+
private readonly api;
|
|
36
|
+
private readonly inlineInflight;
|
|
37
|
+
constructor(http: Http, models: ModelResolver);
|
|
38
|
+
private buildRaw;
|
|
39
|
+
create(spec: AgentSpec, opts?: {
|
|
40
|
+
pathOf?: (field: string) => string;
|
|
41
|
+
}): Promise<AgentRecord>;
|
|
42
|
+
retrieve(agentId: string): Promise<AgentRecord>;
|
|
43
|
+
update(agentId: string, change: (spec: AgentSpec) => AgentSpec, opts?: {
|
|
44
|
+
expectedVersion?: number;
|
|
45
|
+
pathOf?: (field: string) => string;
|
|
46
|
+
}): Promise<AgentRecord>;
|
|
47
|
+
list(offset?: number, limit?: number): Promise<[AgentRecord[], boolean]>;
|
|
48
|
+
delete(agentId: string): Promise<void>;
|
|
49
|
+
private findInlineByName;
|
|
50
|
+
ensureInline(spec: AgentSpec, opts?: {
|
|
51
|
+
pathOf?: (field: string) => string;
|
|
52
|
+
}): Promise<AgentRecord>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Agents = void 0;
|
|
4
|
+
/** Core Agents service: neutral AgentSpec <-> Panja agent.json, AGENT.md, and versions. */
|
|
5
|
+
const AgentsApi_js_1 = require("../../apis/AgentsApi.js");
|
|
6
|
+
const agent_js_1 = require("../../compose/agent.js");
|
|
7
|
+
const errors_js_1 = require("../../compose/errors.js");
|
|
8
|
+
const fingerprint_js_1 = require("../../compose/fingerprint.js");
|
|
9
|
+
const settingsTransport_js_1 = require("../../compose/settingsTransport.js");
|
|
10
|
+
const errors_js_2 = require("./errors.js");
|
|
11
|
+
const ids_js_1 = require("./ids.js");
|
|
12
|
+
const refusals_js_1 = require("./refusals.js");
|
|
13
|
+
const warnings_js_1 = require("./warnings.js");
|
|
14
|
+
const ALLOWED_SARVAM_KEYS = new Set([
|
|
15
|
+
"limits",
|
|
16
|
+
"webhooks",
|
|
17
|
+
"triggers",
|
|
18
|
+
"fallback",
|
|
19
|
+
"endpoint",
|
|
20
|
+
"temperature",
|
|
21
|
+
"top_p",
|
|
22
|
+
"max_output_tokens",
|
|
23
|
+
"function_timeout_seconds",
|
|
24
|
+
"output_schema",
|
|
25
|
+
"builtin_tools",
|
|
26
|
+
"skills",
|
|
27
|
+
"knowledge_bases",
|
|
28
|
+
"clarify",
|
|
29
|
+
"apps",
|
|
30
|
+
]);
|
|
31
|
+
function isObj(v) {
|
|
32
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
33
|
+
}
|
|
34
|
+
function clone(v) {
|
|
35
|
+
return JSON.parse(JSON.stringify(v));
|
|
36
|
+
}
|
|
37
|
+
function iso(v) {
|
|
38
|
+
if (v instanceof Date)
|
|
39
|
+
return v.toISOString();
|
|
40
|
+
if (typeof v === "string" && v)
|
|
41
|
+
return v;
|
|
42
|
+
return new Date().toISOString();
|
|
43
|
+
}
|
|
44
|
+
function mapUserError(err, pathOf) {
|
|
45
|
+
const msg = err.message;
|
|
46
|
+
const colon = msg.indexOf(":");
|
|
47
|
+
const prefix = colon !== -1 ? msg.slice(0, colon).trim() : "";
|
|
48
|
+
const param = prefix ? (pathOf ? pathOf(prefix) : prefix) : null;
|
|
49
|
+
return new errors_js_2.CoreAPIError(msg, { status: 400, code: "invalid_request_error", param });
|
|
50
|
+
}
|
|
51
|
+
function validateSarvamKeys(sarvam, provider) {
|
|
52
|
+
for (const k of Object.keys(sarvam)) {
|
|
53
|
+
if (!ALLOWED_SARVAM_KEYS.has(k)) {
|
|
54
|
+
(0, refusals_js_1.refuse)(provider, `sarvam_config.${k}`, {
|
|
55
|
+
reason: `unknown Sarvam extension key ${JSON.stringify(k)}`,
|
|
56
|
+
param: `sarvam_config.${k}`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function specToSettings(spec, sarvamModel, baseSettings) {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
validateSarvamKeys((_a = spec.sarvam) !== null && _a !== void 0 ? _a : {}, spec.provider);
|
|
64
|
+
// Preserve unmapped settings & non-sdk_compat metadata from baseSettings.
|
|
65
|
+
const out = baseSettings ? clone(baseSettings) : {};
|
|
66
|
+
if (!("version" in out))
|
|
67
|
+
out["version"] = 1;
|
|
68
|
+
if (sarvamModel !== null)
|
|
69
|
+
out["model"] = sarvamModel;
|
|
70
|
+
else
|
|
71
|
+
delete out["model"];
|
|
72
|
+
if (spec.reasoningEffort !== undefined && spec.reasoningEffort !== null)
|
|
73
|
+
out["reasoning"] = spec.reasoningEffort;
|
|
74
|
+
else
|
|
75
|
+
delete out["reasoning"];
|
|
76
|
+
if (spec.toolChoice !== undefined && spec.toolChoice !== null)
|
|
77
|
+
out["tool_choice"] = spec.toolChoice;
|
|
78
|
+
else
|
|
79
|
+
delete out["tool_choice"];
|
|
80
|
+
if (spec.builtins !== undefined && spec.builtins !== null) {
|
|
81
|
+
const b = clone(spec.builtins);
|
|
82
|
+
if (spec.multiAgentEnabled !== undefined && spec.multiAgentEnabled !== null) {
|
|
83
|
+
b["subagents"] = spec.multiAgentEnabled;
|
|
84
|
+
}
|
|
85
|
+
out["builtin_tools"] = b;
|
|
86
|
+
}
|
|
87
|
+
else if (spec.multiAgentEnabled !== undefined && spec.multiAgentEnabled !== null) {
|
|
88
|
+
const existingB = isObj(out["builtin_tools"]) ? clone(out["builtin_tools"]) : {};
|
|
89
|
+
existingB["subagents"] = spec.multiAgentEnabled;
|
|
90
|
+
out["builtin_tools"] = existingB;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
delete out["builtin_tools"];
|
|
94
|
+
}
|
|
95
|
+
if (spec.functions.length > 0)
|
|
96
|
+
out["functions"] = clone(spec.functions);
|
|
97
|
+
else
|
|
98
|
+
delete out["functions"];
|
|
99
|
+
if (spec.outputSchema !== undefined && spec.outputSchema !== null)
|
|
100
|
+
out["output_schema"] = clone(spec.outputSchema);
|
|
101
|
+
else
|
|
102
|
+
delete out["output_schema"];
|
|
103
|
+
for (const [k, v] of Object.entries((_b = spec.sarvam) !== null && _b !== void 0 ? _b : {})) {
|
|
104
|
+
if (v === undefined)
|
|
105
|
+
continue;
|
|
106
|
+
if (v === null) {
|
|
107
|
+
delete out[k];
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (k === "builtin_tools" && isObj(v)) {
|
|
111
|
+
const existing = isObj(out["builtin_tools"]) ? clone(out["builtin_tools"]) : {};
|
|
112
|
+
Object.assign(existing, clone(v));
|
|
113
|
+
out["builtin_tools"] = existing;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
out[k] = clone(v);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const compatMeta = {
|
|
120
|
+
provider: spec.provider,
|
|
121
|
+
metadata: { ...((_c = spec.metadata) !== null && _c !== void 0 ? _c : {}) },
|
|
122
|
+
archived: Boolean(spec.archived),
|
|
123
|
+
};
|
|
124
|
+
if (spec.description !== undefined && spec.description !== null)
|
|
125
|
+
compatMeta["description"] = spec.description;
|
|
126
|
+
if (spec.requestedModel !== undefined && spec.requestedModel !== null)
|
|
127
|
+
compatMeta["requested_model"] = spec.requestedModel;
|
|
128
|
+
if (spec.reasoningSummary !== undefined && spec.reasoningSummary !== null)
|
|
129
|
+
compatMeta["reasoning_summary"] = spec.reasoningSummary;
|
|
130
|
+
if (spec.outputSchemaName !== undefined && spec.outputSchemaName !== null)
|
|
131
|
+
compatMeta["output_schema_name"] = spec.outputSchemaName;
|
|
132
|
+
const existingMeta = isObj(out["metadata"]) ? clone(out["metadata"]) : {};
|
|
133
|
+
delete existingMeta["sdk_name"];
|
|
134
|
+
delete existingMeta["sdk_hash"];
|
|
135
|
+
existingMeta["sdk_compat"] = compatMeta;
|
|
136
|
+
out["metadata"] = existingMeta;
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
function settingsToRecord(id, name, instructions, settings, version, createdAt) {
|
|
140
|
+
const rawMeta = isObj(settings["metadata"]) ? settings["metadata"] : {};
|
|
141
|
+
const compat = isObj(rawMeta["sdk_compat"]) ? rawMeta["sdk_compat"] : {};
|
|
142
|
+
const userMetaRaw = isObj(compat["metadata"]) ? compat["metadata"] : {};
|
|
143
|
+
const userMeta = {};
|
|
144
|
+
for (const [k, v] of Object.entries(userMetaRaw))
|
|
145
|
+
userMeta[k] = String(v);
|
|
146
|
+
const sarvamModel = typeof settings["model"] === "string" ? settings["model"] : null;
|
|
147
|
+
const requestedModel = typeof compat["requested_model"] === "string" ? compat["requested_model"] : sarvamModel;
|
|
148
|
+
const builtins = isObj(settings["builtin_tools"]) ? clone(settings["builtin_tools"]) : null;
|
|
149
|
+
const multiAgentEnabled = builtins && "subagents" in builtins ? Boolean(builtins["subagents"]) : null;
|
|
150
|
+
const archived = Boolean(compat["archived"]);
|
|
151
|
+
const spec = {
|
|
152
|
+
name,
|
|
153
|
+
instructions,
|
|
154
|
+
model: requestedModel,
|
|
155
|
+
reasoningEffort: typeof settings["reasoning"] === "string" ? settings["reasoning"] : null,
|
|
156
|
+
reasoningSummary: typeof compat["reasoning_summary"] === "string" ? compat["reasoning_summary"] : null,
|
|
157
|
+
toolChoice: typeof settings["tool_choice"] === "string" ? settings["tool_choice"] : null,
|
|
158
|
+
builtins,
|
|
159
|
+
functions: Array.isArray(settings["functions"]) ? clone(settings["functions"]) : [],
|
|
160
|
+
outputSchema: isObj(settings["output_schema"]) ? clone(settings["output_schema"]) : null,
|
|
161
|
+
outputSchemaName: typeof compat["output_schema_name"] === "string" ? compat["output_schema_name"] : null,
|
|
162
|
+
metadata: userMeta,
|
|
163
|
+
description: typeof compat["description"] === "string" ? compat["description"] : null,
|
|
164
|
+
sarvam: {},
|
|
165
|
+
provider: typeof compat["provider"] === "string" ? compat["provider"] : "openai",
|
|
166
|
+
requestedModel,
|
|
167
|
+
multiAgentEnabled,
|
|
168
|
+
archived,
|
|
169
|
+
};
|
|
170
|
+
return { id, spec, version, createdAt, archived, sarvamModel, rawSettings: clone(settings) };
|
|
171
|
+
}
|
|
172
|
+
class Agents {
|
|
173
|
+
constructor(http, models) {
|
|
174
|
+
this.http = http;
|
|
175
|
+
this.models = models;
|
|
176
|
+
this.inlineInflight = new Map();
|
|
177
|
+
this.transport = new settingsTransport_js_1.SettingsTransport(http.sdkConfig);
|
|
178
|
+
this.api = new AgentsApi_js_1.AgentsApi(http.sdkConfig);
|
|
179
|
+
}
|
|
180
|
+
async buildRaw(spec, pathOf, baseSettings) {
|
|
181
|
+
const resolved = await this.models.resolve(spec.model);
|
|
182
|
+
const effective = clone(spec);
|
|
183
|
+
if (!effective.requestedModel)
|
|
184
|
+
effective.requestedModel = resolved.requested;
|
|
185
|
+
const settings = specToSettings(effective, resolved.sarvamId, baseSettings);
|
|
186
|
+
try {
|
|
187
|
+
return { agent: (0, agent_js_1.rawAgent)(effective.name, effective.instructions, settings), sarvamModel: resolved.sarvamId };
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (err instanceof errors_js_1.UserError)
|
|
191
|
+
throw mapUserError(err, pathOf);
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async create(spec, opts = {}) {
|
|
196
|
+
var _a;
|
|
197
|
+
const { agent } = await this.buildRaw(spec, opts.pathOf);
|
|
198
|
+
try {
|
|
199
|
+
const remote = await this.http.call(() => this.transport.createAgent(agent), { idempotent: false });
|
|
200
|
+
return settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_a = remote.liveVersion) !== null && _a !== void 0 ? _a : null, iso(remote.createdAt));
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
if (err instanceof errors_js_1.UserError)
|
|
204
|
+
throw mapUserError(err, opts.pathOf);
|
|
205
|
+
if (err instanceof errors_js_1.NotSupportedByServerError) {
|
|
206
|
+
throw new errors_js_2.CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
|
|
207
|
+
}
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async retrieve(agentId) {
|
|
212
|
+
var _a;
|
|
213
|
+
const [resource, instr] = await Promise.all([
|
|
214
|
+
this.http.call(() => this.api.getAgent({ agentId }), { idempotent: true }),
|
|
215
|
+
this.http.call(() => this.api.getInstructions({ agentId }), { idempotent: true }),
|
|
216
|
+
]);
|
|
217
|
+
let settings = isObj(resource.settings) ? { ...resource.settings } : {};
|
|
218
|
+
if (Object.keys(settings).length === 0) {
|
|
219
|
+
try {
|
|
220
|
+
const env = await this.http.call(() => this.api.getAgentSettings({ agentId }), { idempotent: true });
|
|
221
|
+
settings = isObj(env.settings) ? { ...env.settings } : {};
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
if (!(err instanceof errors_js_2.CoreAPIError && err.status === 404))
|
|
225
|
+
throw err;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return settingsToRecord(resource.id, resource.name, instr.data, settings, (_a = resource.liveVersion) !== null && _a !== void 0 ? _a : null, iso(resource.createdAt));
|
|
229
|
+
}
|
|
230
|
+
async update(agentId, change, opts = {}) {
|
|
231
|
+
var _a;
|
|
232
|
+
const current = await this.retrieve(agentId);
|
|
233
|
+
if (current.archived) {
|
|
234
|
+
throw new errors_js_2.CoreAPIError(`Cannot update archived agent ${agentId}.`, {
|
|
235
|
+
status: 409,
|
|
236
|
+
code: "agent_archived",
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
if (opts.expectedVersion !== undefined) {
|
|
240
|
+
(0, warnings_js_1.warnOnce)("version_check_not_atomic", "Expected-version check on agent update is not atomic on Sarvam yet; concurrent writers are last-write-wins.");
|
|
241
|
+
if (current.version !== opts.expectedVersion) {
|
|
242
|
+
throw new errors_js_2.CoreAPIError(`Agent ${agentId} is at version ${current.version}, expected ${opts.expectedVersion}`, { status: 409, code: "version_conflict" });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const updatedSpec = change(clone(current.spec));
|
|
246
|
+
const { agent } = await this.buildRaw(updatedSpec, opts.pathOf, current.rawSettings);
|
|
247
|
+
const currentHash = (0, fingerprint_js_1.settingsFingerprint)(current.spec.instructions, current.rawSettings);
|
|
248
|
+
if (updatedSpec.name === current.spec.name && agent.specHash() === currentHash) {
|
|
249
|
+
return current;
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
const remote = await this.http.call(() => this.transport.updateAgent(agentId, agent), { idempotent: false });
|
|
253
|
+
return settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_a = remote.liveVersion) !== null && _a !== void 0 ? _a : null, current.createdAt);
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
if (err instanceof errors_js_1.UserError)
|
|
257
|
+
throw mapUserError(err, opts.pathOf);
|
|
258
|
+
if (err instanceof errors_js_1.NotSupportedByServerError) {
|
|
259
|
+
throw new errors_js_2.CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
|
|
260
|
+
}
|
|
261
|
+
throw err;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async list(offset = 0, limit = 20) {
|
|
265
|
+
var _a;
|
|
266
|
+
const resp = await this.http.call(() => this.api.listAgents({ limit, offset }), { idempotent: true });
|
|
267
|
+
const records = ((_a = resp.data) !== null && _a !== void 0 ? _a : []).map((item) => {
|
|
268
|
+
var _a;
|
|
269
|
+
const instrRaw = item["instructions"];
|
|
270
|
+
return settingsToRecord(item.id, item.name, typeof instrRaw === "string" ? instrRaw : "", isObj(item.settings) ? { ...item.settings } : {}, (_a = item.liveVersion) !== null && _a !== void 0 ? _a : null, iso(item.createdAt));
|
|
271
|
+
});
|
|
272
|
+
return [records, Boolean(resp.hasMore)];
|
|
273
|
+
}
|
|
274
|
+
async delete(agentId) {
|
|
275
|
+
await this.http.call(() => this.transport.deleteAgent(agentId), { idempotent: true });
|
|
276
|
+
}
|
|
277
|
+
async findInlineByName(name) {
|
|
278
|
+
var _a, _b;
|
|
279
|
+
const matches = [];
|
|
280
|
+
let offset = 0;
|
|
281
|
+
for (;;) {
|
|
282
|
+
const off = offset;
|
|
283
|
+
const page = await this.http.call(() => this.api.listAgents({ limit: 100, offset: off }), { idempotent: true });
|
|
284
|
+
const items = (_a = page.data) !== null && _a !== void 0 ? _a : [];
|
|
285
|
+
for (const item of items) {
|
|
286
|
+
const settings = isObj(item.settings) ? { ...item.settings } : {};
|
|
287
|
+
const meta = isObj(settings["metadata"]) ? settings["metadata"] : {};
|
|
288
|
+
const sdkName = typeof meta["sdk_name"] === "string" ? meta["sdk_name"] : null;
|
|
289
|
+
if (item.name === name || sdkName === name) {
|
|
290
|
+
const instrRaw = item["instructions"];
|
|
291
|
+
matches.push({
|
|
292
|
+
id: item.id,
|
|
293
|
+
name: item.name,
|
|
294
|
+
instructions: typeof instrRaw === "string" ? instrRaw : "",
|
|
295
|
+
settings,
|
|
296
|
+
version: (_b = item.liveVersion) !== null && _b !== void 0 ? _b : null,
|
|
297
|
+
createdAt: iso(item.createdAt),
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (items.length === 0 || !page.hasMore)
|
|
302
|
+
break;
|
|
303
|
+
offset += items.length;
|
|
304
|
+
}
|
|
305
|
+
if (matches.length === 0)
|
|
306
|
+
return null;
|
|
307
|
+
matches.sort((a, b) => a.id.localeCompare(b.id));
|
|
308
|
+
const chosen = matches[0];
|
|
309
|
+
return settingsToRecord(chosen.id, chosen.name, chosen.instructions, chosen.settings, chosen.version, chosen.createdAt);
|
|
310
|
+
}
|
|
311
|
+
async ensureInline(spec, opts = {}) {
|
|
312
|
+
const resolved = await this.models.resolve(spec.model);
|
|
313
|
+
const temp = clone(spec);
|
|
314
|
+
if (!temp.requestedModel)
|
|
315
|
+
temp.requestedModel = resolved.requested;
|
|
316
|
+
const tempSettings = specToSettings(temp, resolved.sarvamId);
|
|
317
|
+
const fp = (0, fingerprint_js_1.settingsFingerprint)(temp.instructions, tempSettings);
|
|
318
|
+
temp.name = (0, ids_js_1.inlineAgentName)(fp);
|
|
319
|
+
const key = temp.name;
|
|
320
|
+
const inflight = this.inlineInflight.get(key);
|
|
321
|
+
if (inflight)
|
|
322
|
+
return inflight;
|
|
323
|
+
const promise = (async () => {
|
|
324
|
+
const existing = await this.findInlineByName(key);
|
|
325
|
+
if (existing)
|
|
326
|
+
return existing;
|
|
327
|
+
return this.create(temp, opts);
|
|
328
|
+
})();
|
|
329
|
+
this.inlineInflight.set(key, promise);
|
|
330
|
+
try {
|
|
331
|
+
return await promise;
|
|
332
|
+
}
|
|
333
|
+
finally {
|
|
334
|
+
this.inlineInflight.delete(key);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
exports.Agents = Agents;
|