sarvam-agents-client 0.1.2 → 0.2.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/README.md +15 -0
- package/dist/apis/AgentConversationsApi.d.ts +44 -1
- package/dist/apis/AgentConversationsApi.js +104 -1
- package/dist/apis/AgentsApi.d.ts +77 -5
- package/dist/apis/AgentsApi.js +163 -5
- package/dist/apis/AppsApi.d.ts +43 -1
- package/dist/apis/AppsApi.js +108 -0
- package/dist/apis/ArtifactsApi.d.ts +31 -1
- package/dist/apis/ArtifactsApi.js +82 -0
- package/dist/apis/CustomAPIsApi.d.ts +124 -0
- package/dist/apis/CustomAPIsApi.js +305 -0
- package/dist/apis/ModelsApi.d.ts +26 -0
- package/dist/apis/ModelsApi.js +52 -0
- package/dist/apis/index.d.ts +2 -0
- package/dist/apis/index.js +2 -0
- package/dist/compose/agent.d.ts +44 -0
- package/dist/compose/agent.js +222 -0
- package/dist/compose/builtins.d.ts +4 -0
- package/dist/compose/builtins.js +22 -0
- package/dist/compose/deploy.d.ts +30 -0
- package/dist/compose/deploy.js +146 -0
- package/dist/compose/errors.d.ts +48 -0
- package/dist/compose/errors.js +68 -0
- package/dist/compose/events.d.ts +63 -0
- package/dist/compose/events.js +100 -0
- package/dist/compose/fingerprint.d.ts +6 -0
- package/dist/compose/fingerprint.js +83 -0
- package/dist/compose/index.d.ts +13 -0
- package/dist/compose/index.js +33 -0
- package/dist/compose/run.d.ts +100 -0
- package/dist/compose/run.js +536 -0
- package/dist/compose/runState.d.ts +26 -0
- package/dist/compose/runState.js +79 -0
- package/dist/compose/settingsTransport.d.ts +24 -0
- package/dist/compose/settingsTransport.js +280 -0
- package/dist/compose/tools.d.ts +17 -0
- package/dist/compose/tools.js +44 -0
- package/dist/compose/transport.d.ts +34 -0
- package/dist/compose/transport.js +2 -0
- package/dist/esm/apis/AgentConversationsApi.d.ts +44 -1
- package/dist/esm/apis/AgentConversationsApi.js +104 -1
- package/dist/esm/apis/AgentsApi.d.ts +77 -5
- package/dist/esm/apis/AgentsApi.js +163 -5
- package/dist/esm/apis/AppsApi.d.ts +43 -1
- package/dist/esm/apis/AppsApi.js +109 -1
- package/dist/esm/apis/ArtifactsApi.d.ts +31 -1
- package/dist/esm/apis/ArtifactsApi.js +83 -1
- package/dist/esm/apis/CustomAPIsApi.d.ts +124 -0
- package/dist/esm/apis/CustomAPIsApi.js +301 -0
- package/dist/esm/apis/ModelsApi.d.ts +26 -0
- package/dist/esm/apis/ModelsApi.js +48 -0
- package/dist/esm/apis/index.d.ts +2 -0
- package/dist/esm/apis/index.js +2 -0
- package/dist/esm/compose/agent.d.ts +44 -0
- package/dist/esm/compose/agent.js +218 -0
- package/dist/esm/compose/builtins.d.ts +4 -0
- package/dist/esm/compose/builtins.js +18 -0
- package/dist/esm/compose/deploy.d.ts +30 -0
- package/dist/esm/compose/deploy.js +137 -0
- package/dist/esm/compose/errors.d.ts +48 -0
- package/dist/esm/compose/errors.js +53 -0
- package/dist/esm/compose/events.d.ts +63 -0
- package/dist/esm/compose/events.js +97 -0
- package/dist/esm/compose/fingerprint.d.ts +6 -0
- package/dist/esm/compose/fingerprint.js +78 -0
- package/dist/esm/compose/index.d.ts +13 -0
- package/dist/esm/compose/index.js +8 -0
- package/dist/esm/compose/run.d.ts +100 -0
- package/dist/esm/compose/run.js +530 -0
- package/dist/esm/compose/runState.d.ts +26 -0
- package/dist/esm/compose/runState.js +75 -0
- package/dist/esm/compose/settingsTransport.d.ts +24 -0
- package/dist/esm/compose/settingsTransport.js +276 -0
- package/dist/esm/compose/tools.d.ts +17 -0
- package/dist/esm/compose/tools.js +34 -0
- package/dist/esm/compose/transport.d.ts +34 -0
- package/dist/esm/compose/transport.js +1 -0
- package/dist/esm/models/AgentChannelRef.d.ts +1 -0
- package/dist/esm/models/AgentChannelRef.js +2 -1
- package/dist/esm/models/AgentConversationDetail.d.ts +15 -2
- package/dist/esm/models/AgentConversationDetail.js +6 -4
- package/dist/esm/models/AgentConversationView.d.ts +11 -5
- package/dist/esm/models/AgentConversationView.js +4 -4
- package/dist/esm/models/AgentCreate.d.ts +8 -0
- package/dist/esm/models/AgentCreate.js +2 -0
- package/dist/esm/models/AgentCustomApiList.d.ts +33 -0
- package/dist/esm/models/AgentCustomApiList.js +44 -0
- package/dist/esm/models/AgentResource.d.ts +14 -0
- package/dist/esm/models/AgentResource.js +4 -0
- package/dist/esm/models/AgentSettingsResponse.d.ts +53 -0
- package/dist/esm/models/AgentSettingsResponse.js +52 -0
- package/dist/esm/models/AgentUpdate.d.ts +8 -0
- package/dist/esm/models/AgentUpdate.js +2 -0
- package/dist/esm/models/AppAccountView.d.ts +88 -0
- package/dist/esm/models/AppAccountView.js +79 -0
- package/dist/esm/models/AppAccountsView.d.ts +66 -0
- package/dist/esm/models/AppAccountsView.js +60 -0
- package/dist/esm/models/AppResource.d.ts +36 -0
- package/dist/esm/models/AppResource.js +12 -0
- package/dist/esm/models/AppResourceCheckView.d.ts +74 -0
- package/dist/esm/models/AppResourceCheckView.js +63 -0
- package/dist/esm/models/AppResourceView.d.ts +68 -0
- package/dist/esm/models/AppResourceView.js +67 -0
- package/dist/esm/models/AppResourcesView.d.ts +54 -0
- package/dist/esm/models/AppResourcesView.js +56 -0
- package/dist/esm/models/AppToolsResponse.d.ts +14 -0
- package/dist/esm/models/AppToolsResponse.js +4 -0
- package/dist/esm/models/AppView.d.ts +42 -0
- package/dist/esm/models/AppView.js +14 -0
- package/dist/esm/models/ArtifactVersionResponse.d.ts +94 -0
- package/dist/esm/models/ArtifactVersionResponse.js +80 -0
- package/dist/esm/models/AttachedAgent.d.ts +44 -0
- package/dist/esm/models/AttachedAgent.js +49 -0
- package/dist/esm/models/Auth.d.ts +30 -0
- package/dist/esm/models/Auth.js +52 -0
- package/dist/esm/models/BindCustomApiBody.d.ts +32 -0
- package/dist/esm/models/BindCustomApiBody.js +43 -0
- package/dist/esm/models/ConversationArtifact.d.ts +14 -0
- package/dist/esm/models/ConversationArtifact.js +9 -0
- package/dist/esm/models/CustomApi.d.ts +102 -0
- package/dist/esm/models/CustomApi.js +90 -0
- package/dist/esm/models/CustomApiBasicAuth.d.ts +51 -0
- package/dist/esm/models/CustomApiBasicAuth.js +57 -0
- package/dist/esm/models/CustomApiBearerAuth.d.ts +45 -0
- package/dist/esm/models/CustomApiBearerAuth.js +53 -0
- package/dist/esm/models/CustomApiCreate.d.ts +57 -0
- package/dist/esm/models/CustomApiCreate.js +56 -0
- package/dist/esm/models/CustomApiHeader.d.ts +44 -0
- package/dist/esm/models/CustomApiHeader.js +47 -0
- package/dist/esm/models/CustomApiHeadersAuth.d.ts +46 -0
- package/dist/esm/models/CustomApiHeadersAuth.js +54 -0
- package/dist/esm/models/CustomApiList.d.ts +64 -0
- package/dist/esm/models/CustomApiList.js +64 -0
- package/dist/esm/models/CustomApiName.d.ts +44 -0
- package/dist/esm/models/CustomApiName.js +49 -0
- package/dist/esm/models/CustomApiUpdate.d.ts +58 -0
- package/dist/esm/models/CustomApiUpdate.js +50 -0
- package/dist/esm/models/CustomApiUpdateAuth.d.ts +30 -0
- package/dist/esm/models/CustomApiUpdateAuth.js +52 -0
- package/dist/esm/models/ErrorDetailItem.d.ts +5 -0
- package/dist/esm/models/ListModels403Response.d.ts +23 -0
- package/dist/esm/models/ListModels403Response.js +45 -0
- package/dist/esm/models/ModelEndpointView.d.ts +38 -0
- package/dist/esm/models/ModelEndpointView.js +45 -0
- package/dist/esm/models/ModelView.d.ts +69 -0
- package/dist/esm/models/ModelView.js +56 -0
- package/dist/esm/models/ModelsResponse.d.ts +33 -0
- package/dist/esm/models/ModelsResponse.js +44 -0
- package/dist/esm/models/SettingsIssueView.d.ts +44 -0
- package/dist/esm/models/SettingsIssueView.js +51 -0
- package/dist/esm/models/VersionCommitBody.d.ts +44 -0
- package/dist/esm/models/VersionCommitBody.js +51 -0
- package/dist/esm/models/VersionUploadUrlBody.d.ts +32 -0
- package/dist/esm/models/VersionUploadUrlBody.js +43 -0
- package/dist/esm/models/index.d.ts +28 -0
- package/dist/esm/models/index.js +28 -0
- package/dist/models/AgentChannelRef.d.ts +1 -0
- package/dist/models/AgentChannelRef.js +2 -1
- package/dist/models/AgentConversationDetail.d.ts +15 -2
- package/dist/models/AgentConversationDetail.js +6 -4
- package/dist/models/AgentConversationView.d.ts +11 -5
- package/dist/models/AgentConversationView.js +4 -4
- package/dist/models/AgentCreate.d.ts +8 -0
- package/dist/models/AgentCreate.js +2 -0
- package/dist/models/AgentCustomApiList.d.ts +33 -0
- package/dist/models/AgentCustomApiList.js +51 -0
- package/dist/models/AgentResource.d.ts +14 -0
- package/dist/models/AgentResource.js +4 -0
- package/dist/models/AgentSettingsResponse.d.ts +53 -0
- package/dist/models/AgentSettingsResponse.js +59 -0
- package/dist/models/AgentUpdate.d.ts +8 -0
- package/dist/models/AgentUpdate.js +2 -0
- package/dist/models/AppAccountView.d.ts +88 -0
- package/dist/models/AppAccountView.js +87 -0
- package/dist/models/AppAccountsView.d.ts +66 -0
- package/dist/models/AppAccountsView.js +68 -0
- package/dist/models/AppResource.d.ts +36 -0
- package/dist/models/AppResource.js +12 -0
- package/dist/models/AppResourceCheckView.d.ts +74 -0
- package/dist/models/AppResourceCheckView.js +71 -0
- package/dist/models/AppResourceView.d.ts +68 -0
- package/dist/models/AppResourceView.js +74 -0
- package/dist/models/AppResourcesView.d.ts +54 -0
- package/dist/models/AppResourcesView.js +64 -0
- package/dist/models/AppToolsResponse.d.ts +14 -0
- package/dist/models/AppToolsResponse.js +4 -0
- package/dist/models/AppView.d.ts +42 -0
- package/dist/models/AppView.js +14 -0
- package/dist/models/ArtifactVersionResponse.d.ts +94 -0
- package/dist/models/ArtifactVersionResponse.js +88 -0
- package/dist/models/AttachedAgent.d.ts +44 -0
- package/dist/models/AttachedAgent.js +56 -0
- package/dist/models/Auth.d.ts +30 -0
- package/dist/models/Auth.js +58 -0
- package/dist/models/BindCustomApiBody.d.ts +32 -0
- package/dist/models/BindCustomApiBody.js +50 -0
- package/dist/models/ConversationArtifact.d.ts +14 -0
- package/dist/models/ConversationArtifact.js +10 -0
- package/dist/models/CustomApi.d.ts +102 -0
- package/dist/models/CustomApi.js +98 -0
- package/dist/models/CustomApiBasicAuth.d.ts +51 -0
- package/dist/models/CustomApiBasicAuth.js +65 -0
- package/dist/models/CustomApiBearerAuth.d.ts +45 -0
- package/dist/models/CustomApiBearerAuth.js +61 -0
- package/dist/models/CustomApiCreate.d.ts +57 -0
- package/dist/models/CustomApiCreate.js +63 -0
- package/dist/models/CustomApiHeader.d.ts +44 -0
- package/dist/models/CustomApiHeader.js +54 -0
- package/dist/models/CustomApiHeadersAuth.d.ts +46 -0
- package/dist/models/CustomApiHeadersAuth.js +62 -0
- package/dist/models/CustomApiList.d.ts +64 -0
- package/dist/models/CustomApiList.js +71 -0
- package/dist/models/CustomApiName.d.ts +44 -0
- package/dist/models/CustomApiName.js +56 -0
- package/dist/models/CustomApiUpdate.d.ts +58 -0
- package/dist/models/CustomApiUpdate.js +57 -0
- package/dist/models/CustomApiUpdateAuth.d.ts +30 -0
- package/dist/models/CustomApiUpdateAuth.js +58 -0
- package/dist/models/ErrorDetailItem.d.ts +5 -0
- package/dist/models/ListModels403Response.d.ts +23 -0
- package/dist/models/ListModels403Response.js +51 -0
- package/dist/models/ModelEndpointView.d.ts +38 -0
- package/dist/models/ModelEndpointView.js +52 -0
- package/dist/models/ModelView.d.ts +69 -0
- package/dist/models/ModelView.js +63 -0
- package/dist/models/ModelsResponse.d.ts +33 -0
- package/dist/models/ModelsResponse.js +51 -0
- package/dist/models/SettingsIssueView.d.ts +44 -0
- package/dist/models/SettingsIssueView.js +58 -0
- package/dist/models/VersionCommitBody.d.ts +44 -0
- package/dist/models/VersionCommitBody.js +58 -0
- package/dist/models/VersionUploadUrlBody.d.ts +32 -0
- package/dist/models/VersionUploadUrlBody.js +50 -0
- package/dist/models/index.d.ts +28 -0
- package/dist/models/index.js +28 -0
- package/package.json +15 -2
- package/src/apis/AgentConversationsApi.ts +172 -3
- package/src/apis/AgentsApi.ts +242 -7
- package/src/apis/AppsApi.ts +174 -3
- package/src/apis/ArtifactsApi.ts +144 -0
- package/src/apis/ClarifyApi.ts +3 -3
- package/src/apis/CollectionsApi.ts +3 -3
- package/src/apis/ConversationsApi.ts +3 -3
- package/src/apis/CronsApi.ts +3 -3
- package/src/apis/CustomAPIsApi.ts +471 -0
- package/src/apis/MessagesApi.ts +3 -3
- package/src/apis/ModelsApi.ts +70 -0
- package/src/apis/QueueApi.ts +3 -3
- package/src/apis/ResponsesApi.ts +3 -3
- package/src/apis/SkillsApi.ts +3 -3
- package/src/apis/index.ts +2 -0
- package/src/compose/agent.ts +254 -0
- package/src/compose/builtins.ts +19 -0
- package/src/compose/deploy.ts +168 -0
- package/src/compose/errors.ts +66 -0
- package/src/compose/events.ts +122 -0
- package/src/compose/fingerprint.ts +74 -0
- package/src/compose/index.ts +13 -0
- package/src/compose/run.ts +601 -0
- package/src/compose/runState.ts +95 -0
- package/src/compose/settingsTransport.ts +299 -0
- package/src/compose/tools.ts +42 -0
- package/src/compose/transport.ts +48 -0
- package/src/models/AgentChannelRef.ts +2 -1
- package/src/models/AgentConversationDetail.ts +21 -5
- package/src/models/AgentConversationView.ts +15 -8
- package/src/models/AgentCreate.ts +8 -0
- package/src/models/AgentCustomApiList.ts +74 -0
- package/src/models/AgentResource.ts +16 -0
- package/src/models/AgentSettingsResponse.ts +99 -0
- package/src/models/AgentUpdate.ts +8 -0
- package/src/models/AppAccountView.ts +143 -0
- package/src/models/AppAccountsView.ts +118 -0
- package/src/models/AppResource.ts +48 -0
- package/src/models/AppResourceCheckView.ts +117 -0
- package/src/models/AppResourceView.ts +120 -0
- package/src/models/AppResourcesView.ts +102 -0
- package/src/models/AppToolsResponse.ts +16 -0
- package/src/models/AppView.ts +56 -0
- package/src/models/ArtifactVersionResponse.ts +155 -0
- package/src/models/AttachedAgent.ts +83 -0
- package/src/models/Auth.ts +84 -0
- package/src/models/BindCustomApiBody.ts +66 -0
- package/src/models/ConversationArtifact.ts +19 -0
- package/src/models/CustomApi.ts +175 -0
- package/src/models/CustomApiBasicAuth.ts +94 -0
- package/src/models/CustomApiBearerAuth.ts +85 -0
- package/src/models/CustomApiCreate.ts +108 -0
- package/src/models/CustomApiHeader.ts +82 -0
- package/src/models/CustomApiHeadersAuth.ts +93 -0
- package/src/models/CustomApiList.ts +120 -0
- package/src/models/CustomApiName.ts +83 -0
- package/src/models/CustomApiUpdate.ts +106 -0
- package/src/models/CustomApiUpdateAuth.ts +84 -0
- package/src/models/ErrorDetailItem.ts +5 -0
- package/src/models/ListModels403Response.ts +73 -0
- package/src/models/ModelEndpointView.ts +74 -0
- package/src/models/ModelView.ts +122 -0
- package/src/models/ModelsResponse.ts +74 -0
- package/src/models/SettingsIssueView.ts +84 -0
- package/src/models/VersionCommitBody.ts +84 -0
- package/src/models/VersionUploadUrlBody.ts +66 -0
- package/src/models/index.ts +28 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { NotSupportedError, UserError } from "./errors.js";
|
|
2
|
+
import { settingsFingerprint } from "./fingerprint.js";
|
|
3
|
+
import { BUILTIN_NAMES, checkWebSearchOptions } from "./builtins.js";
|
|
4
|
+
import { BuiltinTool } from "./tools.js";
|
|
5
|
+
|
|
6
|
+
const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
7
|
+
const REASONING = ["off", "none", "low", "medium", "high", "xhigh", "max"];
|
|
8
|
+
const TOOL_CHOICES = ["auto", "none"];
|
|
9
|
+
|
|
10
|
+
export interface ModelSettings {
|
|
11
|
+
temperature?: number;
|
|
12
|
+
topP?: number;
|
|
13
|
+
maxTokens?: number;
|
|
14
|
+
toolChoice?: "auto" | "none";
|
|
15
|
+
reasoning?: { effort: "off" | "none" | "low" | "medium" | "high" | "xhigh" | "max" };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Limits {
|
|
19
|
+
maxTurns?: number;
|
|
20
|
+
maxToolCallsPerTurn?: number;
|
|
21
|
+
maxTokensPerTurn?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AgentOptions {
|
|
25
|
+
name: string;
|
|
26
|
+
instructions?: string;
|
|
27
|
+
model?: string;
|
|
28
|
+
/** Catalog endpoint to serve `model` from (agent.json `endpoint`). */
|
|
29
|
+
endpoint?: string;
|
|
30
|
+
modelSettings?: ModelSettings;
|
|
31
|
+
fallbackModels?: string[];
|
|
32
|
+
/** Exact list of built-ins to keep on; omit to keep the server default (all on). */
|
|
33
|
+
tools?: BuiltinTool[];
|
|
34
|
+
limits?: Limits;
|
|
35
|
+
metadata?: Record<string, string>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function deepFreeze<T>(value: T): T {
|
|
39
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
40
|
+
Object.freeze(value);
|
|
41
|
+
for (const key of Object.keys(value as object)) deepFreeze((value as Record<string, unknown>)[key]);
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function clone<T>(value: T): T {
|
|
47
|
+
return value === undefined ? value : (JSON.parse(JSON.stringify(value)) as T);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isPositiveInt(n: unknown): boolean {
|
|
51
|
+
return typeof n === "number" && Number.isInteger(n) && n >= 1;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isFiniteNumber(n: unknown): boolean {
|
|
55
|
+
return typeof n === "number" && Number.isFinite(n);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Runs on the raw options, before the JSON clone turns NaN/Infinity into null. */
|
|
59
|
+
function checkNumbers(opts: AgentOptions): void {
|
|
60
|
+
const ms = opts.modelSettings;
|
|
61
|
+
if (ms !== undefined && ms !== null) {
|
|
62
|
+
if (ms.temperature !== undefined && !(isFiniteNumber(ms.temperature) && ms.temperature >= 0 && ms.temperature <= 2)) {
|
|
63
|
+
throw new UserError("modelSettings.temperature: must be a number between 0 and 2");
|
|
64
|
+
}
|
|
65
|
+
if (ms.topP !== undefined && !(isFiniteNumber(ms.topP) && ms.topP > 0 && ms.topP <= 1)) {
|
|
66
|
+
throw new UserError("modelSettings.topP: must be a number > 0 and <= 1");
|
|
67
|
+
}
|
|
68
|
+
if (ms.maxTokens !== undefined && !isPositiveInt(ms.maxTokens)) {
|
|
69
|
+
throw new UserError("modelSettings.maxTokens: must be an integer >= 1");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const limits = opts.limits;
|
|
73
|
+
if (limits !== undefined && limits !== null) {
|
|
74
|
+
for (const key of ["maxTurns", "maxToolCallsPerTurn", "maxTokensPerTurn"] as (keyof Limits)[]) {
|
|
75
|
+
if (limits[key] !== undefined && !isPositiveInt(limits[key])) {
|
|
76
|
+
throw new UserError(`limits.${key}: must be an integer >= 1`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Drops null-valued own keys (shallow copy); a non-object (null/undefined) becomes undefined. */
|
|
83
|
+
function dropNulls<T extends object>(value: T | null | undefined): T | undefined {
|
|
84
|
+
if (value === null || value === undefined || typeof value !== "object") return undefined;
|
|
85
|
+
const out: Record<string, unknown> = {};
|
|
86
|
+
for (const key of Object.keys(value)) {
|
|
87
|
+
const v = (value as Record<string, unknown>)[key];
|
|
88
|
+
if (v !== null && v !== undefined) out[key] = v;
|
|
89
|
+
}
|
|
90
|
+
return out as T;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** JS callers spreading JSON configs pass null for unset fields; null means unset everywhere (like Python's None). */
|
|
94
|
+
function normalize(raw: AgentOptions): AgentOptions {
|
|
95
|
+
const opts = dropNulls(raw) as AgentOptions;
|
|
96
|
+
if (opts.modelSettings !== undefined) opts.modelSettings = dropNulls(opts.modelSettings);
|
|
97
|
+
if (opts.limits !== undefined) opts.limits = dropNulls(opts.limits);
|
|
98
|
+
return opts;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const LIMIT_KEYS: [keyof Limits, string][] = [
|
|
102
|
+
["maxTurns", "max_turns"],
|
|
103
|
+
["maxToolCallsPerTurn", "max_tool_calls_per_turn"],
|
|
104
|
+
["maxTokensPerTurn", "max_tokens_per_turn"],
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
export class Agent {
|
|
108
|
+
readonly name: string;
|
|
109
|
+
readonly instructions: string;
|
|
110
|
+
readonly model?: string;
|
|
111
|
+
readonly endpoint?: string;
|
|
112
|
+
readonly modelSettings?: ModelSettings;
|
|
113
|
+
readonly fallbackModels?: string[];
|
|
114
|
+
readonly tools?: readonly BuiltinTool[];
|
|
115
|
+
readonly limits?: Limits;
|
|
116
|
+
readonly metadata?: Record<string, string>;
|
|
117
|
+
|
|
118
|
+
constructor(rawOpts: AgentOptions) {
|
|
119
|
+
const opts = normalize(rawOpts);
|
|
120
|
+
for (const tool of opts.tools ?? []) {
|
|
121
|
+
if (tool instanceof Agent) {
|
|
122
|
+
throw new NotSupportedError("sub-agents in tools are not supported yet (field: tools)");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
checkNumbers(opts);
|
|
126
|
+
this.name = opts.name;
|
|
127
|
+
this.instructions = opts.instructions ?? "";
|
|
128
|
+
this.model = opts.model;
|
|
129
|
+
this.endpoint = opts.endpoint;
|
|
130
|
+
this.modelSettings = clone(opts.modelSettings);
|
|
131
|
+
this.fallbackModels = clone(opts.fallbackModels);
|
|
132
|
+
this.tools = clone(opts.tools);
|
|
133
|
+
this.limits = clone(opts.limits);
|
|
134
|
+
this.metadata = clone(opts.metadata);
|
|
135
|
+
this.check();
|
|
136
|
+
deepFreeze(this);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private check(): void {
|
|
140
|
+
if (typeof this.name !== "string" || !SLUG.test(this.name)) {
|
|
141
|
+
throw new UserError(`name: ${JSON.stringify(this.name)} must match ${SLUG.source}`);
|
|
142
|
+
}
|
|
143
|
+
for (const [key, value] of [["model", this.model], ["endpoint", this.endpoint]] as [string, unknown][]) {
|
|
144
|
+
if (value !== undefined && (typeof value !== "string" || value === "")) {
|
|
145
|
+
throw new UserError(`${key}: must be a non-empty string`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (this.endpoint !== undefined && this.model === undefined) {
|
|
149
|
+
throw new UserError("endpoint: needs model; set the model this endpoint serves");
|
|
150
|
+
}
|
|
151
|
+
const ms = this.modelSettings;
|
|
152
|
+
if (ms !== undefined) {
|
|
153
|
+
if (ms.reasoning !== undefined && REASONING.indexOf(ms.reasoning.effort) === -1) {
|
|
154
|
+
throw new UserError(
|
|
155
|
+
`modelSettings.reasoning: ${JSON.stringify(ms.reasoning.effort)} must be one of ${REASONING.join(", ")}`);
|
|
156
|
+
}
|
|
157
|
+
if (ms.toolChoice !== undefined && TOOL_CHOICES.indexOf(ms.toolChoice) === -1) {
|
|
158
|
+
throw new UserError(
|
|
159
|
+
`modelSettings.toolChoice: ${JSON.stringify(ms.toolChoice)} must be one of ${TOOL_CHOICES.join(", ")}`);
|
|
160
|
+
}
|
|
161
|
+
if (ms.temperature !== undefined && !(ms.temperature >= 0 && ms.temperature <= 2)) {
|
|
162
|
+
throw new UserError("modelSettings.temperature: must be between 0 and 2");
|
|
163
|
+
}
|
|
164
|
+
if (ms.topP !== undefined && !(ms.topP > 0 && ms.topP <= 1)) {
|
|
165
|
+
throw new UserError("modelSettings.topP: must be > 0 and <= 1");
|
|
166
|
+
}
|
|
167
|
+
if (ms.maxTokens !== undefined && !isPositiveInt(ms.maxTokens)) {
|
|
168
|
+
throw new UserError("modelSettings.maxTokens: must be an integer >= 1");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (this.limits !== undefined) {
|
|
172
|
+
for (const [key] of LIMIT_KEYS) {
|
|
173
|
+
const value = this.limits[key];
|
|
174
|
+
if (value !== undefined && !isPositiveInt(value)) {
|
|
175
|
+
throw new UserError(`limits.${key}: must be an integer >= 1`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (this.metadata !== undefined) {
|
|
180
|
+
const bad = Object.keys(this.metadata).filter((k) => k.indexOf("sdk_") === 0).sort();
|
|
181
|
+
if (bad.length > 0) {
|
|
182
|
+
throw new UserError(`metadata: keys starting with 'sdk_' are reserved (${bad.join(", ")})`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const names = (this.tools ?? []).map((t) => t.name);
|
|
186
|
+
for (const n of names) {
|
|
187
|
+
if ((BUILTIN_NAMES as readonly string[]).indexOf(n) === -1) {
|
|
188
|
+
throw new UserError(`tools: unknown built-in tool ${JSON.stringify(n)} (known: ${BUILTIN_NAMES.join(", ")})`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const dupes = Array.from(new Set(names.filter((n, i) => names.indexOf(n) !== i))).sort();
|
|
192
|
+
if (dupes.length > 0) {
|
|
193
|
+
throw new UserError(`tools: duplicate built-in tool(s): ${dupes.join(", ")}`);
|
|
194
|
+
}
|
|
195
|
+
for (const t of this.tools ?? []) {
|
|
196
|
+
if (t.options === undefined || Object.keys(t.options).length === 0) continue;
|
|
197
|
+
if (t.name !== "web_search") throw new UserError(`tools: ${t.name} takes no options`);
|
|
198
|
+
checkWebSearchOptions(t.options);
|
|
199
|
+
}
|
|
200
|
+
if (names.indexOf("code_mode") !== -1 && names.indexOf("sandbox") === -1) {
|
|
201
|
+
throw new UserError("tools: codeMode needs sandbox; add sandbox() to tools");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private baseSettings(): Record<string, unknown> {
|
|
206
|
+
const out: Record<string, unknown> = { version: 1 };
|
|
207
|
+
if (this.model !== undefined) out["model"] = this.model;
|
|
208
|
+
if (this.endpoint !== undefined) out["endpoint"] = this.endpoint;
|
|
209
|
+
const ms = this.modelSettings;
|
|
210
|
+
if (ms !== undefined) {
|
|
211
|
+
if (ms.reasoning !== undefined) out["reasoning"] = ms.reasoning.effort;
|
|
212
|
+
if (ms.temperature !== undefined) out["temperature"] = ms.temperature;
|
|
213
|
+
if (ms.topP !== undefined) out["top_p"] = ms.topP;
|
|
214
|
+
if (ms.maxTokens !== undefined) out["max_output_tokens"] = ms.maxTokens;
|
|
215
|
+
if (ms.toolChoice !== undefined) out["tool_choice"] = ms.toolChoice;
|
|
216
|
+
}
|
|
217
|
+
if (this.fallbackModels !== undefined && this.fallbackModels.length > 0) {
|
|
218
|
+
out["fallback"] = clone(this.fallbackModels);
|
|
219
|
+
}
|
|
220
|
+
if (this.limits !== undefined) {
|
|
221
|
+
const limits: Record<string, number> = {};
|
|
222
|
+
for (const [key, wire] of LIMIT_KEYS) {
|
|
223
|
+
const value = this.limits[key];
|
|
224
|
+
if (value !== undefined) limits[wire] = value;
|
|
225
|
+
}
|
|
226
|
+
if (Object.keys(limits).length > 0) out["limits"] = limits;
|
|
227
|
+
}
|
|
228
|
+
if (this.tools !== undefined) {
|
|
229
|
+
// Built-ins are on by default, so an exact list writes every unlisted one as off.
|
|
230
|
+
const tools: Record<string, unknown> = {};
|
|
231
|
+
for (const n of BUILTIN_NAMES) {
|
|
232
|
+
const t = this.tools.find((x) => x.name === n);
|
|
233
|
+
tools[n] = t === undefined ? false
|
|
234
|
+
: t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
|
|
235
|
+
}
|
|
236
|
+
out["builtin_tools"] = tools;
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
specHash(): string {
|
|
242
|
+
const settings = this.baseSettings();
|
|
243
|
+
if (this.metadata !== undefined && Object.keys(this.metadata).length > 0) {
|
|
244
|
+
settings["metadata"] = { ...this.metadata };
|
|
245
|
+
}
|
|
246
|
+
return settingsFingerprint(this.instructions, settings);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
toSettings(): Record<string, unknown> {
|
|
250
|
+
const out = this.baseSettings();
|
|
251
|
+
out["metadata"] = { ...(this.metadata ?? {}), sdk_name: this.name, sdk_hash: this.specHash() };
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Internal: built-in tool names and checks shared by tools.ts and agent.ts. Not re-exported from index.ts. */
|
|
2
|
+
import { UserError } from "./errors.js";
|
|
3
|
+
|
|
4
|
+
/** The agent.json builtin_tools switches, in schema order. */
|
|
5
|
+
export const BUILTIN_NAMES = ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"] as const;
|
|
6
|
+
|
|
7
|
+
/** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
|
|
8
|
+
export function checkWebSearchOptions(options: Record<string, unknown>): void {
|
|
9
|
+
const keys = Object.keys(options);
|
|
10
|
+
for (const k of keys) {
|
|
11
|
+
if (k !== "allowed_domains" && k !== "blocked_domains") throw new UserError(`tools.web_search: unknown option ${JSON.stringify(k)}`);
|
|
12
|
+
const v = options[k];
|
|
13
|
+
if (!Array.isArray(v) || v.length === 0 || v.some((d) => typeof d !== "string")) {
|
|
14
|
+
throw new UserError(`tools.web_search.${k}: must be a non-empty list of domains`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (keys.length > 1) throw new UserError("tools.web_search: give allowed_domains or blocked_domains, not both");
|
|
18
|
+
}
|
|
19
|
+
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/** deploy()/undeploy(): upsert a hosted agent by name, with fingerprint-based drift detection, and the deploy cache. */
|
|
2
|
+
import type { Configuration } from "../runtime.js";
|
|
3
|
+
import type { Agent } from "./agent.js";
|
|
4
|
+
import { DriftError, DuplicateAgentsError, UserError } from "./errors.js";
|
|
5
|
+
import { canonicalJson, fingerprintBody, settingsFingerprint } from "./fingerprint.js";
|
|
6
|
+
import { SettingsTransport } from "./settingsTransport.js";
|
|
7
|
+
import type { RemoteAgent, Transport } from "./transport.js";
|
|
8
|
+
|
|
9
|
+
export interface Deployment {
|
|
10
|
+
agentId: string;
|
|
11
|
+
action: "created" | "updated" | "unchanged";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let defaultClient: Configuration | null = null;
|
|
15
|
+
|
|
16
|
+
/** Set (or clear, with null) the client used when deploy/undeploy/run get no client. */
|
|
17
|
+
export function setDefaultClient(config: Configuration | null): void {
|
|
18
|
+
defaultClient = config;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getDefaultClient(): Configuration | null {
|
|
22
|
+
return defaultClient;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveTransport(client?: Configuration, transport?: Transport): Transport {
|
|
26
|
+
if (transport) return transport;
|
|
27
|
+
const config = client ?? defaultClient;
|
|
28
|
+
if (!config) {
|
|
29
|
+
throw new UserError("no client configured: pass client (or transport) or call setDefaultClient(config)");
|
|
30
|
+
}
|
|
31
|
+
return new SettingsTransport(config);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function storedHash(remote: RemoteAgent): string | undefined {
|
|
35
|
+
const metadata = remote.settings["metadata"] as { sdk_hash?: unknown } | null | undefined;
|
|
36
|
+
const h = metadata ? metadata.sdk_hash : undefined;
|
|
37
|
+
return typeof h === "string" ? h : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function changedFields(remote: RemoteAgent, agent: Agent): string[] {
|
|
41
|
+
const want = fingerprintBody(agent.toSettings());
|
|
42
|
+
const have = fingerprintBody(remote.settings);
|
|
43
|
+
const keys: string[] = [];
|
|
44
|
+
const all = new Set<string>([...Object.keys(want), ...Object.keys(have)]);
|
|
45
|
+
all.forEach((k) => {
|
|
46
|
+
// like Python's want.get(k) != have.get(k): a missing key equals null
|
|
47
|
+
const a = canonicalJson(want[k] === undefined ? null : want[k]);
|
|
48
|
+
const b = canonicalJson(have[k] === undefined ? null : have[k]);
|
|
49
|
+
if (a !== b) keys.push(k);
|
|
50
|
+
});
|
|
51
|
+
keys.sort();
|
|
52
|
+
if (remote.instructions !== agent.instructions) keys.push("instructions");
|
|
53
|
+
return keys;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function update(transport: Transport, remote: RemoteAgent, agent: Agent, strict: boolean): Promise<Deployment> {
|
|
57
|
+
const stored = storedHash(remote);
|
|
58
|
+
if (stored !== undefined && settingsFingerprint(remote.instructions, remote.settings) !== stored) {
|
|
59
|
+
const changed = changedFields(remote, agent);
|
|
60
|
+
if (strict) throw new DriftError(changed);
|
|
61
|
+
console.warn(
|
|
62
|
+
`agent "${agent.name}" (${remote.agentId}) was edited outside the SDK; overwriting fields: ${changed.join(", ")}`,
|
|
63
|
+
);
|
|
64
|
+
} else if (stored === agent.specHash()) {
|
|
65
|
+
return { agentId: remote.agentId, action: "unchanged" };
|
|
66
|
+
}
|
|
67
|
+
await transport.updateAgent(remote.agentId, agent);
|
|
68
|
+
return { agentId: remote.agentId, action: "updated" };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function deployWith(agent: Agent, transport: Transport, strict: boolean, adopt?: string): Promise<Deployment> {
|
|
72
|
+
const found = await transport.findAgents(agent.name);
|
|
73
|
+
if (adopt !== undefined) {
|
|
74
|
+
const target = await transport.readAgent(adopt);
|
|
75
|
+
const metadata = target.settings["metadata"] as { sdk_name?: unknown } | null | undefined;
|
|
76
|
+
const owner = metadata ? metadata.sdk_name : undefined;
|
|
77
|
+
if (owner !== undefined && owner !== null && owner !== agent.name) {
|
|
78
|
+
throw new UserError(`agent ${adopt} is managed by SDK agent "${owner}", cannot adopt it as "${agent.name}"`);
|
|
79
|
+
}
|
|
80
|
+
const others = found.filter((r) => r.agentId !== adopt).map((r) => r.agentId);
|
|
81
|
+
if (others.length) {
|
|
82
|
+
throw new UserError(`cannot adopt ${adopt}: agent "${agent.name}" is already deployed as ${others.join(", ")}`);
|
|
83
|
+
}
|
|
84
|
+
await transport.updateAgent(adopt, agent);
|
|
85
|
+
return { agentId: adopt, action: "updated" };
|
|
86
|
+
}
|
|
87
|
+
if (found.length > 1) throw new DuplicateAgentsError(found.map((r) => r.agentId));
|
|
88
|
+
if (found.length === 0) return { agentId: (await transport.createAgent(agent)).agentId, action: "created" };
|
|
89
|
+
return update(transport, found[0], agent, strict);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function deploy(
|
|
93
|
+
agent: Agent,
|
|
94
|
+
opts: { client?: Configuration; transport?: Transport; strict?: boolean; adopt?: string } = {},
|
|
95
|
+
): Promise<Deployment> {
|
|
96
|
+
const transport = resolveTransport(opts.client, opts.transport);
|
|
97
|
+
forget(agent.name); // this deploy may replace what cached runs of the name point at
|
|
98
|
+
return serialized(agent.name, () => deployWith(agent, transport, opts.strict ?? false, opts.adopt));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function undeploy(
|
|
102
|
+
name: string,
|
|
103
|
+
opts: { client?: Configuration; transport?: Transport } = {},
|
|
104
|
+
): Promise<boolean> {
|
|
105
|
+
const transport = resolveTransport(opts.client, opts.transport);
|
|
106
|
+
forget(name);
|
|
107
|
+
return serialized(name, async () => {
|
|
108
|
+
const found = await transport.findAgents(name);
|
|
109
|
+
for (const remote of found) await transport.deleteAgent(remote.agentId);
|
|
110
|
+
return found.length > 0;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Per-name tail: every deploy/undeploy of a name (cached or direct) queues behind the previous one, so they never
|
|
115
|
+
// overlap. forget() does not touch it.
|
|
116
|
+
const tails = new Map<string, Promise<unknown>>();
|
|
117
|
+
|
|
118
|
+
function serialized<T>(name: string, fn: () => Promise<T>): Promise<T> {
|
|
119
|
+
const prev = tails.get(name);
|
|
120
|
+
const p = prev ? prev.then(fn) : fn();
|
|
121
|
+
const tail = p.then(
|
|
122
|
+
() => undefined,
|
|
123
|
+
() => undefined,
|
|
124
|
+
);
|
|
125
|
+
tails.set(name, tail);
|
|
126
|
+
tail.then(() => {
|
|
127
|
+
if (tails.get(name) === tail) tails.delete(name);
|
|
128
|
+
});
|
|
129
|
+
return p;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// -- deploy cache: one deploy per (owner, name, spec hash) per process --------------------------------------------
|
|
133
|
+
|
|
134
|
+
interface Entry {
|
|
135
|
+
hash: string;
|
|
136
|
+
id: Promise<string>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const cache = new Map<string, WeakMap<object, Entry>>();
|
|
140
|
+
|
|
141
|
+
/** Drop cached deployments of agent `name` (any owner); called by deploy() and undeploy(). */
|
|
142
|
+
export function forget(name: string): void {
|
|
143
|
+
cache.delete(name);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The server id for `agent`, deploying it first unless this owner already deployed this exact definition.
|
|
148
|
+
* Keyed by name, as the server is: a variant with the same name redeploys, chained after any in-flight deploy so
|
|
149
|
+
* deploys of one name never overlap.
|
|
150
|
+
*/
|
|
151
|
+
export function agentIdFor(agent: Agent, transport: Transport, owner: object): Promise<string> {
|
|
152
|
+
const hash = agent.specHash();
|
|
153
|
+
let owners = cache.get(agent.name);
|
|
154
|
+
if (!owners) {
|
|
155
|
+
owners = new WeakMap();
|
|
156
|
+
cache.set(agent.name, owners);
|
|
157
|
+
}
|
|
158
|
+
const prev = owners.get(owner);
|
|
159
|
+
if (prev && prev.hash === hash) return prev.id;
|
|
160
|
+
const id = serialized(agent.name, () => deployWith(agent, transport, false)).then((d) => d.agentId);
|
|
161
|
+
const entry: Entry = { hash, id };
|
|
162
|
+
owners.set(owner, entry);
|
|
163
|
+
const slot = owners;
|
|
164
|
+
id.catch(() => {
|
|
165
|
+
if (slot.get(owner) === entry) slot.delete(owner);
|
|
166
|
+
});
|
|
167
|
+
return id;
|
|
168
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Error hierarchy for the compose layer. */
|
|
2
|
+
|
|
3
|
+
export class AgentsError extends Error {
|
|
4
|
+
constructor(message?: string) {
|
|
5
|
+
super(message);
|
|
6
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
7
|
+
this.name = new.target.name;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The caller supplied an invalid value. */
|
|
12
|
+
export class UserError extends AgentsError {
|
|
13
|
+
/** The server's `{ code, path, message }` issues when it rejected agent settings. */
|
|
14
|
+
readonly issues: Array<{ code?: string; path?: string; message?: string }>;
|
|
15
|
+
|
|
16
|
+
constructor(message?: string, issues: Array<{ code?: string; path?: string; message?: string }> = []) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.issues = issues;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class ModelBehaviorError extends AgentsError {}
|
|
23
|
+
|
|
24
|
+
export class MaxTurnsExceededError extends AgentsError {}
|
|
25
|
+
|
|
26
|
+
export class MaxTokensExceededError extends AgentsError {}
|
|
27
|
+
|
|
28
|
+
export class MaxToolCallsExceededError extends AgentsError {}
|
|
29
|
+
|
|
30
|
+
/** The agent has not been deployed. */
|
|
31
|
+
export class NotDeployedError extends AgentsError {}
|
|
32
|
+
|
|
33
|
+
/** A feature the SDK does not support yet. */
|
|
34
|
+
export class NotSupportedError extends AgentsError {}
|
|
35
|
+
|
|
36
|
+
export class RunError extends AgentsError {
|
|
37
|
+
readonly code: string;
|
|
38
|
+
constructor(code: string, message: string) {
|
|
39
|
+
super(`${code}: ${message}`);
|
|
40
|
+
this.code = code;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class DriftError extends AgentsError {
|
|
45
|
+
readonly changed: string[];
|
|
46
|
+
constructor(changed: string[]) {
|
|
47
|
+
super("deployed agent drifted: " + changed.join(", "));
|
|
48
|
+
this.changed = changed.slice();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class DuplicateAgentsError extends AgentsError {
|
|
53
|
+
readonly agentIds: string[];
|
|
54
|
+
constructor(agentIds: string[]) {
|
|
55
|
+
super("duplicate agents: " + agentIds.join(", "));
|
|
56
|
+
this.agentIds = agentIds.slice();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class NotSupportedByServerError extends AgentsError {
|
|
61
|
+
readonly features: string[];
|
|
62
|
+
constructor(features: string[]) {
|
|
63
|
+
super("server does not support: " + features.join(", "));
|
|
64
|
+
this.features = features.slice();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/** Stream event model for `/v1/responses` UI-message-stream chunks. */
|
|
2
|
+
|
|
3
|
+
export interface ClarifyQuestion {
|
|
4
|
+
id: string;
|
|
5
|
+
prompt: string;
|
|
6
|
+
inputType?: string;
|
|
7
|
+
choices?: string[];
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface FunctionCallRequest {
|
|
12
|
+
callId: string;
|
|
13
|
+
name: string;
|
|
14
|
+
arguments: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Event =
|
|
18
|
+
| { kind: "text_start"; id: string }
|
|
19
|
+
| { kind: "text_delta"; delta: string; id?: string }
|
|
20
|
+
| { kind: "text_end"; id: string }
|
|
21
|
+
| { kind: "reasoning_delta"; delta: string }
|
|
22
|
+
| { kind: "tool_called"; callId: string; name: string; arguments: unknown }
|
|
23
|
+
| { kind: "tool_output"; callId: string; output: unknown }
|
|
24
|
+
| { kind: "clarify_requested"; requestId: string; questions: ClarifyQuestion[] }
|
|
25
|
+
| { kind: "function_calls_requested"; requestId: string; calls: FunctionCallRequest[]; expiresAt?: string }
|
|
26
|
+
| { kind: "stopped"; reason: string }
|
|
27
|
+
| { kind: "turn_finished"; reason: string }
|
|
28
|
+
| { kind: "turn_failed"; code: string; message: string }
|
|
29
|
+
| { kind: "unknown"; chunk: Record<string, unknown> };
|
|
30
|
+
|
|
31
|
+
const IGNORED = ["start", "start-step", "finish-step", "tool-input-start", "tool-input-delta"];
|
|
32
|
+
|
|
33
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
34
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function str(v: unknown, fallback: string): string {
|
|
38
|
+
return typeof v === "string" && v !== "" ? v : fallback;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function text(v: unknown): string {
|
|
42
|
+
return typeof v === "string" ? v : "";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Map one stream chunk to an Event; `null` for chunks the runner ignores.
|
|
47
|
+
* Never throws: malformed chunks degrade to `{ kind: "unknown", chunk }`.
|
|
48
|
+
*/
|
|
49
|
+
export function fromUiChunk(chunk: unknown): Event | null {
|
|
50
|
+
if (!isObject(chunk)) return { kind: "unknown", chunk: chunk as Record<string, unknown> };
|
|
51
|
+
try {
|
|
52
|
+
return map(chunk);
|
|
53
|
+
} catch {
|
|
54
|
+
return { kind: "unknown", chunk };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function map(chunk: Record<string, unknown>): Event | null {
|
|
59
|
+
const type = chunk["type"];
|
|
60
|
+
const data = isObject(chunk["data"]) ? (chunk["data"] as Record<string, unknown>) : {};
|
|
61
|
+
if (typeof type === "string" && IGNORED.indexOf(type) !== -1) return null;
|
|
62
|
+
switch (type) {
|
|
63
|
+
case "text-start":
|
|
64
|
+
return { kind: "text_start", id: text(chunk["id"]) };
|
|
65
|
+
case "text-delta": {
|
|
66
|
+
const ev: Event = { kind: "text_delta", delta: text(chunk["delta"]) };
|
|
67
|
+
if (typeof chunk["id"] === "string") ev.id = chunk["id"];
|
|
68
|
+
return ev;
|
|
69
|
+
}
|
|
70
|
+
case "text-end":
|
|
71
|
+
return { kind: "text_end", id: text(chunk["id"]) };
|
|
72
|
+
case "reasoning-delta":
|
|
73
|
+
return { kind: "reasoning_delta", delta: text(chunk["delta"]) };
|
|
74
|
+
case "tool-input-available":
|
|
75
|
+
return {
|
|
76
|
+
kind: "tool_called",
|
|
77
|
+
callId: text(chunk["toolCallId"]),
|
|
78
|
+
name: text(chunk["toolName"]),
|
|
79
|
+
arguments: chunk["input"],
|
|
80
|
+
};
|
|
81
|
+
case "tool-output-available":
|
|
82
|
+
return { kind: "tool_output", callId: text(chunk["toolCallId"]), output: chunk["output"] };
|
|
83
|
+
case "data-clarify-request": {
|
|
84
|
+
const questions = data["questions"] ?? [];
|
|
85
|
+
if (!Array.isArray(questions)) throw new TypeError("questions must be a list");
|
|
86
|
+
return {
|
|
87
|
+
kind: "clarify_requested",
|
|
88
|
+
requestId: str(data["requestId"], text(chunk["id"])),
|
|
89
|
+
questions: questions as ClarifyQuestion[],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
case "data-function-call-request": {
|
|
93
|
+
const raw = data["calls"] ?? [];
|
|
94
|
+
if (!Array.isArray(raw)) throw new TypeError("calls must be a list");
|
|
95
|
+
const calls = raw.map((c): FunctionCallRequest => {
|
|
96
|
+
if (!isObject(c)) throw new TypeError("call must be an object");
|
|
97
|
+
const args = c["arguments"];
|
|
98
|
+
return {
|
|
99
|
+
callId: text(c["toolCallId"]),
|
|
100
|
+
name: text(c["name"]),
|
|
101
|
+
arguments: isObject(args) ? args : {},
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
const ev: Event = {
|
|
105
|
+
kind: "function_calls_requested",
|
|
106
|
+
requestId: str(data["requestId"], text(chunk["id"])),
|
|
107
|
+
calls,
|
|
108
|
+
};
|
|
109
|
+
if (typeof data["expiresAt"] === "string") ev.expiresAt = data["expiresAt"];
|
|
110
|
+
return ev;
|
|
111
|
+
}
|
|
112
|
+
case "data-stopped":
|
|
113
|
+
return { kind: "stopped", reason: str(data["reason"], "stopped") };
|
|
114
|
+
case "error":
|
|
115
|
+
return { kind: "turn_failed", code: str(chunk["code"], "turn_failed"), message: text(chunk["errorText"]) };
|
|
116
|
+
case "finish":
|
|
117
|
+
return { kind: "turn_finished", reason: str(chunk["finishReason"], "stop") };
|
|
118
|
+
default:
|
|
119
|
+
// data-output and data-output-error are unknown until structured output lands.
|
|
120
|
+
return { kind: "unknown", chunk };
|
|
121
|
+
}
|
|
122
|
+
}
|