@to11ai/sdk 3.1.0-rc → 4.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -42
- package/dist/behaviors/store.d.ts +1 -1
- package/dist/client.cjs +17 -71
- package/dist/client.d.ts +16 -112
- package/dist/client.js +22 -72
- package/dist/config.d.ts +3 -6
- package/dist/errors.cjs +0 -72
- package/dist/errors.d.ts +0 -77
- package/dist/errors.js +0 -72
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/types.gen.d.ts +2962 -17155
- package/dist/generated/zod.gen.d.ts +2576 -11477
- package/dist/projectEnvironments/resource.cjs +0 -2
- package/dist/projectEnvironments/resource.d.ts +3 -12
- package/dist/projectEnvironments/resource.js +0 -2
- package/dist/projectEnvironments/types.d.ts +0 -2
- package/dist/{gateway.d.ts → prompts/convert.d.ts} +12 -75
- package/dist/prompts/resource.d.ts +7 -7
- package/dist/prompts/shape.d.ts +4 -5
- package/dist/prompts/types.d.ts +5 -17
- package/package.json +7 -24
- package/src/behaviors/store.ts +1 -1
- package/src/client.ts +36 -242
- package/src/config.ts +3 -9
- package/src/errors.ts +0 -132
- package/src/generated/index.ts +1 -1
- package/src/generated/types.gen.ts +2889 -17652
- package/src/generated/zod.gen.ts +1980 -9967
- package/src/projectEnvironments/resource.ts +3 -14
- package/src/projectEnvironments/types.ts +0 -2
- package/src/{gateway.ts → prompts/convert.ts} +18 -152
- package/src/prompts/resource.ts +10 -12
- package/src/prompts/shape.ts +4 -5
- package/src/prompts/types.ts +5 -17
- package/src/transport.ts +0 -133
- package/dist/context.d.ts +0 -124
- package/dist/gateway.cjs +0 -264
- package/dist/gateway.js +0 -226
- package/dist/routingRules/resource.d.ts +0 -64
- package/dist/routingRules/types.d.ts +0 -88
- package/dist/traceparent.d.ts +0 -61
- package/dist/usage/resource.cjs +0 -113
- package/dist/usage/resource.d.ts +0 -29
- package/dist/usage/resource.js +0 -78
- package/dist/usage/types.d.ts +0 -81
- package/src/context.ts +0 -196
- package/src/routingRules/resource.ts +0 -261
- package/src/routingRules/types.ts +0 -120
- package/src/traceparent.ts +0 -189
- package/src/usage/resource.ts +0 -183
- package/src/usage/types.ts +0 -121
package/dist/context.d.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Observability context — the `session → conversation → turn` hierarchy that
|
|
3
|
-
* maps onto to11's telemetry grouping (Session → Conversation → Turn →
|
|
4
|
-
* LLM-Call). Each level provides a lazy default for the level below; explicit
|
|
5
|
-
* control is opt-in per use case. See
|
|
6
|
-
* docs/archive-2026-08/specs/2026-07-14-to11-2691-sdk-simpler-setup.md.
|
|
7
|
-
*
|
|
8
|
-
* Header-only: `turn.headers(prompt?)` returns a plain object the caller applies
|
|
9
|
-
* to their own provider client. It never sends a request.
|
|
10
|
-
*/
|
|
11
|
-
import { type GatewayPromptProvenance } from "./gateway";
|
|
12
|
-
/** Auth/config the client hands to the context for gateway header building. */
|
|
13
|
-
export interface TurnAuthContext {
|
|
14
|
-
readonly apiKey: string;
|
|
15
|
-
readonly projectId: string;
|
|
16
|
-
readonly env?: string;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* A prompt argument accepted by `turn.headers()`. Either the flat provenance
|
|
20
|
-
* (a neutral `RenderedPrompt`) or a `format`-shaped result carrying it under
|
|
21
|
-
* `metadata` — both are accepted so the format and neutral render paths carry
|
|
22
|
-
* provenance identically.
|
|
23
|
-
*/
|
|
24
|
-
export type PromptForHeaders = GatewayPromptProvenance | {
|
|
25
|
-
metadata: GatewayPromptProvenance;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* One agent turn — a single trace. A turn's `traceparent` is fixed for its
|
|
29
|
-
* lifetime, so every LLM-call made under it groups into one trace.
|
|
30
|
-
*/
|
|
31
|
-
export declare class Turn {
|
|
32
|
-
private readonly auth;
|
|
33
|
-
readonly sessionId: string;
|
|
34
|
-
readonly conversationId: string;
|
|
35
|
-
/** The fixed W3C traceparent for this turn (one trace). */
|
|
36
|
-
readonly traceparent: string;
|
|
37
|
-
constructor(auth: TurnAuthContext, sessionId: string, conversationId: string, traceparent?: string);
|
|
38
|
-
/**
|
|
39
|
-
* The complete gateway header bag: auth + session + conversation +
|
|
40
|
-
* traceparent, plus prompt provenance when a `prompt` is supplied. A pure
|
|
41
|
-
* read of the turn's fixed ids — reuse it across a tool-loop or call per
|
|
42
|
-
* request; the result is identical.
|
|
43
|
-
*
|
|
44
|
-
* Only valid for requests targeting the gateway (`GATEWAY_URL`); it carries
|
|
45
|
-
* the to11 API key and must never be attached to a direct provider endpoint.
|
|
46
|
-
*
|
|
47
|
-
* @example One call (inline turn)
|
|
48
|
-
* const res = await openai.chat.completions.create(
|
|
49
|
-
* { ...prompt.config, messages: prompt.messages },
|
|
50
|
-
* { headers: to11.turn().headers(prompt) },
|
|
51
|
-
* );
|
|
52
|
-
*
|
|
53
|
-
* @example Tool loop — hoist the turn so every call shares one trace
|
|
54
|
-
* const turn = to11.turn();
|
|
55
|
-
* const headers = turn.headers(prompt);
|
|
56
|
-
* while (true) {
|
|
57
|
-
* const res = await openai.chat.completions.create({ ...prompt.config, messages, tools }, { headers });
|
|
58
|
-
* // …reuse `headers` for every call in the loop…
|
|
59
|
-
* }
|
|
60
|
-
*/
|
|
61
|
-
headers(prompt?: PromptForHeaders): Record<string, string>;
|
|
62
|
-
}
|
|
63
|
-
/** One agent thread — a stable conversation id shared across all its turns. */
|
|
64
|
-
export declare class Conversation {
|
|
65
|
-
private readonly auth;
|
|
66
|
-
readonly sessionId: string;
|
|
67
|
-
/** Stable conversation id, reused by every turn created from this object. */
|
|
68
|
-
readonly id: string;
|
|
69
|
-
constructor(auth: TurnAuthContext, sessionId: string, id?: string);
|
|
70
|
-
/** Start a new turn (fresh trace) in this conversation. */
|
|
71
|
-
turn(): Turn;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* One unit of work. Groups conversations under a single session id. A session
|
|
75
|
-
* lazily owns one default conversation, used by `session.turn()`; call
|
|
76
|
-
* `session.conversation()` for additional (side/parallel) agent threads.
|
|
77
|
-
*/
|
|
78
|
-
export declare class Session {
|
|
79
|
-
private readonly auth;
|
|
80
|
-
/** Stable session id, carried by every conversation and turn beneath it. */
|
|
81
|
-
readonly id: string;
|
|
82
|
-
private defaultConversation;
|
|
83
|
-
constructor(auth: TurnAuthContext, id?: string);
|
|
84
|
-
/**
|
|
85
|
-
* Create a distinct conversation (a separate agent thread) in this session.
|
|
86
|
-
* Use this when one unit of work runs multiple agents (side / parallel
|
|
87
|
-
* agents): each conversation gets its own `x-to11-conversation-id`, all under
|
|
88
|
-
* the one session.
|
|
89
|
-
*
|
|
90
|
-
* @example Multiple agents in one session
|
|
91
|
-
* const session = to11.session();
|
|
92
|
-
* const main = session.conversation(); // main agent thread
|
|
93
|
-
* const research = session.conversation(); // separate research thread, same session
|
|
94
|
-
*
|
|
95
|
-
* await openai.chat.completions.create(
|
|
96
|
-
* { ...mainPrompt.config, messages: mainMessages },
|
|
97
|
-
* { headers: main.turn().headers(mainPrompt) },
|
|
98
|
-
* );
|
|
99
|
-
* await openai.chat.completions.create(
|
|
100
|
-
* { ...researchPrompt.config, messages: researchMessages },
|
|
101
|
-
* { headers: research.turn().headers(researchPrompt) },
|
|
102
|
-
* );
|
|
103
|
-
*/
|
|
104
|
-
conversation(id?: string): Conversation;
|
|
105
|
-
/**
|
|
106
|
-
* Start a turn in the session's single implicit conversation. The default
|
|
107
|
-
* conversation is created lazily on first call and reused, so consecutive
|
|
108
|
-
* `session.turn()` calls share one `x-to11-conversation-id` (each with its
|
|
109
|
-
* own `traceparent`).
|
|
110
|
-
*
|
|
111
|
-
* @example Multi-turn chat — every turn shares the session's conversation
|
|
112
|
-
* const session = to11.session();
|
|
113
|
-
* for await (const input of inputs) {
|
|
114
|
-
* messages.push({ role: "user", content: input });
|
|
115
|
-
* const turn = session.turn(); // fresh trace, same conversation id
|
|
116
|
-
* const res = await openai.chat.completions.create(
|
|
117
|
-
* { ...prompt.config, messages },
|
|
118
|
-
* { headers: turn.headers(prompt) },
|
|
119
|
-
* );
|
|
120
|
-
* messages.push(res.choices[0].message);
|
|
121
|
-
* }
|
|
122
|
-
*/
|
|
123
|
-
turn(): Turn;
|
|
124
|
-
}
|
package/dist/gateway.cjs
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
function __accessProp(key) {
|
|
6
|
-
return this[key];
|
|
7
|
-
}
|
|
8
|
-
var __toCommonJS = (from) => {
|
|
9
|
-
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
-
if (entry)
|
|
11
|
-
return entry;
|
|
12
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (var key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(entry, key))
|
|
16
|
-
__defProp(entry, key, {
|
|
17
|
-
get: __accessProp.bind(from, key),
|
|
18
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
__moduleCache.set(from, entry);
|
|
22
|
-
return entry;
|
|
23
|
-
};
|
|
24
|
-
var __moduleCache;
|
|
25
|
-
var __returnValue = (v) => v;
|
|
26
|
-
function __exportSetter(name, newValue) {
|
|
27
|
-
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
-
}
|
|
29
|
-
var __export = (target, all) => {
|
|
30
|
-
for (var name in all)
|
|
31
|
-
__defProp(target, name, {
|
|
32
|
-
get: all[name],
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
set: __exportSetter.bind(all, name)
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// src/gateway.ts
|
|
40
|
-
var exports_gateway = {};
|
|
41
|
-
__export(exports_gateway, {
|
|
42
|
-
gatewayAuthHeaders: () => gatewayAuthHeaders,
|
|
43
|
-
gatewayPromptHeaders: () => gatewayPromptHeaders,
|
|
44
|
-
toAnthropicMessages: () => toAnthropicMessages,
|
|
45
|
-
toAnthropicSystem: () => toAnthropicSystem,
|
|
46
|
-
toAnthropicToolChoice: () => toAnthropicToolChoice,
|
|
47
|
-
toAnthropicTools: () => toAnthropicTools,
|
|
48
|
-
toOpenAIMessages: () => toOpenAIMessages,
|
|
49
|
-
toOpenAIToolChoice: () => toOpenAIToolChoice,
|
|
50
|
-
toOpenAITools: () => toOpenAITools
|
|
51
|
-
});
|
|
52
|
-
module.exports = __toCommonJS(exports_gateway);
|
|
53
|
-
var import_errors = require("./errors");
|
|
54
|
-
var import_types = require("./prompts/types");
|
|
55
|
-
function gatewayAuthHeaders(options) {
|
|
56
|
-
if (options.apiKey.trim().length === 0) {
|
|
57
|
-
throw new Error("[to11ai/sdk] `apiKey` must not be empty.");
|
|
58
|
-
}
|
|
59
|
-
if (options.projectId.trim().length === 0) {
|
|
60
|
-
throw new Error("[to11ai/sdk] `projectId` must not be empty.");
|
|
61
|
-
}
|
|
62
|
-
const headers = {
|
|
63
|
-
"x-to11-authorization": `Bearer ${options.apiKey}`,
|
|
64
|
-
"x-to11-project-id": options.projectId
|
|
65
|
-
};
|
|
66
|
-
if (options.env !== undefined) {
|
|
67
|
-
if (options.env.trim().length === 0) {
|
|
68
|
-
throw new Error("[to11ai/sdk] `env` must not be empty — omit the field to use the project's gateway-fallback environment.");
|
|
69
|
-
}
|
|
70
|
-
if (!import_types.ENV_SLUG_REGEX.test(options.env)) {
|
|
71
|
-
throw new Error(`[to11ai/sdk] \`env\` must match ${import_types.ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(options.env)}`);
|
|
72
|
-
}
|
|
73
|
-
headers["x-to11-env"] = options.env;
|
|
74
|
-
}
|
|
75
|
-
return headers;
|
|
76
|
-
}
|
|
77
|
-
function gatewayPromptHeaders(prompt) {
|
|
78
|
-
const p = prompt != null && "metadata" in prompt ? prompt.metadata : prompt;
|
|
79
|
-
const headers = {};
|
|
80
|
-
if (p == null)
|
|
81
|
-
return headers;
|
|
82
|
-
if (typeof p.promptId === "string" && p.promptId.length > 0) {
|
|
83
|
-
headers["x-to11-prompt-id"] = p.promptId;
|
|
84
|
-
}
|
|
85
|
-
if (typeof p.version === "number" && Number.isFinite(p.version)) {
|
|
86
|
-
headers["x-to11-prompt-version"] = String(p.version);
|
|
87
|
-
}
|
|
88
|
-
if (typeof p.label === "string" && p.label.length > 0) {
|
|
89
|
-
headers["x-to11-prompt-label"] = p.label;
|
|
90
|
-
}
|
|
91
|
-
if (typeof p.slug === "string" && p.slug.length > 0) {
|
|
92
|
-
headers["x-to11-prompt-slug"] = p.slug;
|
|
93
|
-
}
|
|
94
|
-
return headers;
|
|
95
|
-
}
|
|
96
|
-
function toOpenAITools(tools) {
|
|
97
|
-
if (tools == null)
|
|
98
|
-
return [];
|
|
99
|
-
return tools.map((tool) => {
|
|
100
|
-
const fn = {
|
|
101
|
-
name: tool.name,
|
|
102
|
-
parameters: tool.parameters
|
|
103
|
-
};
|
|
104
|
-
if (typeof tool.description === "string") {
|
|
105
|
-
fn.description = tool.description;
|
|
106
|
-
}
|
|
107
|
-
return { type: "function", function: fn };
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
function toOpenAIToolChoice(toolChoice) {
|
|
111
|
-
if (toolChoice == null)
|
|
112
|
-
return;
|
|
113
|
-
if (typeof toolChoice === "string")
|
|
114
|
-
return toolChoice;
|
|
115
|
-
return { type: "function", function: { name: toolChoice.name } };
|
|
116
|
-
}
|
|
117
|
-
function toAnthropicTools(tools) {
|
|
118
|
-
if (tools == null)
|
|
119
|
-
return [];
|
|
120
|
-
return tools.map((tool) => {
|
|
121
|
-
const out = {
|
|
122
|
-
name: tool.name,
|
|
123
|
-
input_schema: tool.parameters
|
|
124
|
-
};
|
|
125
|
-
if (typeof tool.description === "string") {
|
|
126
|
-
out.description = tool.description;
|
|
127
|
-
}
|
|
128
|
-
return out;
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
function toAnthropicToolChoice(toolChoice) {
|
|
132
|
-
if (toolChoice == null)
|
|
133
|
-
return;
|
|
134
|
-
if (toolChoice === "auto")
|
|
135
|
-
return { type: "auto" };
|
|
136
|
-
if (toolChoice === "none")
|
|
137
|
-
return { type: "none" };
|
|
138
|
-
if (toolChoice === "required")
|
|
139
|
-
return { type: "any" };
|
|
140
|
-
return { type: "tool", name: toolChoice.name };
|
|
141
|
-
}
|
|
142
|
-
function requireToolCallId(id) {
|
|
143
|
-
if (typeof id !== "string" || id.length === 0) {
|
|
144
|
-
throw new Error("to11 SDK: malformed tool history — a tool call/result is missing its id (toolCallId). Author a non-empty id before converting messages.");
|
|
145
|
-
}
|
|
146
|
-
return id;
|
|
147
|
-
}
|
|
148
|
-
var KNOWN_RENDERED_ROLES = new Set([
|
|
149
|
-
"system",
|
|
150
|
-
"user",
|
|
151
|
-
"assistant",
|
|
152
|
-
"developer",
|
|
153
|
-
"tool"
|
|
154
|
-
]);
|
|
155
|
-
function skipUnknownRole(role, index, mode) {
|
|
156
|
-
if (KNOWN_RENDERED_ROLES.has(role))
|
|
157
|
-
return false;
|
|
158
|
-
if (mode === "error")
|
|
159
|
-
throw new import_errors.UnknownRoleError(role, index);
|
|
160
|
-
if (mode === "warn") {
|
|
161
|
-
console.warn(`[to11ai/sdk] dropped message[${index}] with unknown role ${JSON.stringify(role)} (expected system|user|assistant|developer|tool)`);
|
|
162
|
-
}
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
function toOpenAIMessages(messages, options = {}) {
|
|
166
|
-
const mode = options.onUnknownRole ?? "drop";
|
|
167
|
-
const out = [];
|
|
168
|
-
messages.forEach((message, index) => {
|
|
169
|
-
if (skipUnknownRole(message.role, index, mode))
|
|
170
|
-
return;
|
|
171
|
-
if (message.role === "tool") {
|
|
172
|
-
out.push({
|
|
173
|
-
role: "tool",
|
|
174
|
-
tool_call_id: requireToolCallId(message.toolCallId),
|
|
175
|
-
content: message.content ?? ""
|
|
176
|
-
});
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
if (message.role === "assistant") {
|
|
180
|
-
const content = message.content ?? null;
|
|
181
|
-
out.push(message.toolCalls != null && message.toolCalls.length > 0 ? {
|
|
182
|
-
role: "assistant",
|
|
183
|
-
content,
|
|
184
|
-
tool_calls: message.toolCalls.map((call) => ({
|
|
185
|
-
id: requireToolCallId(call.id),
|
|
186
|
-
type: "function",
|
|
187
|
-
function: {
|
|
188
|
-
name: call.name,
|
|
189
|
-
arguments: JSON.stringify(call.arguments)
|
|
190
|
-
}
|
|
191
|
-
}))
|
|
192
|
-
} : { role: "assistant", content });
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
const content = message.content ?? "";
|
|
196
|
-
if (message.role === "user") {
|
|
197
|
-
out.push({ role: "user", content });
|
|
198
|
-
} else if (message.role === "developer") {
|
|
199
|
-
out.push({ role: "developer", content });
|
|
200
|
-
} else {
|
|
201
|
-
out.push({ role: "system", content });
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
return out;
|
|
205
|
-
}
|
|
206
|
-
function toAnthropicMessages(messages, options = {}) {
|
|
207
|
-
const mode = options.onUnknownRole ?? "drop";
|
|
208
|
-
const out = [];
|
|
209
|
-
messages.forEach((message, index) => {
|
|
210
|
-
if (message.role === "system" || message.role === "developer") {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (skipUnknownRole(message.role, index, mode))
|
|
214
|
-
return;
|
|
215
|
-
if (message.role === "tool") {
|
|
216
|
-
out.push({
|
|
217
|
-
role: "user",
|
|
218
|
-
content: [
|
|
219
|
-
{
|
|
220
|
-
type: "tool_result",
|
|
221
|
-
tool_use_id: requireToolCallId(message.toolCallId),
|
|
222
|
-
content: message.content ?? ""
|
|
223
|
-
}
|
|
224
|
-
]
|
|
225
|
-
});
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
if (message.role === "assistant") {
|
|
229
|
-
if (message.toolCalls != null && message.toolCalls.length > 0) {
|
|
230
|
-
const blocks = [];
|
|
231
|
-
if (typeof message.content === "string" && message.content.length > 0) {
|
|
232
|
-
blocks.push({ type: "text", text: message.content });
|
|
233
|
-
}
|
|
234
|
-
for (const call of message.toolCalls) {
|
|
235
|
-
blocks.push({
|
|
236
|
-
type: "tool_use",
|
|
237
|
-
id: requireToolCallId(call.id),
|
|
238
|
-
name: call.name,
|
|
239
|
-
input: call.arguments
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
out.push({ role: "assistant", content: blocks });
|
|
243
|
-
} else {
|
|
244
|
-
out.push({ role: "assistant", content: message.content ?? "" });
|
|
245
|
-
}
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
out.push({ role: "user", content: message.content ?? "" });
|
|
249
|
-
});
|
|
250
|
-
return out;
|
|
251
|
-
}
|
|
252
|
-
function toAnthropicSystem(messages) {
|
|
253
|
-
const parts = [];
|
|
254
|
-
for (const message of messages) {
|
|
255
|
-
if (message.role !== "system" && message.role !== "developer")
|
|
256
|
-
continue;
|
|
257
|
-
if (typeof message.content === "string" && message.content.length > 0) {
|
|
258
|
-
parts.push(message.content);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
return parts.length > 0 ? parts.join(`
|
|
262
|
-
|
|
263
|
-
`) : undefined;
|
|
264
|
-
}
|
package/dist/gateway.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
// src/gateway.ts
|
|
2
|
-
import { UnknownRoleError } from "./errors";
|
|
3
|
-
import {
|
|
4
|
-
ENV_SLUG_REGEX
|
|
5
|
-
} from "./prompts/types";
|
|
6
|
-
function gatewayAuthHeaders(options) {
|
|
7
|
-
if (options.apiKey.trim().length === 0) {
|
|
8
|
-
throw new Error("[to11ai/sdk] `apiKey` must not be empty.");
|
|
9
|
-
}
|
|
10
|
-
if (options.projectId.trim().length === 0) {
|
|
11
|
-
throw new Error("[to11ai/sdk] `projectId` must not be empty.");
|
|
12
|
-
}
|
|
13
|
-
const headers = {
|
|
14
|
-
"x-to11-authorization": `Bearer ${options.apiKey}`,
|
|
15
|
-
"x-to11-project-id": options.projectId
|
|
16
|
-
};
|
|
17
|
-
if (options.env !== undefined) {
|
|
18
|
-
if (options.env.trim().length === 0) {
|
|
19
|
-
throw new Error("[to11ai/sdk] `env` must not be empty — omit the field to use the project's gateway-fallback environment.");
|
|
20
|
-
}
|
|
21
|
-
if (!ENV_SLUG_REGEX.test(options.env)) {
|
|
22
|
-
throw new Error(`[to11ai/sdk] \`env\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(options.env)}`);
|
|
23
|
-
}
|
|
24
|
-
headers["x-to11-env"] = options.env;
|
|
25
|
-
}
|
|
26
|
-
return headers;
|
|
27
|
-
}
|
|
28
|
-
function gatewayPromptHeaders(prompt) {
|
|
29
|
-
const p = prompt != null && "metadata" in prompt ? prompt.metadata : prompt;
|
|
30
|
-
const headers = {};
|
|
31
|
-
if (p == null)
|
|
32
|
-
return headers;
|
|
33
|
-
if (typeof p.promptId === "string" && p.promptId.length > 0) {
|
|
34
|
-
headers["x-to11-prompt-id"] = p.promptId;
|
|
35
|
-
}
|
|
36
|
-
if (typeof p.version === "number" && Number.isFinite(p.version)) {
|
|
37
|
-
headers["x-to11-prompt-version"] = String(p.version);
|
|
38
|
-
}
|
|
39
|
-
if (typeof p.label === "string" && p.label.length > 0) {
|
|
40
|
-
headers["x-to11-prompt-label"] = p.label;
|
|
41
|
-
}
|
|
42
|
-
if (typeof p.slug === "string" && p.slug.length > 0) {
|
|
43
|
-
headers["x-to11-prompt-slug"] = p.slug;
|
|
44
|
-
}
|
|
45
|
-
return headers;
|
|
46
|
-
}
|
|
47
|
-
function toOpenAITools(tools) {
|
|
48
|
-
if (tools == null)
|
|
49
|
-
return [];
|
|
50
|
-
return tools.map((tool) => {
|
|
51
|
-
const fn = {
|
|
52
|
-
name: tool.name,
|
|
53
|
-
parameters: tool.parameters
|
|
54
|
-
};
|
|
55
|
-
if (typeof tool.description === "string") {
|
|
56
|
-
fn.description = tool.description;
|
|
57
|
-
}
|
|
58
|
-
return { type: "function", function: fn };
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
function toOpenAIToolChoice(toolChoice) {
|
|
62
|
-
if (toolChoice == null)
|
|
63
|
-
return;
|
|
64
|
-
if (typeof toolChoice === "string")
|
|
65
|
-
return toolChoice;
|
|
66
|
-
return { type: "function", function: { name: toolChoice.name } };
|
|
67
|
-
}
|
|
68
|
-
function toAnthropicTools(tools) {
|
|
69
|
-
if (tools == null)
|
|
70
|
-
return [];
|
|
71
|
-
return tools.map((tool) => {
|
|
72
|
-
const out = {
|
|
73
|
-
name: tool.name,
|
|
74
|
-
input_schema: tool.parameters
|
|
75
|
-
};
|
|
76
|
-
if (typeof tool.description === "string") {
|
|
77
|
-
out.description = tool.description;
|
|
78
|
-
}
|
|
79
|
-
return out;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function toAnthropicToolChoice(toolChoice) {
|
|
83
|
-
if (toolChoice == null)
|
|
84
|
-
return;
|
|
85
|
-
if (toolChoice === "auto")
|
|
86
|
-
return { type: "auto" };
|
|
87
|
-
if (toolChoice === "none")
|
|
88
|
-
return { type: "none" };
|
|
89
|
-
if (toolChoice === "required")
|
|
90
|
-
return { type: "any" };
|
|
91
|
-
return { type: "tool", name: toolChoice.name };
|
|
92
|
-
}
|
|
93
|
-
function requireToolCallId(id) {
|
|
94
|
-
if (typeof id !== "string" || id.length === 0) {
|
|
95
|
-
throw new Error("to11 SDK: malformed tool history — a tool call/result is missing its id (toolCallId). Author a non-empty id before converting messages.");
|
|
96
|
-
}
|
|
97
|
-
return id;
|
|
98
|
-
}
|
|
99
|
-
var KNOWN_RENDERED_ROLES = new Set([
|
|
100
|
-
"system",
|
|
101
|
-
"user",
|
|
102
|
-
"assistant",
|
|
103
|
-
"developer",
|
|
104
|
-
"tool"
|
|
105
|
-
]);
|
|
106
|
-
function skipUnknownRole(role, index, mode) {
|
|
107
|
-
if (KNOWN_RENDERED_ROLES.has(role))
|
|
108
|
-
return false;
|
|
109
|
-
if (mode === "error")
|
|
110
|
-
throw new UnknownRoleError(role, index);
|
|
111
|
-
if (mode === "warn") {
|
|
112
|
-
console.warn(`[to11ai/sdk] dropped message[${index}] with unknown role ${JSON.stringify(role)} (expected system|user|assistant|developer|tool)`);
|
|
113
|
-
}
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
function toOpenAIMessages(messages, options = {}) {
|
|
117
|
-
const mode = options.onUnknownRole ?? "drop";
|
|
118
|
-
const out = [];
|
|
119
|
-
messages.forEach((message, index) => {
|
|
120
|
-
if (skipUnknownRole(message.role, index, mode))
|
|
121
|
-
return;
|
|
122
|
-
if (message.role === "tool") {
|
|
123
|
-
out.push({
|
|
124
|
-
role: "tool",
|
|
125
|
-
tool_call_id: requireToolCallId(message.toolCallId),
|
|
126
|
-
content: message.content ?? ""
|
|
127
|
-
});
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (message.role === "assistant") {
|
|
131
|
-
const content = message.content ?? null;
|
|
132
|
-
out.push(message.toolCalls != null && message.toolCalls.length > 0 ? {
|
|
133
|
-
role: "assistant",
|
|
134
|
-
content,
|
|
135
|
-
tool_calls: message.toolCalls.map((call) => ({
|
|
136
|
-
id: requireToolCallId(call.id),
|
|
137
|
-
type: "function",
|
|
138
|
-
function: {
|
|
139
|
-
name: call.name,
|
|
140
|
-
arguments: JSON.stringify(call.arguments)
|
|
141
|
-
}
|
|
142
|
-
}))
|
|
143
|
-
} : { role: "assistant", content });
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const content = message.content ?? "";
|
|
147
|
-
if (message.role === "user") {
|
|
148
|
-
out.push({ role: "user", content });
|
|
149
|
-
} else if (message.role === "developer") {
|
|
150
|
-
out.push({ role: "developer", content });
|
|
151
|
-
} else {
|
|
152
|
-
out.push({ role: "system", content });
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
return out;
|
|
156
|
-
}
|
|
157
|
-
function toAnthropicMessages(messages, options = {}) {
|
|
158
|
-
const mode = options.onUnknownRole ?? "drop";
|
|
159
|
-
const out = [];
|
|
160
|
-
messages.forEach((message, index) => {
|
|
161
|
-
if (message.role === "system" || message.role === "developer") {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
if (skipUnknownRole(message.role, index, mode))
|
|
165
|
-
return;
|
|
166
|
-
if (message.role === "tool") {
|
|
167
|
-
out.push({
|
|
168
|
-
role: "user",
|
|
169
|
-
content: [
|
|
170
|
-
{
|
|
171
|
-
type: "tool_result",
|
|
172
|
-
tool_use_id: requireToolCallId(message.toolCallId),
|
|
173
|
-
content: message.content ?? ""
|
|
174
|
-
}
|
|
175
|
-
]
|
|
176
|
-
});
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
if (message.role === "assistant") {
|
|
180
|
-
if (message.toolCalls != null && message.toolCalls.length > 0) {
|
|
181
|
-
const blocks = [];
|
|
182
|
-
if (typeof message.content === "string" && message.content.length > 0) {
|
|
183
|
-
blocks.push({ type: "text", text: message.content });
|
|
184
|
-
}
|
|
185
|
-
for (const call of message.toolCalls) {
|
|
186
|
-
blocks.push({
|
|
187
|
-
type: "tool_use",
|
|
188
|
-
id: requireToolCallId(call.id),
|
|
189
|
-
name: call.name,
|
|
190
|
-
input: call.arguments
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
out.push({ role: "assistant", content: blocks });
|
|
194
|
-
} else {
|
|
195
|
-
out.push({ role: "assistant", content: message.content ?? "" });
|
|
196
|
-
}
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
out.push({ role: "user", content: message.content ?? "" });
|
|
200
|
-
});
|
|
201
|
-
return out;
|
|
202
|
-
}
|
|
203
|
-
function toAnthropicSystem(messages) {
|
|
204
|
-
const parts = [];
|
|
205
|
-
for (const message of messages) {
|
|
206
|
-
if (message.role !== "system" && message.role !== "developer")
|
|
207
|
-
continue;
|
|
208
|
-
if (typeof message.content === "string" && message.content.length > 0) {
|
|
209
|
-
parts.push(message.content);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return parts.length > 0 ? parts.join(`
|
|
213
|
-
|
|
214
|
-
`) : undefined;
|
|
215
|
-
}
|
|
216
|
-
export {
|
|
217
|
-
gatewayAuthHeaders,
|
|
218
|
-
gatewayPromptHeaders,
|
|
219
|
-
toAnthropicMessages,
|
|
220
|
-
toAnthropicSystem,
|
|
221
|
-
toAnthropicToolChoice,
|
|
222
|
-
toAnthropicTools,
|
|
223
|
-
toOpenAIMessages,
|
|
224
|
-
toOpenAIToolChoice,
|
|
225
|
-
toOpenAITools
|
|
226
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { Transport } from "../transport";
|
|
2
|
-
import type { CreateRuleVersionInput, CreateRuleVersionResult, GetRuleVersionInput, GetRuleVersionResult, IterateRuleReleasesInput, IterateRuleVersionsInput, ListRuleReleasesInput, ListRuleReleasesResult, ListRuleVersionsInput, ListRuleVersionsResult, RollbackRuleReleaseInput, RollbackRuleReleaseResult, RuleRelease, RuleVersion, SetRuleReleaseInput, SetRuleReleaseResult } from "./types";
|
|
3
|
-
export type { CreateRuleVersionInput, CreateRuleVersionResult, GetRuleVersionInput, GetRuleVersionResult, IterateRuleReleasesInput, IterateRuleVersionsInput, ListRuleReleasesInput, ListRuleReleasesResult, ListRuleVersionsInput, ListRuleVersionsResult, RollbackRuleReleaseInput, RollbackRuleReleaseResult, RuleModelBinding, RuleProviderRef, RuleRelease, RuleReleaseActorType, RuleReleaseApprovalStatus, RuleReleaseChangeKind, RuleReleaseStrategy, RuleReleaseWeight, RuleSnapshot, RuleStrategy, RuleVersion, SetRuleReleaseInput, SetRuleReleaseResult, } from "./types";
|
|
4
|
-
export declare class RoutingRuleVersionResource {
|
|
5
|
-
private readonly transport;
|
|
6
|
-
constructor(transport: Transport);
|
|
7
|
-
create(input: CreateRuleVersionInput): Promise<CreateRuleVersionResult>;
|
|
8
|
-
/**
|
|
9
|
-
* One page of a rule's versions, newest first. The result mirrors the wire
|
|
10
|
-
* shape, `{ items, pagination: { nextCursor, limit } }`; pass
|
|
11
|
-
* `pagination.nextCursor` back as `cursor` until it is `null`. Pass only a
|
|
12
|
-
* cursor this list returned: a malformed or unreadable one answers 400
|
|
13
|
-
* `invalid_cursor`.
|
|
14
|
-
*/
|
|
15
|
-
list(input: ListRuleVersionsInput): Promise<ListRuleVersionsResult>;
|
|
16
|
-
/**
|
|
17
|
-
* Every version of a rule, newest first: walks `list()` page by page until
|
|
18
|
-
* `pagination.nextCursor` is `null` and yields one `RuleVersion` at a time.
|
|
19
|
-
* `limit` sets the page size of each request. Throws a `To11aiError` if the
|
|
20
|
-
* server repeats a cursor.
|
|
21
|
-
*
|
|
22
|
-
* ```ts
|
|
23
|
-
* for await (const version of rules.versions.iterate({ projectId, ruleId })) {
|
|
24
|
-
* console.log(version.version);
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
iterate(input: IterateRuleVersionsInput): AsyncGenerator<RuleVersion, void, undefined>;
|
|
29
|
-
get(input: GetRuleVersionInput): Promise<GetRuleVersionResult>;
|
|
30
|
-
}
|
|
31
|
-
export declare class RoutingRuleReleaseResource {
|
|
32
|
-
private readonly transport;
|
|
33
|
-
constructor(transport: Transport);
|
|
34
|
-
set(input: SetRuleReleaseInput): Promise<SetRuleReleaseResult>;
|
|
35
|
-
rollback(input: RollbackRuleReleaseInput): Promise<RollbackRuleReleaseResult>;
|
|
36
|
-
/**
|
|
37
|
-
* One page of a rule's releases, newest first. Without `envSlug` it lists
|
|
38
|
-
* every environment's releases; with it, one environment's. The result
|
|
39
|
-
* mirrors the wire shape, `{ items, pagination: { nextCursor, limit } }`;
|
|
40
|
-
* pass `pagination.nextCursor` back as `cursor` until it is `null`. Pass
|
|
41
|
-
* only a cursor this list returned: a malformed or unreadable one answers
|
|
42
|
-
* 400 `invalid_cursor`, while a well-formed cursor from another list is
|
|
43
|
-
* accepted and continues from its position, which can skip releases.
|
|
44
|
-
*/
|
|
45
|
-
list(input: ListRuleReleasesInput): Promise<ListRuleReleasesResult>;
|
|
46
|
-
/**
|
|
47
|
-
* Every release of a rule, newest first: walks `list()` page by page until
|
|
48
|
-
* `pagination.nextCursor` is `null` and yields one `RuleRelease` at a time.
|
|
49
|
-
* `limit` sets the page size of each request. Throws a `To11aiError` if the
|
|
50
|
-
* server repeats a cursor.
|
|
51
|
-
*
|
|
52
|
-
* ```ts
|
|
53
|
-
* for await (const release of rules.releases.iterate({ projectId, ruleId })) {
|
|
54
|
-
* console.log(release.envSlug, release.changeKind);
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
iterate(input: IterateRuleReleasesInput): AsyncGenerator<RuleRelease, void, undefined>;
|
|
59
|
-
}
|
|
60
|
-
export declare class RoutingRulesResource {
|
|
61
|
-
readonly versions: RoutingRuleVersionResource;
|
|
62
|
-
readonly releases: RoutingRuleReleaseResource;
|
|
63
|
-
constructor(transport: Transport);
|
|
64
|
-
}
|