langsmith 0.10.7 → 0.10.9

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.
Files changed (101) hide show
  1. package/dist/_openapi_client/client.cjs +26 -2
  2. package/dist/_openapi_client/client.d.ts +15 -6
  3. package/dist/_openapi_client/client.js +27 -3
  4. package/dist/_openapi_client/internal/detect-platform.cjs +1 -1
  5. package/dist/_openapi_client/internal/detect-platform.js +1 -1
  6. package/dist/_openapi_client/resources/annotation-queues/items.cjs +13 -6
  7. package/dist/_openapi_client/resources/annotation-queues/items.d.ts +33 -8
  8. package/dist/_openapi_client/resources/annotation-queues/items.js +13 -6
  9. package/dist/_openapi_client/resources/datasets/datasets.cjs +9 -0
  10. package/dist/_openapi_client/resources/datasets/datasets.d.ts +4 -0
  11. package/dist/_openapi_client/resources/datasets/datasets.js +9 -0
  12. package/dist/_openapi_client/resources/datasets/examples.cjs +26 -0
  13. package/dist/_openapi_client/resources/datasets/examples.d.ts +23 -0
  14. package/dist/_openapi_client/resources/datasets/examples.js +22 -0
  15. package/dist/_openapi_client/resources/fleet/fleet.cjs +54 -0
  16. package/dist/_openapi_client/resources/fleet/fleet.d.ts +9 -0
  17. package/dist/_openapi_client/resources/fleet/fleet.js +17 -0
  18. package/dist/_openapi_client/resources/fleet/threads.cjs +18 -0
  19. package/dist/_openapi_client/resources/fleet/threads.d.ts +19 -0
  20. package/dist/_openapi_client/resources/fleet/threads.js +14 -0
  21. package/dist/_openapi_client/resources/index.cjs +8 -2
  22. package/dist/_openapi_client/resources/index.d.ts +6 -3
  23. package/dist/_openapi_client/resources/index.js +5 -2
  24. package/dist/_openapi_client/resources/info.d.ts +6 -0
  25. package/dist/_openapi_client/resources/issues.cjs +2 -2
  26. package/dist/_openapi_client/resources/issues.d.ts +188 -4
  27. package/dist/_openapi_client/resources/issues.js +2 -2
  28. package/dist/_openapi_client/resources/online-evaluators.cjs +7 -4
  29. package/dist/_openapi_client/resources/online-evaluators.d.ts +63 -4
  30. package/dist/_openapi_client/resources/online-evaluators.js +7 -4
  31. package/dist/_openapi_client/resources/product-feedback.cjs +33 -0
  32. package/dist/_openapi_client/resources/product-feedback.d.ts +77 -0
  33. package/dist/_openapi_client/resources/product-feedback.js +29 -0
  34. package/dist/_openapi_client/resources/runs/runs.d.ts +26 -16
  35. package/dist/_openapi_client/resources/sandboxes/boxes.cjs +44 -4
  36. package/dist/_openapi_client/resources/sandboxes/boxes.d.ts +256 -21
  37. package/dist/_openapi_client/resources/sandboxes/boxes.js +44 -4
  38. package/dist/_openapi_client/resources/sandboxes/registries.d.ts +10 -2
  39. package/dist/_openapi_client/resources/sandboxes/sandboxes.cjs +13 -0
  40. package/dist/_openapi_client/resources/sandboxes/sandboxes.d.ts +169 -14
  41. package/dist/_openapi_client/resources/sandboxes/sandboxes.js +13 -0
  42. package/dist/_openapi_client/resources/sandboxes/snapshots.cjs +10 -6
  43. package/dist/_openapi_client/resources/sandboxes/snapshots.d.ts +37 -6
  44. package/dist/_openapi_client/resources/sandboxes/snapshots.js +10 -6
  45. package/dist/_openapi_client/resources/sessions.cjs +23 -0
  46. package/dist/_openapi_client/resources/sessions.d.ts +732 -0
  47. package/dist/_openapi_client/resources/sessions.js +19 -0
  48. package/dist/_openapi_client/resources/threads/index.cjs +9 -0
  49. package/dist/_openapi_client/resources/threads/index.d.ts +2 -0
  50. package/dist/_openapi_client/resources/threads/index.js +4 -0
  51. package/dist/_openapi_client/resources/threads/share.cjs +61 -0
  52. package/dist/_openapi_client/resources/threads/share.d.ts +71 -0
  53. package/dist/_openapi_client/resources/threads/share.js +57 -0
  54. package/dist/_openapi_client/resources/threads/threads.cjs +143 -0
  55. package/dist/_openapi_client/resources/threads/threads.d.ts +973 -0
  56. package/dist/_openapi_client/resources/threads/threads.js +106 -0
  57. package/dist/_openapi_client/resources/threads.cjs +15 -67
  58. package/dist/_openapi_client/resources/threads.d.ts +1 -709
  59. package/dist/_openapi_client/resources/threads.js +1 -65
  60. package/dist/_openapi_client/resources/traces.d.ts +7 -1
  61. package/dist/_openapi_client/version.cjs +1 -1
  62. package/dist/_openapi_client/version.js +1 -1
  63. package/dist/address.cjs +176 -99
  64. package/dist/address.d.ts +63 -8
  65. package/dist/address.js +170 -90
  66. package/dist/anonymizer/index.cjs +1 -1
  67. package/dist/anonymizer/index.js +1 -1
  68. package/dist/client.cjs +39 -19
  69. package/dist/client.d.ts +20 -4
  70. package/dist/client.js +34 -14
  71. package/dist/experimental/otel/exporter.cjs +21 -4
  72. package/dist/experimental/otel/exporter.d.ts +4 -2
  73. package/dist/experimental/otel/exporter.js +21 -4
  74. package/dist/index.cjs +5 -2
  75. package/dist/index.d.ts +2 -2
  76. package/dist/index.js +2 -2
  77. package/dist/run_trees.cjs +26 -43
  78. package/dist/run_trees.d.ts +4 -4
  79. package/dist/run_trees.js +23 -40
  80. package/dist/sandbox/client.cjs +33 -4
  81. package/dist/sandbox/client.d.ts +4 -0
  82. package/dist/sandbox/client.js +34 -5
  83. package/dist/sandbox/command_handle.cjs +8 -0
  84. package/dist/sandbox/command_handle.js +8 -0
  85. package/dist/sandbox/sandbox.cjs +7 -0
  86. package/dist/sandbox/sandbox.js +7 -0
  87. package/dist/sandbox/service_url.cjs +9 -1
  88. package/dist/sandbox/service_url.d.ts +2 -1
  89. package/dist/sandbox/service_url.js +9 -1
  90. package/dist/sandbox/tracing.cjs +13 -0
  91. package/dist/sandbox/tracing.d.ts +1 -0
  92. package/dist/sandbox/tracing.js +10 -0
  93. package/dist/schemas.d.ts +3 -3
  94. package/dist/traceable.cjs +5 -4
  95. package/dist/traceable.js +4 -3
  96. package/dist/utils/{agent_addressing.cjs → addressing.cjs} +22 -23
  97. package/dist/utils/{agent_addressing.d.ts → addressing.d.ts} +5 -8
  98. package/dist/utils/{agent_addressing.js → addressing.js} +23 -23
  99. package/dist/utils/env.cjs +6 -4
  100. package/dist/utils/env.js +6 -4
  101. package/package.json +1 -1
@@ -1,67 +1,3 @@
1
1
  // @ts-nocheck
2
2
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
- import { APIResource } from '../core/resource.js';
4
- import { ItemsCursorGetPagination, ItemsCursorPostPagination, } from '../core/pagination.js';
5
- import { path } from '../internal/utils/path.js';
6
- export class Threads extends APIResource {
7
- /**
8
- * Retrieve all traces belonging to a specific thread within a project.
9
- *
10
- * Self-hosted deployments require LangSmith `v0.16` or later.
11
- *
12
- * @example
13
- * ```ts
14
- * // Automatically fetches more pages as needed.
15
- * for await (const threadTrace of client.threads.listTraces(
16
- * 'thread_id',
17
- * { project_id: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' },
18
- * )) {
19
- * // ...
20
- * }
21
- * ```
22
- */
23
- listTraces(threadID, query, options) {
24
- return this._client.getAPIList(path `/api/v2/threads/${threadID}/traces`, (ItemsCursorGetPagination), { query, ...options });
25
- }
26
- /**
27
- * Query threads within a project (session), with cursor-based pagination. Returns
28
- * threads matching the given time range and optional filters.
29
- *
30
- * Self-hosted deployments require LangSmith `v0.16` or later.
31
- *
32
- * @example
33
- * ```ts
34
- * // Automatically fetches more pages as needed.
35
- * for await (const thread of client.threads.query()) {
36
- * // ...
37
- * }
38
- * ```
39
- */
40
- query(body, options) {
41
- return this._client.getAPIList('/api/v2/threads/query', (ItemsCursorPostPagination), {
42
- body,
43
- method: 'post',
44
- ...options,
45
- });
46
- }
47
- /**
48
- * Compute aggregate stats for a single thread (turn count, latency percentiles,
49
- * token/cost sums, and detail breakdowns) within a project.
50
- *
51
- * Self-hosted deployments require LangSmith `v0.16` or later.
52
- *
53
- * @example
54
- * ```ts
55
- * const threadStats = await client.threads.stats(
56
- * 'thread_id',
57
- * {
58
- * selects: ['TURNS'],
59
- * session_id: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e',
60
- * },
61
- * );
62
- * ```
63
- */
64
- stats(threadID, query, options) {
65
- return this._client.get(path `/api/v2/threads/${threadID}/stats`, { query, ...options });
66
- }
67
- }
3
+ export * from './threads/index.js';
@@ -46,6 +46,12 @@ export declare class Traces extends APIResource {
46
46
  }
47
47
  export type TracesItemsCursorPostPagination = ItemsCursorPostPagination<Trace>;
48
48
  export interface Trace {
49
+ /**
50
+ * `query_metadata` describes why this trace matched the query, including
51
+ * `sem_filter_score` when a semantic filter is used. Returned automatically, or
52
+ * null when unavailable.
53
+ */
54
+ query_metadata?: unknown | null;
49
55
  /**
50
56
  * `root_run` is the trace's root run. Which properties are populated is controlled
51
57
  * by `selects` in the request.
@@ -111,7 +117,7 @@ export interface TraceListRunsParams {
111
117
  * (repeatable query parameter). Accepts any value of the `RunSelectField` enum. If
112
118
  * omitted, only `id` is returned.
113
119
  */
114
- selects?: Array<'ID' | 'NAME' | 'RUN_TYPE' | 'STATUS' | 'START_TIME' | 'END_TIME' | 'LATENCY_SECONDS' | 'FIRST_TOKEN_TIME' | 'ERROR' | 'ERROR_PREVIEW' | 'EXTRA' | 'METADATA' | 'EVENTS' | 'INPUTS' | 'INPUTS_PREVIEW' | 'OUTPUTS' | 'OUTPUTS_PREVIEW' | 'MANIFEST' | 'PARENT_RUN_IDS' | 'PROJECT_ID' | 'TRACE_ID' | 'THREAD_ID' | 'DOTTED_ORDER' | 'IS_ROOT' | 'REFERENCE_EXAMPLE_ID' | 'REFERENCE_DATASET_ID' | 'TOTAL_TOKENS' | 'PROMPT_TOKENS' | 'COMPLETION_TOKENS' | 'TOTAL_COST' | 'PROMPT_COST' | 'COMPLETION_COST' | 'PROMPT_TOKEN_DETAILS' | 'COMPLETION_TOKEN_DETAILS' | 'PROMPT_COST_DETAILS' | 'COMPLETION_COST_DETAILS' | 'PRICE_MODEL_ID' | 'TAGS' | 'APP_PATH' | 'ATTACHMENTS' | 'THREAD_EVALUATION_TIME' | 'IS_IN_DATASET' | 'LAST_QUEUED_AT' | 'SHARE_URL' | 'FEEDBACK_STATS'>;
120
+ selects?: Array<'ID' | 'NAME' | 'RUN_TYPE' | 'STATUS' | 'START_TIME' | 'END_TIME' | 'LATENCY_SECONDS' | 'FIRST_TOKEN_TIME' | 'ERROR' | 'ERROR_PREVIEW' | 'EXTRA' | 'METADATA' | 'EVENTS' | 'INPUTS' | 'INPUTS_PREVIEW' | 'OUTPUTS' | 'OUTPUTS_PREVIEW' | 'MANIFEST' | 'PARENT_RUN_IDS' | 'PROJECT_ID' | 'TRACE_ID' | 'THREAD_ID' | 'DOTTED_ORDER' | 'IS_ROOT' | 'REFERENCE_EXAMPLE_ID' | 'REFERENCE_DATASET_ID' | 'TOTAL_TOKENS' | 'PROMPT_TOKENS' | 'COMPLETION_TOKENS' | 'TOTAL_COST' | 'PROMPT_COST' | 'COMPLETION_COST' | 'PROMPT_TOKEN_DETAILS' | 'COMPLETION_TOKEN_DETAILS' | 'PROMPT_COST_DETAILS' | 'COMPLETION_COST_DETAILS' | 'PRICE_MODEL_ID' | 'TAGS' | 'APP_PATH' | 'ATTACHMENTS' | 'THREAD_EVALUATION_TIME' | 'IS_IN_DATASET' | 'LAST_QUEUED_AT' | 'SHARE_URL' | 'FEEDBACK_STATS' | 'LS_USER_ID'>;
115
121
  /**
116
122
  * Header param: application/json
117
123
  */
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  // @ts-nocheck
5
- exports.VERSION = '0.0.1';
5
+ exports.VERSION = '0.0.1'; // x-release-please-version
@@ -1,2 +1,2 @@
1
1
  // @ts-nocheck
2
- export const VERSION = '0.0.1';
2
+ export const VERSION = '0.0.1'; // x-release-please-version
package/dist/address.cjs CHANGED
@@ -1,33 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvAddressError = void 0;
4
- exports.normalizeAddress = normalizeAddress;
5
- exports.wireKeys = wireKeys;
3
+ exports.EvaluatorAddress = exports.ExperimentAddress = exports.AgentAddress = exports.EnvAddressError = void 0;
4
+ exports.ensureAgent = ensureAgent;
5
+ exports.ensureAddress = ensureAddress;
6
6
  exports.envNames = envNames;
7
- exports.toPayload = toPayload;
8
- exports.toWire = toWire;
9
- exports.fromWire = fromWire;
10
- exports.addressFromEnv = addressFromEnv;
11
- exports.seed = seed;
12
- exports.sameAddress = sameAddress;
13
- // Wire key per field; env var is `LANGSMITH_<WIRE KEY>`.
14
- const FIELDS = {
15
- agentId: "agent_id",
16
- agentEnvironment: "agent_environment",
17
- };
7
+ /**
8
+ * (beta) Addresses that name where runs are sent instead of a project.
9
+ * Enabled per workspace; a workspace without it rejects the runs.
10
+ *
11
+ * An `Address` names a tracing project held by a feature: an `AgentAddress`, an
12
+ * `ExperimentAddress` or the workspace's `EvaluatorAddress`. Any of them can be resolved to
13
+ * its project. Only an `AgentAddress` can receive traces, and ingestion rejects the
14
+ * others at its entry points.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { AgentAddress, traceable } from "langsmith";
19
+ *
20
+ * const support = new AgentAddress("customer-support", "production");
21
+ * const handle = traceable(fn, { address: support });
22
+ * ```
23
+ */
24
+ const env_js_1 = require("./utils/env.cjs");
25
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
18
26
  // The server's agent id rule: a DNS label, so a hostname can carry the id.
19
27
  const AGENT_ID_PATTERN = /^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/;
20
- // Not `utils/env`: it imports the package index, which imports this module.
21
- function getEnv(name) {
22
- try {
23
- // eslint-disable-next-line no-process-env
24
- return typeof process !== "undefined" ? process.env?.[name] : undefined;
25
- }
26
- catch {
27
- return undefined;
28
- }
29
- }
30
- /** The `LANGSMITH_AGENT_*` / project env vars name half an address, or both. */
28
+ const ENVIRONMENTS = [
29
+ "local",
30
+ "development",
31
+ "staging",
32
+ "production",
33
+ ];
34
+ // The wire form of an agent address. The parts are validated by `AgentAddress`.
35
+ const LRN_PATTERN = /^lrn:agents\/([^/]+)\/environments\/([^/]+)$/;
36
+ const ENV_NAMES = ["LANGSMITH_AGENT_ID", "LANGSMITH_AGENT_ENVIRONMENT"];
37
+ /** (beta) The `LANGSMITH_AGENT_*` / project env vars name half an address, or both. */
31
38
  class EnvAddressError extends Error {
32
39
  constructor(message) {
33
40
  super(message);
@@ -36,98 +43,168 @@ class EnvAddressError extends Error {
36
43
  }
37
44
  exports.EnvAddressError = EnvAddressError;
38
45
  /**
39
- * @internal Validate `value` and return a frozen copy of its fields.
40
- * @throws If it isn't an object with exactly the address fields, or a value is invalid.
46
+ * (beta) The address of an agent's environment.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * new AgentAddress("customer-support", "production");
51
+ * ```
41
52
  */
42
- function normalizeAddress(value) {
43
- if (value == null || typeof value !== "object" || Array.isArray(value)) {
44
- throw new Error("`address` must be an object: { agentId, agentEnvironment }.");
53
+ class AgentAddress {
54
+ /**
55
+ * @param id 1 to 63 lowercase ASCII letters, digits or hyphens, starting
56
+ * with a letter and ending with a letter or digit. The server creates the
57
+ * agent on first use.
58
+ * @param environment One of `local`, `development`, `staging` or `production`, in
59
+ * any case.
60
+ * @throws If a value is invalid.
61
+ */
62
+ constructor(id, environment) {
63
+ Object.defineProperty(this, "id", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ /** Always lowercase. */
70
+ Object.defineProperty(this, "environment", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: void 0
75
+ });
76
+ if (typeof id !== "string" || !AGENT_ID_PATTERN.test(id)) {
77
+ throw new Error("Address agent id must be 1 to 63 lowercase ASCII letters, digits, or " +
78
+ "hyphens, start with a letter, and end with a letter or digit, got " +
79
+ `${JSON.stringify(id)}. Use an id such as "support-agent".`);
80
+ }
81
+ const normalized = typeof environment === "string" ? environment.toLowerCase() : undefined;
82
+ if (normalized === undefined || !ENVIRONMENTS.includes(normalized)) {
83
+ throw new Error(`Address environment must be one of ${ENVIRONMENTS.join(", ")}, got ` +
84
+ `${JSON.stringify(environment)}.`);
85
+ }
86
+ this.id = id;
87
+ this.environment = normalized;
88
+ Object.freeze(this);
89
+ }
90
+ toApiAddress() {
91
+ return {
92
+ kind: "AGENT",
93
+ id: this.id,
94
+ environment: this.environment.toUpperCase(),
95
+ };
45
96
  }
46
- const fields = value;
47
- const unknown = Object.keys(fields).filter((key) => !(key in FIELDS));
48
- if (unknown.length > 0) {
49
- throw new Error(`Unknown address fields: ${unknown.join(", ")}.`);
97
+ /** @internal */
98
+ _toLrn() {
99
+ return `lrn:agents/${this.id}/environments/${this.environment}`;
50
100
  }
51
- for (const name of Object.keys(FIELDS)) {
52
- const field = fields[name];
53
- if (typeof field !== "string" || field === "") {
54
- throw new Error(`Address ${name} must be a non-empty string.`);
101
+ /**
102
+ * @internal Parse an LRN read from the wire, such as a `baggage` header. The
103
+ * value is untrusted, so it is not echoed in the error.
104
+ * @throws If `value` is not an agent LRN, or names an invalid id or environment.
105
+ */
106
+ static _fromLrn(value) {
107
+ const match = typeof value === "string" ? LRN_PATTERN.exec(value) : null;
108
+ if (match === null) {
109
+ throw new Error("Not an agent address.");
55
110
  }
111
+ return new AgentAddress(match[1], match[2]);
56
112
  }
57
- const { agentId, agentEnvironment } = fields;
58
- if (!AGENT_ID_PATTERN.test(agentId)) {
59
- throw new Error("Address agentId must be 1 to 63 lowercase ASCII letters, digits, or " +
60
- "hyphens, start with a letter, and end with a letter or digit, got " +
61
- `${JSON.stringify(agentId)}. Use an id such as "support-agent".`);
113
+ /**
114
+ * Read the agent named by `LANGSMITH_AGENT_ID` and
115
+ * `LANGSMITH_AGENT_ENVIRONMENT`; `undefined` if neither is set.
116
+ * @throws {EnvAddressError} If only one is set, or a value is invalid.
117
+ */
118
+ static fromEnv() {
119
+ const values = ENV_NAMES.map((name) => (0, env_js_1.getEnvironmentVariable)(name));
120
+ if (!values.some(Boolean)) {
121
+ return undefined;
122
+ }
123
+ try {
124
+ const missing = ENV_NAMES.filter((_, i) => !values[i]);
125
+ if (missing.length > 0) {
126
+ throw new Error(`An address needs ${missing.join(" and ")} as well.`);
127
+ }
128
+ return new AgentAddress(values[0], values[1]);
129
+ }
130
+ catch (e) {
131
+ const present = ENV_NAMES.flatMap((name, i) => values[i] ? [`${name}=${JSON.stringify(values[i])}`] : []).join(", ");
132
+ throw new EnvAddressError(`The LANGSMITH_AGENT_* env vars can't address a run (${present}): ${e.message}`);
133
+ }
62
134
  }
63
- return Object.freeze({ agentId, agentEnvironment });
64
135
  }
65
- /** @internal */
66
- function wireKeys() {
67
- return Object.values(FIELDS);
136
+ exports.AgentAddress = AgentAddress;
137
+ /** (beta) The address of an experiment's tracing project. */
138
+ class ExperimentAddress {
139
+ /**
140
+ * @param id The experiment's UUID.
141
+ * @throws If `id` is not a UUID.
142
+ */
143
+ constructor(id) {
144
+ /** Always lowercase. */
145
+ Object.defineProperty(this, "id", {
146
+ enumerable: true,
147
+ configurable: true,
148
+ writable: true,
149
+ value: void 0
150
+ });
151
+ if (typeof id !== "string" || !UUID_PATTERN.test(id)) {
152
+ throw new Error(`Experiment id must be a UUID, got ${JSON.stringify(id)}.`);
153
+ }
154
+ this.id = id.toLowerCase();
155
+ Object.freeze(this);
156
+ }
157
+ toApiAddress() {
158
+ return { kind: "EXPERIMENT", id: this.id };
159
+ }
68
160
  }
69
- /** @internal */
70
- function envNames() {
71
- return wireKeys().map((key) => `LANGSMITH_${key.toUpperCase()}`);
161
+ exports.ExperimentAddress = ExperimentAddress;
162
+ /** (beta) The address of the workspace's shared evaluators project. */
163
+ class EvaluatorAddress {
164
+ constructor() {
165
+ Object.freeze(this);
166
+ }
167
+ toApiAddress() {
168
+ return { kind: "EVALUATOR" };
169
+ }
72
170
  }
171
+ exports.EvaluatorAddress = EvaluatorAddress;
73
172
  /**
74
- * @internal The `address` object of a run / feedback payload. `kind` and
75
- * `environment` are uppercase and case-sensitive on the wire.
173
+ * @internal The ingestion entry-point guard: `value` must be an `AgentAddress` (or
174
+ * absent), and is returned as is. Other addresses and strings are rejected.
175
+ * @throws If `value` is not an `AgentAddress`.
76
176
  */
77
- function toPayload(address) {
78
- return {
79
- address: {
80
- kind: "AGENT",
81
- id: address.agentId,
82
- environment: address.agentEnvironment.toUpperCase(),
83
- },
84
- };
85
- }
86
- /** @internal Header / replica fields. */
87
- function toWire(address) {
88
- return {
89
- [FIELDS.agentId]: address.agentId,
90
- [FIELDS.agentEnvironment]: address.agentEnvironment,
91
- };
92
- }
93
- /** @internal `undefined` if no field is set; throws if only some are. */
94
- function fromWire(values) {
95
- const fields = Object.fromEntries(Object.entries(FIELDS).map(([name, key]) => [
96
- name,
97
- values[key] ?? undefined,
98
- ]));
99
- const missing = Object.entries(FIELDS)
100
- .filter(([name]) => fields[name] === undefined)
101
- .map(([, key]) => key);
102
- if (missing.length === Object.keys(FIELDS).length) {
177
+ function ensureAgent(value) {
178
+ if (value == null) {
103
179
  return undefined;
104
180
  }
105
- if (missing.length > 0) {
106
- throw new Error(`An address needs ${missing.join(" and ")} as well.`);
181
+ if (value instanceof AgentAddress) {
182
+ return value;
183
+ }
184
+ if (typeof value === "object" &&
185
+ typeof value.toApiAddress === "function") {
186
+ throw new Error(`Only an AgentAddress can receive traces, got ${value.constructor?.name ?? "an unknown address"}.`);
107
187
  }
108
- return normalizeAddress(fields);
188
+ throw new Error("address must be an AgentAddress such as `new AgentAddress(id, environment)`, got " +
189
+ `${typeof value === "string" ? JSON.stringify(value) : typeof value}.`);
109
190
  }
110
191
  /**
111
- * @internal
112
- * @throws {EnvAddressError} If only some `LANGSMITH_AGENT_*` vars are set, or one is invalid.
192
+ * Return `value` if it is an `Address` of any kind, as a lookup takes it.
193
+ * For the calls that only resolve an address to its project, such as
194
+ * `getRunUrl`. Whatever receives traces takes `ensureAgent`.
113
195
  */
114
- function addressFromEnv() {
115
- const values = Object.fromEntries(wireKeys().map((key, i) => [key, getEnv(envNames()[i]) || undefined]));
116
- try {
117
- return fromWire(values);
196
+ function ensureAddress(value) {
197
+ if (value == null) {
198
+ return undefined;
118
199
  }
119
- catch (e) {
120
- throw new EnvAddressError(`The LANGSMITH_AGENT_* env vars can't address a run: ${e.message}`);
200
+ if (typeof value === "object" &&
201
+ typeof value.toApiAddress === "function") {
202
+ return value;
121
203
  }
204
+ throw new Error("address must be an AgentAddress, ExperimentAddress or EvaluatorAddress, got " +
205
+ `${typeof value === "string" ? JSON.stringify(value) : typeof value}.`);
122
206
  }
123
- /** @internal Replica id derivation seed. */
124
- function seed(address) {
125
- return ["agent", ...Object.values(toWire(address))].join("/");
126
- }
127
- /** @internal */
128
- function sameAddress(a, b) {
129
- if (!a || !b) {
130
- return a === b;
131
- }
132
- return a.agentId === b.agentId && a.agentEnvironment === b.agentEnvironment;
207
+ /** @internal The `LANGSMITH_AGENT_*` env var names an address reads. */
208
+ function envNames() {
209
+ return ENV_NAMES;
133
210
  }
package/dist/address.d.ts CHANGED
@@ -1,13 +1,68 @@
1
- /**
2
- * (beta) An address that runs are sent to instead of a project.
3
- * Enabled per workspace; a workspace without it rejects the runs.
4
- */
1
+ /** (beta) An agent environment. */
2
+ export type Environment = "local" | "development" | "staging" | "production";
3
+ /** (beta) The backend's form of an address, as the resolve endpoint takes it. */
4
+ export type ApiAddress = {
5
+ kind: "AGENT" | "EXPERIMENT" | "EVALUATOR";
6
+ id?: string;
7
+ environment?: Uppercase<Environment>;
8
+ };
9
+ /** (beta) A feature that holds traces in a tracing project. */
5
10
  export interface Address {
6
- /** 1 to 63 lowercase ASCII letters, digits or hyphens; starts with a letter. */
7
- agentId: string;
8
- agentEnvironment: string;
11
+ /** The address in the backend's form. */
12
+ toApiAddress(): ApiAddress;
9
13
  }
10
- /** The `LANGSMITH_AGENT_*` / project env vars name half an address, or both. */
14
+ /** (beta) The `LANGSMITH_AGENT_*` / project env vars name half an address, or both. */
11
15
  export declare class EnvAddressError extends Error {
12
16
  constructor(message: string);
13
17
  }
18
+ /**
19
+ * (beta) The address of an agent's environment.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * new AgentAddress("customer-support", "production");
24
+ * ```
25
+ */
26
+ export declare class AgentAddress implements Address {
27
+ readonly id: string;
28
+ /** Always lowercase. */
29
+ readonly environment: Environment;
30
+ /**
31
+ * @param id 1 to 63 lowercase ASCII letters, digits or hyphens, starting
32
+ * with a letter and ending with a letter or digit. The server creates the
33
+ * agent on first use.
34
+ * @param environment One of `local`, `development`, `staging` or `production`, in
35
+ * any case.
36
+ * @throws If a value is invalid.
37
+ */
38
+ constructor(id: string, environment: Environment | (string & {}));
39
+ toApiAddress(): ApiAddress;
40
+ /**
41
+ * Read the agent named by `LANGSMITH_AGENT_ID` and
42
+ * `LANGSMITH_AGENT_ENVIRONMENT`; `undefined` if neither is set.
43
+ * @throws {EnvAddressError} If only one is set, or a value is invalid.
44
+ */
45
+ static fromEnv(): AgentAddress | undefined;
46
+ }
47
+ /** (beta) The address of an experiment's tracing project. */
48
+ export declare class ExperimentAddress implements Address {
49
+ /** Always lowercase. */
50
+ readonly id: string;
51
+ /**
52
+ * @param id The experiment's UUID.
53
+ * @throws If `id` is not a UUID.
54
+ */
55
+ constructor(id: string);
56
+ toApiAddress(): ApiAddress;
57
+ }
58
+ /** (beta) The address of the workspace's shared evaluators project. */
59
+ export declare class EvaluatorAddress implements Address {
60
+ constructor();
61
+ toApiAddress(): ApiAddress;
62
+ }
63
+ /**
64
+ * Return `value` if it is an `Address` of any kind, as a lookup takes it.
65
+ * For the calls that only resolve an address to its project, such as
66
+ * `getRunUrl`. Whatever receives traces takes `ensureAgent`.
67
+ */
68
+ export declare function ensureAddress(value: unknown): Address | undefined;