langwatch 1.10.0 → 1.11.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/dist/agent/index.d.mts +301 -0
- package/dist/agent/index.d.ts +301 -0
- package/dist/agent/index.js +1468 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/index.mjs +1424 -0
- package/dist/agent/index.mjs.map +1 -0
- package/dist/{chunk-ZG2F6QBC.js → chunk-2PD3YA5H.js} +17 -6
- package/dist/chunk-2PD3YA5H.js.map +1 -0
- package/dist/{chunk-ODURZH6D.js → chunk-AJDFFRSW.js} +12 -12
- package/dist/{chunk-ODURZH6D.js.map → chunk-AJDFFRSW.js.map} +1 -1
- package/dist/{chunk-LFUK5LSB.mjs → chunk-BS5CNZ5S.mjs} +17 -6
- package/dist/chunk-BS5CNZ5S.mjs.map +1 -0
- package/dist/{chunk-6QIASJKJ.mjs → chunk-VMNKKJVI.mjs} +2 -2
- package/dist/cli/bundle.js +729 -391
- package/dist/{implementation-BvwOB77X.d.ts → implementation-D58yPTJW.d.ts} +1 -1
- package/dist/{implementation-C1F0eCHp.d.mts → implementation-aczLxVpV.d.mts} +1 -1
- package/dist/index.d.mts +166 -3
- package/dist/index.d.ts +166 -3
- package/dist/index.js +292 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +261 -29
- package/dist/index.mjs.map +1 -1
- package/dist/observability-sdk/index.d.mts +3 -3
- package/dist/observability-sdk/index.d.ts +3 -3
- package/dist/observability-sdk/index.js +2 -2
- package/dist/observability-sdk/index.mjs +1 -1
- package/dist/observability-sdk/instrumentation/langchain/index.d.mts +1 -1
- package/dist/observability-sdk/instrumentation/langchain/index.d.ts +1 -1
- package/dist/observability-sdk/setup/node/index.js +3 -3
- package/dist/observability-sdk/setup/node/index.mjs +2 -2
- package/dist/{types-C3NpCWi_.d.mts → types-BmMuUDrO.d.mts} +3858 -657
- package/dist/{types-jx13nj4M.d.ts → types-D_YNU-VL.d.ts} +3858 -657
- package/package.json +8 -1
- package/dist/chunk-LFUK5LSB.mjs.map +0 -1
- package/dist/chunk-ZG2F6QBC.js.map +0 -1
- /package/dist/{chunk-6QIASJKJ.mjs.map → chunk-VMNKKJVI.mjs.map} +0 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
interface Logger {
|
|
2
|
+
debug: (message: string, ...args: unknown[]) => void;
|
|
3
|
+
info: (message: string, ...args: unknown[]) => void;
|
|
4
|
+
warn: (message: string, ...args: unknown[]) => void;
|
|
5
|
+
error: (message: string, ...args: unknown[]) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The frames the SDK and the platform exchange over the agent socket.
|
|
10
|
+
*
|
|
11
|
+
* Every frame is one JSON text message with a `type` and the protocol
|
|
12
|
+
* version. The shapes here match the contract table in ADR-128 and the
|
|
13
|
+
* platform's own frame module; the validators are small and hand-written
|
|
14
|
+
* because this file is part of the public `langwatch/agent` surface, where no
|
|
15
|
+
* schema library may cross as a value.
|
|
16
|
+
*
|
|
17
|
+
* @see dev/docs/adr/128-connected-agents.md
|
|
18
|
+
*/
|
|
19
|
+
declare const PROTOCOL_VERSION = 1;
|
|
20
|
+
/** One conversation message, OpenAI style. Extra keys are carried as is. */
|
|
21
|
+
interface AgentMessage {
|
|
22
|
+
role: string;
|
|
23
|
+
content?: unknown;
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
/** The value of one run parameter as the platform sends it. */
|
|
27
|
+
type AgentParameterValue = string | number | boolean;
|
|
28
|
+
/** A JSON Schema object as the SDK sends it in `register`. */
|
|
29
|
+
type JsonSchemaObject = Record<string, unknown>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The connection the client speaks over, behind one small interface so the
|
|
33
|
+
* client never depends on how the frames travel.
|
|
34
|
+
*
|
|
35
|
+
* Two transports carry the same frames. The WebSocket is the default and it
|
|
36
|
+
* needs the `ws` package: the platform authenticates from the request
|
|
37
|
+
* headers of the upgrade, and no global `WebSocket` constructor can send
|
|
38
|
+
* them. HTTP long polling is for a network that blocks WebSockets: one POST
|
|
39
|
+
* registers, a GET waits for the next frames, a POST carries the answers. It
|
|
40
|
+
* speaks through the global `fetch` (Node 20+).
|
|
41
|
+
*/
|
|
42
|
+
declare const AGENT_TRANSPORTS: readonly ["websocket", "http"];
|
|
43
|
+
type AgentTransport = (typeof AGENT_TRANSPORTS)[number];
|
|
44
|
+
/**
|
|
45
|
+
* The transport to start with: the explicit option, then
|
|
46
|
+
* `LANGWATCH_AGENT_TRANSPORT`, else the WebSocket. Anything that is not
|
|
47
|
+
* `http` is the WebSocket, which falls back to HTTP on its own when the
|
|
48
|
+
* upgrade is refused.
|
|
49
|
+
*/
|
|
50
|
+
declare function resolveTransport({ explicit, env, }: {
|
|
51
|
+
explicit?: string;
|
|
52
|
+
env?: NodeJS.ProcessEnv;
|
|
53
|
+
}): AgentTransport;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The run parameters an agent declares, and the values a call supplies.
|
|
57
|
+
*
|
|
58
|
+
* Three forms are accepted: a definition map, any Standard JSON Schema object
|
|
59
|
+
* (read through `"~standard".jsonSchema`, so zod 4, valibot and arktype work
|
|
60
|
+
* without this package importing them), or a plain JSON Schema. A schema
|
|
61
|
+
* library instance that offers no JSON Schema converter is refused with the
|
|
62
|
+
* three forms named, because the SDK never takes a zod instance as a value.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/** The scalar types a run parameter may hold. */
|
|
66
|
+
type ParameterType = "string" | "number" | "boolean";
|
|
67
|
+
/** One entry of the definition map. */
|
|
68
|
+
interface ParameterDefinition {
|
|
69
|
+
/** The value type. Read from `options`, then `default`, else string. */
|
|
70
|
+
type?: ParameterType;
|
|
71
|
+
/** A closed list of accepted values. */
|
|
72
|
+
options?: readonly string[];
|
|
73
|
+
/** The value a run takes when it does not supply one. Without it the parameter is required. */
|
|
74
|
+
default?: AgentParameterValue;
|
|
75
|
+
description?: string;
|
|
76
|
+
}
|
|
77
|
+
/** Parameters declared by name. */
|
|
78
|
+
type ParameterDefinitions = Record<string, ParameterDefinition>;
|
|
79
|
+
/**
|
|
80
|
+
* The Standard JSON Schema converter an object exposes under `"~standard"`.
|
|
81
|
+
* Method syntax on purpose: a library narrows `target` to its own union, and
|
|
82
|
+
* a method parameter is checked bivariantly, so zod 4, valibot and arktype
|
|
83
|
+
* all fit without the SDK naming any of them.
|
|
84
|
+
*/
|
|
85
|
+
interface StandardJsonSchemaConverter {
|
|
86
|
+
input?(options: {
|
|
87
|
+
readonly target: string;
|
|
88
|
+
}): Record<string, unknown>;
|
|
89
|
+
output?(options: {
|
|
90
|
+
readonly target: string;
|
|
91
|
+
}): Record<string, unknown>;
|
|
92
|
+
}
|
|
93
|
+
/** One problem a Standard Schema `validate` reports. */
|
|
94
|
+
interface StandardSchemaIssue {
|
|
95
|
+
readonly message: string;
|
|
96
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
97
|
+
readonly key: PropertyKey;
|
|
98
|
+
}> | undefined;
|
|
99
|
+
}
|
|
100
|
+
type StandardSchemaResult<O> = {
|
|
101
|
+
readonly value: O;
|
|
102
|
+
readonly issues?: undefined;
|
|
103
|
+
} | {
|
|
104
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Any object that implements the Standard JSON Schema interface. When it also
|
|
108
|
+
* implements Standard Schema (`validate`), the values of every call go
|
|
109
|
+
* through it before the handler runs, so a zod 4 schema validates, fills its
|
|
110
|
+
* defaults and types `params` in one place.
|
|
111
|
+
*/
|
|
112
|
+
interface StandardJsonSchema<O = unknown> {
|
|
113
|
+
readonly "~standard": {
|
|
114
|
+
readonly jsonSchema: StandardJsonSchemaConverter;
|
|
115
|
+
validate?(value: unknown): StandardSchemaResult<O> | Promise<StandardSchemaResult<O>>;
|
|
116
|
+
/** Type-only, from Standard Schema: the parsed output type. */
|
|
117
|
+
readonly types?: {
|
|
118
|
+
readonly input: unknown;
|
|
119
|
+
readonly output: O;
|
|
120
|
+
} | undefined;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** The `params` type a Standard Schema object gives the handler: its parsed output. */
|
|
124
|
+
type InferStandardOutput<S> = S extends {
|
|
125
|
+
readonly "~standard": {
|
|
126
|
+
readonly types?: infer T;
|
|
127
|
+
};
|
|
128
|
+
} ? [NonNullable<T>] extends [never] ? Record<string, AgentParameterValue> : NonNullable<T> extends {
|
|
129
|
+
readonly output: infer O;
|
|
130
|
+
} ? O extends Record<string, unknown> ? O : Record<string, AgentParameterValue> : Record<string, AgentParameterValue> : Record<string, AgentParameterValue>;
|
|
131
|
+
/** Every form `parameters` accepts. */
|
|
132
|
+
type ParameterInput = ParameterDefinitions | StandardJsonSchema | JsonSchemaObject;
|
|
133
|
+
/** One parameter as the platform lists it, derived from the schema. */
|
|
134
|
+
interface ParameterSpec {
|
|
135
|
+
name: string;
|
|
136
|
+
type: ParameterType;
|
|
137
|
+
options?: string[];
|
|
138
|
+
default?: AgentParameterValue;
|
|
139
|
+
description?: string;
|
|
140
|
+
required?: boolean;
|
|
141
|
+
}
|
|
142
|
+
/** The refusal of a parameter definition or of a value a call supplied. */
|
|
143
|
+
declare class AgentParameterError extends Error {
|
|
144
|
+
readonly code = "agent_parameter_invalid";
|
|
145
|
+
constructor(message: string);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The parameter schema the `register` frame carries, from any accepted form.
|
|
149
|
+
* No parameters is an object schema with no properties.
|
|
150
|
+
*/
|
|
151
|
+
declare function toParameterSchema(input: ParameterInput | undefined): JsonSchemaObject;
|
|
152
|
+
/**
|
|
153
|
+
* The parameters a schema declares, one spec per property, the way the
|
|
154
|
+
* platform lists them. Unsupported property types read as text.
|
|
155
|
+
*/
|
|
156
|
+
declare function parameterSpecsFromSchema(schema: JsonSchemaObject): ParameterSpec[];
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* `connectAgent`: the function that runs an agent becomes a simulation target.
|
|
160
|
+
*
|
|
161
|
+
* The wrapper resolves the environment and the parameter schema at definition,
|
|
162
|
+
* registers the agent with the process-wide client, and returns a function
|
|
163
|
+
* that is directly callable (for unit tests and local runs) and exposes
|
|
164
|
+
* `disconnect()`.
|
|
165
|
+
*
|
|
166
|
+
* @see specs/typescript-sdk/agent-wrapper.feature
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/** The default call timeout, and the cap the platform enforces. */
|
|
170
|
+
declare const DEFAULT_TIMEOUT_MS = 120000;
|
|
171
|
+
declare const MAX_TIMEOUT_MS = 300000;
|
|
172
|
+
/** What a handler may return: a string, one message, a list of messages, or an output with a session. */
|
|
173
|
+
type AgentOutput = string | AgentMessage | AgentMessage[];
|
|
174
|
+
/** The output of one turn plus the session the agent keeps for the next turn of the same thread. */
|
|
175
|
+
interface AgentResult {
|
|
176
|
+
output: AgentOutput;
|
|
177
|
+
session?: unknown;
|
|
178
|
+
}
|
|
179
|
+
type AgentReply = AgentOutput | AgentResult;
|
|
180
|
+
/** The one object a handler receives on every turn. */
|
|
181
|
+
interface AgentCall<P = Record<string, AgentParameterValue>> {
|
|
182
|
+
/** The full conversation, OpenAI style. */
|
|
183
|
+
messages: AgentMessage[];
|
|
184
|
+
/** The messages added since the last turn of this thread. */
|
|
185
|
+
newMessages: AgentMessage[];
|
|
186
|
+
/** The platform's conversation id. */
|
|
187
|
+
threadId: string;
|
|
188
|
+
/** The value the handler returned as `session` on the previous turn of this thread, null on the first. */
|
|
189
|
+
session: unknown;
|
|
190
|
+
/** The run parameters, validated and with defaults filled. */
|
|
191
|
+
params: P;
|
|
192
|
+
/** The trace id of the turn, so the agent's own spans join it. Empty when the call carries none. */
|
|
193
|
+
traceId: string;
|
|
194
|
+
}
|
|
195
|
+
type AgentHandler<P> = (call: AgentCall<P>) => AgentReply | Promise<AgentReply>;
|
|
196
|
+
/** What a direct call of the wrapped function takes: messages, and anything else is optional. */
|
|
197
|
+
interface DirectAgentCall<P> {
|
|
198
|
+
messages: AgentMessage[];
|
|
199
|
+
newMessages?: AgentMessage[];
|
|
200
|
+
threadId?: string;
|
|
201
|
+
session?: unknown;
|
|
202
|
+
params?: Partial<P>;
|
|
203
|
+
traceId?: string;
|
|
204
|
+
}
|
|
205
|
+
interface ConnectAgentOptions<P extends ParameterInput = ParameterDefinitions> {
|
|
206
|
+
/** The agent name. One row per name and environment on the platform. */
|
|
207
|
+
name: string;
|
|
208
|
+
/** Resolved from LANGWATCH_AGENT_ENVIRONMENT, APP_ENV, ENVIRONMENT, NODE_ENV, else development. */
|
|
209
|
+
environment?: string;
|
|
210
|
+
/** A definition map, a Standard JSON Schema object, or a JSON Schema object. */
|
|
211
|
+
parameters?: P;
|
|
212
|
+
/** Default true, except when CI is truthy. LANGWATCH_AGENT_CONNECT=0 always disables. */
|
|
213
|
+
enabled?: boolean;
|
|
214
|
+
/** Names this instance in the platform. Also LANGWATCH_AGENT_INSTANCE_LABEL. */
|
|
215
|
+
instanceLabel?: string;
|
|
216
|
+
/** Per call, default 120000, at most 300000. */
|
|
217
|
+
timeoutMs?: number;
|
|
218
|
+
/** Calls in flight per instance, default 1 in development and 4 elsewhere. */
|
|
219
|
+
concurrency?: number;
|
|
220
|
+
/** Keep every turn of a thread on the instance that answered the first one. */
|
|
221
|
+
sticky?: boolean;
|
|
222
|
+
apiKey?: string;
|
|
223
|
+
endpoint?: string;
|
|
224
|
+
projectId?: string;
|
|
225
|
+
/** `websocket` (default, falls back to HTTP when the upgrade is refused) or `http`. Also LANGWATCH_AGENT_TRANSPORT. */
|
|
226
|
+
transport?: AgentTransport;
|
|
227
|
+
logger?: Logger;
|
|
228
|
+
}
|
|
229
|
+
/** The wrapped function: callable, and connected until `disconnect()`. */
|
|
230
|
+
interface ConnectedAgent<P> {
|
|
231
|
+
(call: DirectAgentCall<P>): Promise<AgentResult>;
|
|
232
|
+
readonly name: string;
|
|
233
|
+
readonly environment: string;
|
|
234
|
+
/** The parameter schema as registered. */
|
|
235
|
+
readonly parameters: JsonSchemaObject;
|
|
236
|
+
/** Send deregister and close the socket when this was the last agent of the process. */
|
|
237
|
+
disconnect: () => Promise<void>;
|
|
238
|
+
}
|
|
239
|
+
type Widen<V> = V extends string ? string : V extends number ? number : V extends boolean ? boolean : V;
|
|
240
|
+
type ParameterValueOf<D extends ParameterDefinition> = D extends {
|
|
241
|
+
options: readonly (infer O extends string)[];
|
|
242
|
+
} ? O : D extends {
|
|
243
|
+
type: "number";
|
|
244
|
+
} ? number : D extends {
|
|
245
|
+
type: "boolean";
|
|
246
|
+
} ? boolean : D extends {
|
|
247
|
+
type: "string";
|
|
248
|
+
} ? string : D extends {
|
|
249
|
+
default: infer V;
|
|
250
|
+
} ? Widen<V> : string;
|
|
251
|
+
/** The `params` type a definition map gives the handler. */
|
|
252
|
+
type InferParameters<P extends ParameterDefinitions> = {
|
|
253
|
+
[K in keyof P]: ParameterValueOf<P[K]>;
|
|
254
|
+
};
|
|
255
|
+
/** One of the four reply shapes as the `{ output, session }` the result frame carries. */
|
|
256
|
+
declare function normalizeReply(reply: unknown): AgentResult;
|
|
257
|
+
/**
|
|
258
|
+
* A schema library object (zod 4, valibot, arktype: anything with Standard
|
|
259
|
+
* Schema and Standard JSON Schema) types `params` as its parsed output and
|
|
260
|
+
* validates every call's values before the handler runs.
|
|
261
|
+
*/
|
|
262
|
+
declare function connectAgent<const S extends StandardJsonSchema>(options: ConnectAgentOptions<S> & {
|
|
263
|
+
parameters: S;
|
|
264
|
+
}, handler: AgentHandler<InferStandardOutput<S>>): ConnectedAgent<InferStandardOutput<S>>;
|
|
265
|
+
declare function connectAgent<const P extends ParameterDefinitions = Record<string, never>>(options: ConnectAgentOptions<P>, handler: AgentHandler<InferParameters<P>>): ConnectedAgent<InferParameters<P>>;
|
|
266
|
+
declare function connectAgent(options: ConnectAgentOptions<JsonSchemaObject>, handler: AgentHandler<Record<string, AgentParameterValue>>): ConnectedAgent<Record<string, AgentParameterValue>>;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Who and where a connected agent is: its environment, its instance identity
|
|
270
|
+
* and the endpoint it connects to. Every read of the machine is defensive, so
|
|
271
|
+
* a locked-down sandbox with no hostname or no passwd entry still connects.
|
|
272
|
+
*/
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* An environment name as the platform stores it: lowercase, `[a-z0-9_-]`
|
|
276
|
+
* only, at most 32 characters. Anything else collapses to a dash, and an
|
|
277
|
+
* empty result is the default environment.
|
|
278
|
+
*/
|
|
279
|
+
declare function sanitizeEnvironment(name: string): string;
|
|
280
|
+
/**
|
|
281
|
+
* The environment an agent registers under: the explicit option, then
|
|
282
|
+
* `LANGWATCH_AGENT_ENVIRONMENT`, `APP_ENV`, `ENVIRONMENT`, `NODE_ENV`, else
|
|
283
|
+
* `development`.
|
|
284
|
+
*/
|
|
285
|
+
declare function resolveEnvironment({ explicit, env, }: {
|
|
286
|
+
explicit?: string;
|
|
287
|
+
env?: NodeJS.ProcessEnv;
|
|
288
|
+
}): string;
|
|
289
|
+
/**
|
|
290
|
+
* The socket URL for an endpoint: `https://app.langwatch.ai` becomes
|
|
291
|
+
* `wss://app.langwatch.ai/api/v1/agents/connect`, `http://localhost:5560`
|
|
292
|
+
* becomes `ws://localhost:5560/api/v1/agents/connect`.
|
|
293
|
+
*/
|
|
294
|
+
declare function resolveConnectUrl(endpoint?: string | null): string;
|
|
295
|
+
/**
|
|
296
|
+
* The base of the HTTP long-poll routes for an endpoint:
|
|
297
|
+
* `https://app.langwatch.ai` becomes `https://app.langwatch.ai/api/v1/agents/connect`.
|
|
298
|
+
*/
|
|
299
|
+
declare function resolveHttpConnectUrl(endpoint?: string | null): string;
|
|
300
|
+
|
|
301
|
+
export { AGENT_TRANSPORTS, type AgentCall, type AgentHandler, type AgentMessage, type AgentOutput, AgentParameterError, type AgentParameterValue, type AgentReply, type AgentResult, type AgentTransport, type ConnectAgentOptions, type ConnectedAgent, DEFAULT_TIMEOUT_MS, type DirectAgentCall, type InferParameters, type JsonSchemaObject, MAX_TIMEOUT_MS, PROTOCOL_VERSION, type ParameterDefinition, type ParameterDefinitions, type ParameterInput, type ParameterSpec, type ParameterType, type StandardJsonSchema, connectAgent, normalizeReply, parameterSpecsFromSchema, resolveConnectUrl, resolveEnvironment, resolveHttpConnectUrl, resolveTransport, sanitizeEnvironment, toParameterSchema };
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
interface Logger {
|
|
2
|
+
debug: (message: string, ...args: unknown[]) => void;
|
|
3
|
+
info: (message: string, ...args: unknown[]) => void;
|
|
4
|
+
warn: (message: string, ...args: unknown[]) => void;
|
|
5
|
+
error: (message: string, ...args: unknown[]) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The frames the SDK and the platform exchange over the agent socket.
|
|
10
|
+
*
|
|
11
|
+
* Every frame is one JSON text message with a `type` and the protocol
|
|
12
|
+
* version. The shapes here match the contract table in ADR-128 and the
|
|
13
|
+
* platform's own frame module; the validators are small and hand-written
|
|
14
|
+
* because this file is part of the public `langwatch/agent` surface, where no
|
|
15
|
+
* schema library may cross as a value.
|
|
16
|
+
*
|
|
17
|
+
* @see dev/docs/adr/128-connected-agents.md
|
|
18
|
+
*/
|
|
19
|
+
declare const PROTOCOL_VERSION = 1;
|
|
20
|
+
/** One conversation message, OpenAI style. Extra keys are carried as is. */
|
|
21
|
+
interface AgentMessage {
|
|
22
|
+
role: string;
|
|
23
|
+
content?: unknown;
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
/** The value of one run parameter as the platform sends it. */
|
|
27
|
+
type AgentParameterValue = string | number | boolean;
|
|
28
|
+
/** A JSON Schema object as the SDK sends it in `register`. */
|
|
29
|
+
type JsonSchemaObject = Record<string, unknown>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The connection the client speaks over, behind one small interface so the
|
|
33
|
+
* client never depends on how the frames travel.
|
|
34
|
+
*
|
|
35
|
+
* Two transports carry the same frames. The WebSocket is the default and it
|
|
36
|
+
* needs the `ws` package: the platform authenticates from the request
|
|
37
|
+
* headers of the upgrade, and no global `WebSocket` constructor can send
|
|
38
|
+
* them. HTTP long polling is for a network that blocks WebSockets: one POST
|
|
39
|
+
* registers, a GET waits for the next frames, a POST carries the answers. It
|
|
40
|
+
* speaks through the global `fetch` (Node 20+).
|
|
41
|
+
*/
|
|
42
|
+
declare const AGENT_TRANSPORTS: readonly ["websocket", "http"];
|
|
43
|
+
type AgentTransport = (typeof AGENT_TRANSPORTS)[number];
|
|
44
|
+
/**
|
|
45
|
+
* The transport to start with: the explicit option, then
|
|
46
|
+
* `LANGWATCH_AGENT_TRANSPORT`, else the WebSocket. Anything that is not
|
|
47
|
+
* `http` is the WebSocket, which falls back to HTTP on its own when the
|
|
48
|
+
* upgrade is refused.
|
|
49
|
+
*/
|
|
50
|
+
declare function resolveTransport({ explicit, env, }: {
|
|
51
|
+
explicit?: string;
|
|
52
|
+
env?: NodeJS.ProcessEnv;
|
|
53
|
+
}): AgentTransport;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The run parameters an agent declares, and the values a call supplies.
|
|
57
|
+
*
|
|
58
|
+
* Three forms are accepted: a definition map, any Standard JSON Schema object
|
|
59
|
+
* (read through `"~standard".jsonSchema`, so zod 4, valibot and arktype work
|
|
60
|
+
* without this package importing them), or a plain JSON Schema. A schema
|
|
61
|
+
* library instance that offers no JSON Schema converter is refused with the
|
|
62
|
+
* three forms named, because the SDK never takes a zod instance as a value.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/** The scalar types a run parameter may hold. */
|
|
66
|
+
type ParameterType = "string" | "number" | "boolean";
|
|
67
|
+
/** One entry of the definition map. */
|
|
68
|
+
interface ParameterDefinition {
|
|
69
|
+
/** The value type. Read from `options`, then `default`, else string. */
|
|
70
|
+
type?: ParameterType;
|
|
71
|
+
/** A closed list of accepted values. */
|
|
72
|
+
options?: readonly string[];
|
|
73
|
+
/** The value a run takes when it does not supply one. Without it the parameter is required. */
|
|
74
|
+
default?: AgentParameterValue;
|
|
75
|
+
description?: string;
|
|
76
|
+
}
|
|
77
|
+
/** Parameters declared by name. */
|
|
78
|
+
type ParameterDefinitions = Record<string, ParameterDefinition>;
|
|
79
|
+
/**
|
|
80
|
+
* The Standard JSON Schema converter an object exposes under `"~standard"`.
|
|
81
|
+
* Method syntax on purpose: a library narrows `target` to its own union, and
|
|
82
|
+
* a method parameter is checked bivariantly, so zod 4, valibot and arktype
|
|
83
|
+
* all fit without the SDK naming any of them.
|
|
84
|
+
*/
|
|
85
|
+
interface StandardJsonSchemaConverter {
|
|
86
|
+
input?(options: {
|
|
87
|
+
readonly target: string;
|
|
88
|
+
}): Record<string, unknown>;
|
|
89
|
+
output?(options: {
|
|
90
|
+
readonly target: string;
|
|
91
|
+
}): Record<string, unknown>;
|
|
92
|
+
}
|
|
93
|
+
/** One problem a Standard Schema `validate` reports. */
|
|
94
|
+
interface StandardSchemaIssue {
|
|
95
|
+
readonly message: string;
|
|
96
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
97
|
+
readonly key: PropertyKey;
|
|
98
|
+
}> | undefined;
|
|
99
|
+
}
|
|
100
|
+
type StandardSchemaResult<O> = {
|
|
101
|
+
readonly value: O;
|
|
102
|
+
readonly issues?: undefined;
|
|
103
|
+
} | {
|
|
104
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Any object that implements the Standard JSON Schema interface. When it also
|
|
108
|
+
* implements Standard Schema (`validate`), the values of every call go
|
|
109
|
+
* through it before the handler runs, so a zod 4 schema validates, fills its
|
|
110
|
+
* defaults and types `params` in one place.
|
|
111
|
+
*/
|
|
112
|
+
interface StandardJsonSchema<O = unknown> {
|
|
113
|
+
readonly "~standard": {
|
|
114
|
+
readonly jsonSchema: StandardJsonSchemaConverter;
|
|
115
|
+
validate?(value: unknown): StandardSchemaResult<O> | Promise<StandardSchemaResult<O>>;
|
|
116
|
+
/** Type-only, from Standard Schema: the parsed output type. */
|
|
117
|
+
readonly types?: {
|
|
118
|
+
readonly input: unknown;
|
|
119
|
+
readonly output: O;
|
|
120
|
+
} | undefined;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** The `params` type a Standard Schema object gives the handler: its parsed output. */
|
|
124
|
+
type InferStandardOutput<S> = S extends {
|
|
125
|
+
readonly "~standard": {
|
|
126
|
+
readonly types?: infer T;
|
|
127
|
+
};
|
|
128
|
+
} ? [NonNullable<T>] extends [never] ? Record<string, AgentParameterValue> : NonNullable<T> extends {
|
|
129
|
+
readonly output: infer O;
|
|
130
|
+
} ? O extends Record<string, unknown> ? O : Record<string, AgentParameterValue> : Record<string, AgentParameterValue> : Record<string, AgentParameterValue>;
|
|
131
|
+
/** Every form `parameters` accepts. */
|
|
132
|
+
type ParameterInput = ParameterDefinitions | StandardJsonSchema | JsonSchemaObject;
|
|
133
|
+
/** One parameter as the platform lists it, derived from the schema. */
|
|
134
|
+
interface ParameterSpec {
|
|
135
|
+
name: string;
|
|
136
|
+
type: ParameterType;
|
|
137
|
+
options?: string[];
|
|
138
|
+
default?: AgentParameterValue;
|
|
139
|
+
description?: string;
|
|
140
|
+
required?: boolean;
|
|
141
|
+
}
|
|
142
|
+
/** The refusal of a parameter definition or of a value a call supplied. */
|
|
143
|
+
declare class AgentParameterError extends Error {
|
|
144
|
+
readonly code = "agent_parameter_invalid";
|
|
145
|
+
constructor(message: string);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The parameter schema the `register` frame carries, from any accepted form.
|
|
149
|
+
* No parameters is an object schema with no properties.
|
|
150
|
+
*/
|
|
151
|
+
declare function toParameterSchema(input: ParameterInput | undefined): JsonSchemaObject;
|
|
152
|
+
/**
|
|
153
|
+
* The parameters a schema declares, one spec per property, the way the
|
|
154
|
+
* platform lists them. Unsupported property types read as text.
|
|
155
|
+
*/
|
|
156
|
+
declare function parameterSpecsFromSchema(schema: JsonSchemaObject): ParameterSpec[];
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* `connectAgent`: the function that runs an agent becomes a simulation target.
|
|
160
|
+
*
|
|
161
|
+
* The wrapper resolves the environment and the parameter schema at definition,
|
|
162
|
+
* registers the agent with the process-wide client, and returns a function
|
|
163
|
+
* that is directly callable (for unit tests and local runs) and exposes
|
|
164
|
+
* `disconnect()`.
|
|
165
|
+
*
|
|
166
|
+
* @see specs/typescript-sdk/agent-wrapper.feature
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/** The default call timeout, and the cap the platform enforces. */
|
|
170
|
+
declare const DEFAULT_TIMEOUT_MS = 120000;
|
|
171
|
+
declare const MAX_TIMEOUT_MS = 300000;
|
|
172
|
+
/** What a handler may return: a string, one message, a list of messages, or an output with a session. */
|
|
173
|
+
type AgentOutput = string | AgentMessage | AgentMessage[];
|
|
174
|
+
/** The output of one turn plus the session the agent keeps for the next turn of the same thread. */
|
|
175
|
+
interface AgentResult {
|
|
176
|
+
output: AgentOutput;
|
|
177
|
+
session?: unknown;
|
|
178
|
+
}
|
|
179
|
+
type AgentReply = AgentOutput | AgentResult;
|
|
180
|
+
/** The one object a handler receives on every turn. */
|
|
181
|
+
interface AgentCall<P = Record<string, AgentParameterValue>> {
|
|
182
|
+
/** The full conversation, OpenAI style. */
|
|
183
|
+
messages: AgentMessage[];
|
|
184
|
+
/** The messages added since the last turn of this thread. */
|
|
185
|
+
newMessages: AgentMessage[];
|
|
186
|
+
/** The platform's conversation id. */
|
|
187
|
+
threadId: string;
|
|
188
|
+
/** The value the handler returned as `session` on the previous turn of this thread, null on the first. */
|
|
189
|
+
session: unknown;
|
|
190
|
+
/** The run parameters, validated and with defaults filled. */
|
|
191
|
+
params: P;
|
|
192
|
+
/** The trace id of the turn, so the agent's own spans join it. Empty when the call carries none. */
|
|
193
|
+
traceId: string;
|
|
194
|
+
}
|
|
195
|
+
type AgentHandler<P> = (call: AgentCall<P>) => AgentReply | Promise<AgentReply>;
|
|
196
|
+
/** What a direct call of the wrapped function takes: messages, and anything else is optional. */
|
|
197
|
+
interface DirectAgentCall<P> {
|
|
198
|
+
messages: AgentMessage[];
|
|
199
|
+
newMessages?: AgentMessage[];
|
|
200
|
+
threadId?: string;
|
|
201
|
+
session?: unknown;
|
|
202
|
+
params?: Partial<P>;
|
|
203
|
+
traceId?: string;
|
|
204
|
+
}
|
|
205
|
+
interface ConnectAgentOptions<P extends ParameterInput = ParameterDefinitions> {
|
|
206
|
+
/** The agent name. One row per name and environment on the platform. */
|
|
207
|
+
name: string;
|
|
208
|
+
/** Resolved from LANGWATCH_AGENT_ENVIRONMENT, APP_ENV, ENVIRONMENT, NODE_ENV, else development. */
|
|
209
|
+
environment?: string;
|
|
210
|
+
/** A definition map, a Standard JSON Schema object, or a JSON Schema object. */
|
|
211
|
+
parameters?: P;
|
|
212
|
+
/** Default true, except when CI is truthy. LANGWATCH_AGENT_CONNECT=0 always disables. */
|
|
213
|
+
enabled?: boolean;
|
|
214
|
+
/** Names this instance in the platform. Also LANGWATCH_AGENT_INSTANCE_LABEL. */
|
|
215
|
+
instanceLabel?: string;
|
|
216
|
+
/** Per call, default 120000, at most 300000. */
|
|
217
|
+
timeoutMs?: number;
|
|
218
|
+
/** Calls in flight per instance, default 1 in development and 4 elsewhere. */
|
|
219
|
+
concurrency?: number;
|
|
220
|
+
/** Keep every turn of a thread on the instance that answered the first one. */
|
|
221
|
+
sticky?: boolean;
|
|
222
|
+
apiKey?: string;
|
|
223
|
+
endpoint?: string;
|
|
224
|
+
projectId?: string;
|
|
225
|
+
/** `websocket` (default, falls back to HTTP when the upgrade is refused) or `http`. Also LANGWATCH_AGENT_TRANSPORT. */
|
|
226
|
+
transport?: AgentTransport;
|
|
227
|
+
logger?: Logger;
|
|
228
|
+
}
|
|
229
|
+
/** The wrapped function: callable, and connected until `disconnect()`. */
|
|
230
|
+
interface ConnectedAgent<P> {
|
|
231
|
+
(call: DirectAgentCall<P>): Promise<AgentResult>;
|
|
232
|
+
readonly name: string;
|
|
233
|
+
readonly environment: string;
|
|
234
|
+
/** The parameter schema as registered. */
|
|
235
|
+
readonly parameters: JsonSchemaObject;
|
|
236
|
+
/** Send deregister and close the socket when this was the last agent of the process. */
|
|
237
|
+
disconnect: () => Promise<void>;
|
|
238
|
+
}
|
|
239
|
+
type Widen<V> = V extends string ? string : V extends number ? number : V extends boolean ? boolean : V;
|
|
240
|
+
type ParameterValueOf<D extends ParameterDefinition> = D extends {
|
|
241
|
+
options: readonly (infer O extends string)[];
|
|
242
|
+
} ? O : D extends {
|
|
243
|
+
type: "number";
|
|
244
|
+
} ? number : D extends {
|
|
245
|
+
type: "boolean";
|
|
246
|
+
} ? boolean : D extends {
|
|
247
|
+
type: "string";
|
|
248
|
+
} ? string : D extends {
|
|
249
|
+
default: infer V;
|
|
250
|
+
} ? Widen<V> : string;
|
|
251
|
+
/** The `params` type a definition map gives the handler. */
|
|
252
|
+
type InferParameters<P extends ParameterDefinitions> = {
|
|
253
|
+
[K in keyof P]: ParameterValueOf<P[K]>;
|
|
254
|
+
};
|
|
255
|
+
/** One of the four reply shapes as the `{ output, session }` the result frame carries. */
|
|
256
|
+
declare function normalizeReply(reply: unknown): AgentResult;
|
|
257
|
+
/**
|
|
258
|
+
* A schema library object (zod 4, valibot, arktype: anything with Standard
|
|
259
|
+
* Schema and Standard JSON Schema) types `params` as its parsed output and
|
|
260
|
+
* validates every call's values before the handler runs.
|
|
261
|
+
*/
|
|
262
|
+
declare function connectAgent<const S extends StandardJsonSchema>(options: ConnectAgentOptions<S> & {
|
|
263
|
+
parameters: S;
|
|
264
|
+
}, handler: AgentHandler<InferStandardOutput<S>>): ConnectedAgent<InferStandardOutput<S>>;
|
|
265
|
+
declare function connectAgent<const P extends ParameterDefinitions = Record<string, never>>(options: ConnectAgentOptions<P>, handler: AgentHandler<InferParameters<P>>): ConnectedAgent<InferParameters<P>>;
|
|
266
|
+
declare function connectAgent(options: ConnectAgentOptions<JsonSchemaObject>, handler: AgentHandler<Record<string, AgentParameterValue>>): ConnectedAgent<Record<string, AgentParameterValue>>;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Who and where a connected agent is: its environment, its instance identity
|
|
270
|
+
* and the endpoint it connects to. Every read of the machine is defensive, so
|
|
271
|
+
* a locked-down sandbox with no hostname or no passwd entry still connects.
|
|
272
|
+
*/
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* An environment name as the platform stores it: lowercase, `[a-z0-9_-]`
|
|
276
|
+
* only, at most 32 characters. Anything else collapses to a dash, and an
|
|
277
|
+
* empty result is the default environment.
|
|
278
|
+
*/
|
|
279
|
+
declare function sanitizeEnvironment(name: string): string;
|
|
280
|
+
/**
|
|
281
|
+
* The environment an agent registers under: the explicit option, then
|
|
282
|
+
* `LANGWATCH_AGENT_ENVIRONMENT`, `APP_ENV`, `ENVIRONMENT`, `NODE_ENV`, else
|
|
283
|
+
* `development`.
|
|
284
|
+
*/
|
|
285
|
+
declare function resolveEnvironment({ explicit, env, }: {
|
|
286
|
+
explicit?: string;
|
|
287
|
+
env?: NodeJS.ProcessEnv;
|
|
288
|
+
}): string;
|
|
289
|
+
/**
|
|
290
|
+
* The socket URL for an endpoint: `https://app.langwatch.ai` becomes
|
|
291
|
+
* `wss://app.langwatch.ai/api/v1/agents/connect`, `http://localhost:5560`
|
|
292
|
+
* becomes `ws://localhost:5560/api/v1/agents/connect`.
|
|
293
|
+
*/
|
|
294
|
+
declare function resolveConnectUrl(endpoint?: string | null): string;
|
|
295
|
+
/**
|
|
296
|
+
* The base of the HTTP long-poll routes for an endpoint:
|
|
297
|
+
* `https://app.langwatch.ai` becomes `https://app.langwatch.ai/api/v1/agents/connect`.
|
|
298
|
+
*/
|
|
299
|
+
declare function resolveHttpConnectUrl(endpoint?: string | null): string;
|
|
300
|
+
|
|
301
|
+
export { AGENT_TRANSPORTS, type AgentCall, type AgentHandler, type AgentMessage, type AgentOutput, AgentParameterError, type AgentParameterValue, type AgentReply, type AgentResult, type AgentTransport, type ConnectAgentOptions, type ConnectedAgent, DEFAULT_TIMEOUT_MS, type DirectAgentCall, type InferParameters, type JsonSchemaObject, MAX_TIMEOUT_MS, PROTOCOL_VERSION, type ParameterDefinition, type ParameterDefinitions, type ParameterInput, type ParameterSpec, type ParameterType, type StandardJsonSchema, connectAgent, normalizeReply, parameterSpecsFromSchema, resolveConnectUrl, resolveEnvironment, resolveHttpConnectUrl, resolveTransport, sanitizeEnvironment, toParameterSchema };
|