sarvam-agents-client 0.1.2 → 0.2.0
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 +40 -0
- package/dist/compose/agent.js +191 -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 +29 -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 +40 -0
- package/dist/esm/compose/agent.js +187 -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 +19 -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 +223 -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 +27 -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,223 @@
|
|
|
1
|
+
import { NotSupportedError, UserError } from "./errors.js";
|
|
2
|
+
import { settingsFingerprint } from "./fingerprint.js";
|
|
3
|
+
import { BuiltinTool } from "./tools.js";
|
|
4
|
+
|
|
5
|
+
const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
6
|
+
const REASONING = ["off", "none", "low", "medium", "high", "xhigh", "max"];
|
|
7
|
+
const TOOL_CHOICES = ["auto", "none"];
|
|
8
|
+
|
|
9
|
+
export interface ModelSettings {
|
|
10
|
+
temperature?: number;
|
|
11
|
+
topP?: number;
|
|
12
|
+
maxTokens?: number;
|
|
13
|
+
toolChoice?: "auto" | "none";
|
|
14
|
+
reasoning?: { effort: "off" | "none" | "low" | "medium" | "high" | "xhigh" | "max" };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Limits {
|
|
18
|
+
maxTurns?: number;
|
|
19
|
+
maxToolCallsPerTurn?: number;
|
|
20
|
+
maxTokensPerTurn?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface AgentOptions {
|
|
24
|
+
name: string;
|
|
25
|
+
instructions?: string;
|
|
26
|
+
model?: string;
|
|
27
|
+
modelSettings?: ModelSettings;
|
|
28
|
+
fallbackModels?: string[];
|
|
29
|
+
tools?: BuiltinTool[];
|
|
30
|
+
limits?: Limits;
|
|
31
|
+
metadata?: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function deepFreeze<T>(value: T): T {
|
|
35
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
36
|
+
Object.freeze(value);
|
|
37
|
+
for (const key of Object.keys(value as object)) deepFreeze((value as Record<string, unknown>)[key]);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function clone<T>(value: T): T {
|
|
43
|
+
return value === undefined ? value : (JSON.parse(JSON.stringify(value)) as T);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isPositiveInt(n: unknown): boolean {
|
|
47
|
+
return typeof n === "number" && Number.isInteger(n) && n >= 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isFiniteNumber(n: unknown): boolean {
|
|
51
|
+
return typeof n === "number" && Number.isFinite(n);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Runs on the raw options, before the JSON clone turns NaN/Infinity into null. */
|
|
55
|
+
function checkNumbers(opts: AgentOptions): void {
|
|
56
|
+
const ms = opts.modelSettings;
|
|
57
|
+
if (ms !== undefined && ms !== null) {
|
|
58
|
+
if (ms.temperature !== undefined && !(isFiniteNumber(ms.temperature) && ms.temperature >= 0 && ms.temperature <= 2)) {
|
|
59
|
+
throw new UserError("modelSettings.temperature: must be a number between 0 and 2");
|
|
60
|
+
}
|
|
61
|
+
if (ms.topP !== undefined && !(isFiniteNumber(ms.topP) && ms.topP > 0 && ms.topP <= 1)) {
|
|
62
|
+
throw new UserError("modelSettings.topP: must be a number > 0 and <= 1");
|
|
63
|
+
}
|
|
64
|
+
if (ms.maxTokens !== undefined && !isPositiveInt(ms.maxTokens)) {
|
|
65
|
+
throw new UserError("modelSettings.maxTokens: must be an integer >= 1");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const limits = opts.limits;
|
|
69
|
+
if (limits !== undefined && limits !== null) {
|
|
70
|
+
for (const key of ["maxTurns", "maxToolCallsPerTurn", "maxTokensPerTurn"] as (keyof Limits)[]) {
|
|
71
|
+
if (limits[key] !== undefined && !isPositiveInt(limits[key])) {
|
|
72
|
+
throw new UserError(`limits.${key}: must be an integer >= 1`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Drops null-valued own keys (shallow copy); a non-object (null/undefined) becomes undefined. */
|
|
79
|
+
function dropNulls<T extends object>(value: T | null | undefined): T | undefined {
|
|
80
|
+
if (value === null || value === undefined || typeof value !== "object") return undefined;
|
|
81
|
+
const out: Record<string, unknown> = {};
|
|
82
|
+
for (const key of Object.keys(value)) {
|
|
83
|
+
const v = (value as Record<string, unknown>)[key];
|
|
84
|
+
if (v !== null && v !== undefined) out[key] = v;
|
|
85
|
+
}
|
|
86
|
+
return out as T;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** JS callers spreading JSON configs pass null for unset fields; null means unset everywhere (like Python's None). */
|
|
90
|
+
function normalize(raw: AgentOptions): AgentOptions {
|
|
91
|
+
const opts = dropNulls(raw) as AgentOptions;
|
|
92
|
+
if (opts.modelSettings !== undefined) opts.modelSettings = dropNulls(opts.modelSettings);
|
|
93
|
+
if (opts.limits !== undefined) opts.limits = dropNulls(opts.limits);
|
|
94
|
+
return opts;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const LIMIT_KEYS: [keyof Limits, string][] = [
|
|
98
|
+
["maxTurns", "max_turns"],
|
|
99
|
+
["maxToolCallsPerTurn", "max_tool_calls_per_turn"],
|
|
100
|
+
["maxTokensPerTurn", "max_tokens_per_turn"],
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
export class Agent {
|
|
104
|
+
readonly name: string;
|
|
105
|
+
readonly instructions: string;
|
|
106
|
+
readonly model?: string;
|
|
107
|
+
readonly modelSettings?: ModelSettings;
|
|
108
|
+
readonly fallbackModels?: string[];
|
|
109
|
+
readonly tools: readonly BuiltinTool[];
|
|
110
|
+
readonly limits?: Limits;
|
|
111
|
+
readonly metadata?: Record<string, string>;
|
|
112
|
+
|
|
113
|
+
constructor(rawOpts: AgentOptions) {
|
|
114
|
+
const opts = normalize(rawOpts);
|
|
115
|
+
for (const tool of opts.tools ?? []) {
|
|
116
|
+
if (tool instanceof Agent) {
|
|
117
|
+
throw new NotSupportedError("sub-agents in tools are not supported yet (field: tools)");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
checkNumbers(opts);
|
|
121
|
+
this.name = opts.name;
|
|
122
|
+
this.instructions = opts.instructions ?? "";
|
|
123
|
+
this.model = opts.model;
|
|
124
|
+
this.modelSettings = clone(opts.modelSettings);
|
|
125
|
+
this.fallbackModels = clone(opts.fallbackModels);
|
|
126
|
+
this.tools = clone(opts.tools ?? []);
|
|
127
|
+
this.limits = clone(opts.limits);
|
|
128
|
+
this.metadata = clone(opts.metadata);
|
|
129
|
+
this.check();
|
|
130
|
+
deepFreeze(this);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private check(): void {
|
|
134
|
+
if (typeof this.name !== "string" || !SLUG.test(this.name)) {
|
|
135
|
+
throw new UserError(`name: ${JSON.stringify(this.name)} must match ${SLUG.source}`);
|
|
136
|
+
}
|
|
137
|
+
const ms = this.modelSettings;
|
|
138
|
+
if (ms !== undefined) {
|
|
139
|
+
if (ms.reasoning !== undefined && REASONING.indexOf(ms.reasoning.effort) === -1) {
|
|
140
|
+
throw new UserError(
|
|
141
|
+
`modelSettings.reasoning: ${JSON.stringify(ms.reasoning.effort)} must be one of ${REASONING.join(", ")}`);
|
|
142
|
+
}
|
|
143
|
+
if (ms.toolChoice !== undefined && TOOL_CHOICES.indexOf(ms.toolChoice) === -1) {
|
|
144
|
+
throw new UserError(
|
|
145
|
+
`modelSettings.toolChoice: ${JSON.stringify(ms.toolChoice)} must be one of ${TOOL_CHOICES.join(", ")}`);
|
|
146
|
+
}
|
|
147
|
+
if (ms.temperature !== undefined && !(ms.temperature >= 0 && ms.temperature <= 2)) {
|
|
148
|
+
throw new UserError("modelSettings.temperature: must be between 0 and 2");
|
|
149
|
+
}
|
|
150
|
+
if (ms.topP !== undefined && !(ms.topP > 0 && ms.topP <= 1)) {
|
|
151
|
+
throw new UserError("modelSettings.topP: must be > 0 and <= 1");
|
|
152
|
+
}
|
|
153
|
+
if (ms.maxTokens !== undefined && !isPositiveInt(ms.maxTokens)) {
|
|
154
|
+
throw new UserError("modelSettings.maxTokens: must be an integer >= 1");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (this.limits !== undefined) {
|
|
158
|
+
for (const [key] of LIMIT_KEYS) {
|
|
159
|
+
const value = this.limits[key];
|
|
160
|
+
if (value !== undefined && !isPositiveInt(value)) {
|
|
161
|
+
throw new UserError(`limits.${key}: must be an integer >= 1`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (this.metadata !== undefined) {
|
|
166
|
+
const bad = Object.keys(this.metadata).filter((k) => k.indexOf("sdk_") === 0).sort();
|
|
167
|
+
if (bad.length > 0) {
|
|
168
|
+
throw new UserError(`metadata: keys starting with 'sdk_' are reserved (${bad.join(", ")})`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const names = this.tools.map((t) => t.name);
|
|
172
|
+
const dupes = Array.from(new Set(names.filter((n, i) => names.indexOf(n) !== i))).sort();
|
|
173
|
+
if (dupes.length > 0) {
|
|
174
|
+
throw new UserError(`tools: duplicate built-in tool(s): ${dupes.join(", ")}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private baseSettings(): Record<string, unknown> {
|
|
179
|
+
const out: Record<string, unknown> = { version: 1 };
|
|
180
|
+
if (this.model !== undefined) out["model"] = this.model;
|
|
181
|
+
const ms = this.modelSettings;
|
|
182
|
+
if (ms !== undefined) {
|
|
183
|
+
if (ms.reasoning !== undefined) out["reasoning"] = ms.reasoning.effort;
|
|
184
|
+
if (ms.temperature !== undefined) out["temperature"] = ms.temperature;
|
|
185
|
+
if (ms.topP !== undefined) out["top_p"] = ms.topP;
|
|
186
|
+
if (ms.maxTokens !== undefined) out["max_output_tokens"] = ms.maxTokens;
|
|
187
|
+
if (ms.toolChoice !== undefined) out["tool_choice"] = ms.toolChoice;
|
|
188
|
+
}
|
|
189
|
+
if (this.fallbackModels !== undefined && this.fallbackModels.length > 0) {
|
|
190
|
+
out["fallback"] = clone(this.fallbackModels);
|
|
191
|
+
}
|
|
192
|
+
if (this.limits !== undefined) {
|
|
193
|
+
const limits: Record<string, number> = {};
|
|
194
|
+
for (const [key, wire] of LIMIT_KEYS) {
|
|
195
|
+
const value = this.limits[key];
|
|
196
|
+
if (value !== undefined) limits[wire] = value;
|
|
197
|
+
}
|
|
198
|
+
if (Object.keys(limits).length > 0) out["limits"] = limits;
|
|
199
|
+
}
|
|
200
|
+
if (this.tools.length > 0) {
|
|
201
|
+
const tools: Record<string, unknown> = {};
|
|
202
|
+
for (const t of this.tools) {
|
|
203
|
+
tools[t.name] = t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
|
|
204
|
+
}
|
|
205
|
+
out["builtin_tools"] = tools;
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
specHash(): string {
|
|
211
|
+
const settings = this.baseSettings();
|
|
212
|
+
if (this.metadata !== undefined && Object.keys(this.metadata).length > 0) {
|
|
213
|
+
settings["metadata"] = { ...this.metadata };
|
|
214
|
+
}
|
|
215
|
+
return settingsFingerprint(this.instructions, settings);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
toSettings(): Record<string, unknown> {
|
|
219
|
+
const out = this.baseSettings();
|
|
220
|
+
out["metadata"] = { ...(this.metadata ?? {}), sdk_name: this.name, sdk_hash: this.specHash() };
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { UserError } from "./errors.js";
|
|
3
|
+
|
|
4
|
+
const SDK_KEYS = ["sdk_name", "sdk_hash"];
|
|
5
|
+
|
|
6
|
+
/** Settings as fingerprinted: metadata keeps the user's keys but drops sdk_name/sdk_hash (and goes when empty). */
|
|
7
|
+
export function fingerprintBody(settings: Record<string, unknown>): Record<string, unknown> {
|
|
8
|
+
const body: Record<string, unknown> = {};
|
|
9
|
+
for (const key of Object.keys(settings)) {
|
|
10
|
+
if (key !== "metadata") body[key] = settings[key];
|
|
11
|
+
}
|
|
12
|
+
const metadata = settings["metadata"];
|
|
13
|
+
if (metadata !== null && typeof metadata === "object" && !Array.isArray(metadata)) {
|
|
14
|
+
const user: Record<string, unknown> = {};
|
|
15
|
+
let any = false;
|
|
16
|
+
for (const key of Object.keys(metadata as Record<string, unknown>)) {
|
|
17
|
+
if (SDK_KEYS.indexOf(key) === -1) {
|
|
18
|
+
user[key] = (metadata as Record<string, unknown>)[key];
|
|
19
|
+
any = true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (any) body["metadata"] = user;
|
|
23
|
+
} else if (metadata !== undefined && metadata !== null) {
|
|
24
|
+
body["metadata"] = metadata;
|
|
25
|
+
}
|
|
26
|
+
return body;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function compareCodePoints(a: string, b: string): number {
|
|
30
|
+
const x = Array.from(a);
|
|
31
|
+
const y = Array.from(b);
|
|
32
|
+
const n = Math.min(x.length, y.length);
|
|
33
|
+
for (let i = 0; i < n; i++) {
|
|
34
|
+
const d = x[i].codePointAt(0)! - y[i].codePointAt(0)!;
|
|
35
|
+
if (d !== 0) return d;
|
|
36
|
+
}
|
|
37
|
+
return x.length - y.length;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A string as Python's json.dumps(ensure_ascii=True) writes it. */
|
|
41
|
+
function quote(s: string): string {
|
|
42
|
+
const json = JSON.stringify(s); // short escapes for control characters, \uXXXX (lowercase) for lone surrogates
|
|
43
|
+
let out = "";
|
|
44
|
+
for (let i = 0; i < json.length; i++) {
|
|
45
|
+
const c = json.charCodeAt(i);
|
|
46
|
+
out += c > 0x7e ? "\\u" + c.toString(16).padStart(4, "0") : json[i];
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Canonical JSON: code-point-sorted keys, no spaces, ASCII-only strings, ECMAScript numbers. */
|
|
52
|
+
export function canonicalJson(value: unknown): string {
|
|
53
|
+
if (value === null) return "null";
|
|
54
|
+
if (value === true) return "true";
|
|
55
|
+
if (value === false) return "false";
|
|
56
|
+
if (typeof value === "number") {
|
|
57
|
+
if (!Number.isFinite(value)) throw new UserError("NaN and Infinity cannot be fingerprinted");
|
|
58
|
+
return Number.isInteger(value) ? BigInt(value).toString() : JSON.stringify(value);
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "string") return quote(value);
|
|
61
|
+
if (Array.isArray(value)) return "[" + value.map((v) => canonicalJson(v)).join(",") + "]";
|
|
62
|
+
if (typeof value === "object" && value !== undefined) {
|
|
63
|
+
const obj = value as Record<string, unknown>;
|
|
64
|
+
const keys = Object.keys(obj).filter((k) => obj[k] !== undefined).sort(compareCodePoints);
|
|
65
|
+
return "{" + keys.map((k) => quote(k) + ":" + canonicalJson(obj[k])).join(",") + "}";
|
|
66
|
+
}
|
|
67
|
+
throw new UserError(`cannot fingerprint ${typeof value}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Hash of instructions + settings (minus metadata.sdk_*); the single source of truth for sdk_hash. */
|
|
71
|
+
export function settingsFingerprint(instructions: string, settings: Record<string, unknown>): string {
|
|
72
|
+
const raw = canonicalJson({ instructions, settings: fingerprintBody(settings) });
|
|
73
|
+
return createHash("sha256").update(raw, "utf8").digest("hex");
|
|
74
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./errors.js";
|
|
2
|
+
export * from "./fingerprint.js";
|
|
3
|
+
export * from "./tools.js";
|
|
4
|
+
export * from "./agent.js";
|
|
5
|
+
export type { Event, ClarifyQuestion, FunctionCallRequest } from "./events.js";
|
|
6
|
+
export { SettingsTransport } from "./settingsTransport.js";
|
|
7
|
+
export type { RemoteAgent, SettingsWarning, TurnStream, Transport } from "./transport.js";
|
|
8
|
+
export { deploy, undeploy, setDefaultClient } from "./deploy.js";
|
|
9
|
+
export type { Deployment } from "./deploy.js";
|
|
10
|
+
export { run, RunResult, StreamedRunResult } from "./run.js";
|
|
11
|
+
export type { RunOptions, RunStreamEvent, ClarifyHandler } from "./run.js";
|
|
12
|
+
export { RunState } from "./runState.js";
|
|
13
|
+
export type { Interruption } from "./runState.js";
|