@hilbras/sdk 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/output/schema-helpers.d.ts +67 -0
- package/dist/output/schema-helpers.d.ts.map +1 -0
- package/dist/output/schema-helpers.js +155 -0
- package/dist/output/schema-helpers.js.map +1 -0
- package/dist/telemetry/body-logger.d.ts +68 -0
- package/dist/telemetry/body-logger.d.ts.map +1 -0
- package/dist/telemetry/body-logger.js +140 -0
- package/dist/telemetry/body-logger.js.map +1 -0
- package/dist/telemetry/dashboard.d.ts +90 -0
- package/dist/telemetry/dashboard.d.ts.map +1 -0
- package/dist/telemetry/dashboard.js +156 -0
- package/dist/telemetry/dashboard.js.map +1 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +11 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/opentelemetry.d.ts +65 -0
- package/dist/telemetry/opentelemetry.d.ts.map +1 -0
- package/dist/telemetry/opentelemetry.js +172 -0
- package/dist/telemetry/opentelemetry.js.map +1 -0
- package/dist/telemetry/structured-logger.d.ts +104 -0
- package/dist/telemetry/structured-logger.d.ts.map +1 -0
- package/dist/telemetry/structured-logger.js +170 -0
- package/dist/telemetry/structured-logger.js.map +1 -0
- package/dist/types/tool-builder.d.ts +120 -0
- package/dist/types/tool-builder.d.ts.map +1 -0
- package/dist/types/tool-builder.js +142 -0
- package/dist/types/tool-builder.js.map +1 -0
- package/dist/utils/id.d.ts +43 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/id.js +76 -0
- package/dist/utils/id.js.map +1 -0
- package/dist/utils/sse.d.ts +81 -0
- package/dist/utils/sse.d.ts.map +1 -0
- package/dist/utils/sse.js +150 -0
- package/dist/utils/sse.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Structured Logger
|
|
3
|
+
*
|
|
4
|
+
* JSON-structured logging for production environments.
|
|
5
|
+
* Outputs newline-delimited JSON (NDJSON) for easy log aggregation.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { StructuredLogger } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* const logger = new StructuredLogger({ serviceName: "my-app", level: "info" });
|
|
11
|
+
* client.on("request.completed", (e) => logger.logRequest(e));
|
|
12
|
+
* client.on("request.failed", (e) => logger.logError(e));
|
|
13
|
+
*
|
|
14
|
+
* // Or pipe to your own destination
|
|
15
|
+
* const logger = new StructuredLogger({
|
|
16
|
+
* destination: (entry) => myLogService.send(entry),
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
import { redact } from "../logging/logger.js";
|
|
20
|
+
const LEVEL_PRIORITY = {
|
|
21
|
+
debug: 0,
|
|
22
|
+
info: 1,
|
|
23
|
+
warn: 2,
|
|
24
|
+
error: 3,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Structured JSON logger for production environments.
|
|
28
|
+
* Outputs NDJSON (newline-delimited JSON) for easy log aggregation.
|
|
29
|
+
*/
|
|
30
|
+
export class StructuredLogger {
|
|
31
|
+
_config;
|
|
32
|
+
constructor(config) {
|
|
33
|
+
this._config = {
|
|
34
|
+
serviceName: config?.serviceName ?? "hilbras-sdk",
|
|
35
|
+
level: config?.level ?? "info",
|
|
36
|
+
destination: config?.destination ?? ((entry) => console.log(JSON.stringify(entry))),
|
|
37
|
+
redact: config?.redact ?? true,
|
|
38
|
+
defaultFields: config?.defaultFields ?? {},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Get the current log level */
|
|
42
|
+
get level() {
|
|
43
|
+
return this._config.level;
|
|
44
|
+
}
|
|
45
|
+
/** Set the log level at runtime */
|
|
46
|
+
set level(level) {
|
|
47
|
+
this._config.level = level;
|
|
48
|
+
}
|
|
49
|
+
/** Log a raw structured entry */
|
|
50
|
+
log(level, category, data) {
|
|
51
|
+
if (LEVEL_PRIORITY[level] < LEVEL_PRIORITY[this._config.level])
|
|
52
|
+
return;
|
|
53
|
+
const entry = {
|
|
54
|
+
timestamp: new Date().toISOString(),
|
|
55
|
+
level,
|
|
56
|
+
category,
|
|
57
|
+
service: this._config.serviceName,
|
|
58
|
+
...this._config.defaultFields,
|
|
59
|
+
...data,
|
|
60
|
+
};
|
|
61
|
+
if (this._config.redact) {
|
|
62
|
+
this._redactEntry(entry);
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
this._config.destination(entry);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Swallow destination errors to never break the SDK
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Log a completed request */
|
|
72
|
+
logRequest(event) {
|
|
73
|
+
this.log("info", "request", {
|
|
74
|
+
requestId: event.requestId,
|
|
75
|
+
provider: event.provider,
|
|
76
|
+
model: event.model,
|
|
77
|
+
durationMs: event.durationMs,
|
|
78
|
+
tokens: {
|
|
79
|
+
input: event.inputTokens,
|
|
80
|
+
output: event.outputTokens,
|
|
81
|
+
total: (event.inputTokens ?? 0) + (event.outputTokens ?? 0),
|
|
82
|
+
},
|
|
83
|
+
success: true,
|
|
84
|
+
meta: { attempts: event.attempts, structuredOutput: event.structuredOutput },
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Log a failed request */
|
|
88
|
+
logError(event) {
|
|
89
|
+
this.log("error", "request", {
|
|
90
|
+
requestId: event.requestId,
|
|
91
|
+
provider: event.provider,
|
|
92
|
+
model: event.model,
|
|
93
|
+
durationMs: event.durationMs,
|
|
94
|
+
error: event.error,
|
|
95
|
+
success: false,
|
|
96
|
+
meta: { attempts: event.attempts },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** Log a retry event */
|
|
100
|
+
logRetry(event) {
|
|
101
|
+
this.log("warn", "retry", {
|
|
102
|
+
requestId: event.requestId,
|
|
103
|
+
provider: event.provider,
|
|
104
|
+
meta: { attempt: event.attempt, delayMs: event.delayMs, reason: event.reason },
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** Log a first-chunk latency event */
|
|
108
|
+
logFirstChunk(event) {
|
|
109
|
+
this.log("debug", "stream", {
|
|
110
|
+
requestId: event.requestId,
|
|
111
|
+
durationMs: event.latencyMs,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** Log a debug message */
|
|
115
|
+
debug(category, data) {
|
|
116
|
+
this.log("debug", category, data);
|
|
117
|
+
}
|
|
118
|
+
/** Log an info message */
|
|
119
|
+
info(category, data) {
|
|
120
|
+
this.log("info", category, data);
|
|
121
|
+
}
|
|
122
|
+
/** Log a warning message */
|
|
123
|
+
warn(category, data) {
|
|
124
|
+
this.log("warn", category, data);
|
|
125
|
+
}
|
|
126
|
+
/** Log an error message */
|
|
127
|
+
error(category, data) {
|
|
128
|
+
this.log("error", category, data);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Automatically instrument a HilbrasClient by subscribing to hook events.
|
|
132
|
+
* Returns an unsubscribe function.
|
|
133
|
+
*/
|
|
134
|
+
instrumentClient(client) {
|
|
135
|
+
const unsub1 = client.on("request.completed", (e) => {
|
|
136
|
+
if (e.type === "request.completed")
|
|
137
|
+
this.logRequest(e);
|
|
138
|
+
});
|
|
139
|
+
const unsub2 = client.on("request.failed", (e) => {
|
|
140
|
+
if (e.type === "request.failed")
|
|
141
|
+
this.logError(e);
|
|
142
|
+
});
|
|
143
|
+
const unsub3 = client.on("request.retrying", (e) => {
|
|
144
|
+
if (e.type === "request.retrying")
|
|
145
|
+
this.logRetry(e);
|
|
146
|
+
});
|
|
147
|
+
const unsub4 = client.on("stream.first_chunk", (e) => {
|
|
148
|
+
if (e.type === "stream.first_chunk")
|
|
149
|
+
this.logFirstChunk(e);
|
|
150
|
+
});
|
|
151
|
+
return () => {
|
|
152
|
+
unsub1();
|
|
153
|
+
unsub2();
|
|
154
|
+
unsub3();
|
|
155
|
+
unsub4();
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
_redactEntry(entry) {
|
|
159
|
+
if (entry.error)
|
|
160
|
+
entry.error = redact(entry.error);
|
|
161
|
+
if (entry.meta) {
|
|
162
|
+
for (const [key, value] of Object.entries(entry.meta)) {
|
|
163
|
+
if (typeof value === "string") {
|
|
164
|
+
entry.meta[key] = redact(value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=structured-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-logger.js","sourceRoot":"","sources":["../../src/telemetry/structured-logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AA+C9C,MAAM,cAAc,GAA6B;IAC/C,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;CACT,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IACnB,OAAO,CAAmC;IAElD,YAAY,MAA+B;QACzC,IAAI,CAAC,OAAO,GAAG;YACb,WAAW,EAAE,MAAM,EAAE,WAAW,IAAI,aAAa;YACjD,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,MAAM;YAC9B,WAAW,EAAE,MAAM,EAAE,WAAW,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;YACnF,MAAM,EAAE,MAAM,EAAE,MAAM,IAAI,IAAI;YAC9B,aAAa,EAAE,MAAM,EAAE,aAA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AAC,CAAC,EAAE,EAAE;YACnD,IAAI,CAAC,CAAC,IAAI,KAAK,oBAAoB;gBAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAC;QACX,CAAC,CAAC;IACJ,CAAC;IAEO,YAAY,CAAC,KAAyB;QAC5C,IAAI,KAAK,CAAC,KAAK;YAAE,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACnD,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC;YACf,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBAC9B,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Tool Builder
|
|
3
|
+
*
|
|
4
|
+
* Type-safe helpers for defining tools with schemas.
|
|
5
|
+
* Supports both static and runtime-defined tools.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { tool } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* const weatherTool = tool({
|
|
11
|
+
* name: "get_weather",
|
|
12
|
+
* description: "Get the weather for a location",
|
|
13
|
+
* parameters: {
|
|
14
|
+
* location: { type: "string", description: "City name" },
|
|
15
|
+
* unit: { type: "string", enum: ["celsius", "fahrenheit"] },
|
|
16
|
+
* },
|
|
17
|
+
* required: ["location"],
|
|
18
|
+
* execute: async (input) => {
|
|
19
|
+
* return { temp: 22, condition: "sunny" };
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Use with client
|
|
24
|
+
* const result = await client.complete({
|
|
25
|
+
* messages: [...],
|
|
26
|
+
* tools: [weatherTool.definition],
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Or execute directly
|
|
30
|
+
* const weather = await weatherTool.execute({ location: "London" });
|
|
31
|
+
*/
|
|
32
|
+
import type { Tool, ToolParameters } from "../types/tools.js";
|
|
33
|
+
/** Schema definition for tool parameters (simplified, user-friendly format) */
|
|
34
|
+
export type ParameterSchema = Record<string, {
|
|
35
|
+
type: string;
|
|
36
|
+
description?: string;
|
|
37
|
+
enum?: string[];
|
|
38
|
+
properties?: Record<string, ParameterSchema[string]>;
|
|
39
|
+
required?: string[];
|
|
40
|
+
}>;
|
|
41
|
+
/** A tool with both definition and execution function */
|
|
42
|
+
export interface DefinedTool<TInput = Record<string, unknown>, TOutput = unknown> {
|
|
43
|
+
/** The tool definition for passing to the LLM */
|
|
44
|
+
definition: Tool;
|
|
45
|
+
/** Execute the tool with validated input */
|
|
46
|
+
inputSchema: ParameterSchema;
|
|
47
|
+
required: string[];
|
|
48
|
+
execute: (input: TInput) => Promise<TOutput>;
|
|
49
|
+
}
|
|
50
|
+
/** A tool definition without an execute function (for LLM-only use) */
|
|
51
|
+
export type ToolDefinition = Tool;
|
|
52
|
+
/**
|
|
53
|
+
* Define a type-safe tool with schema and execution function.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const calculator = tool({
|
|
57
|
+
* name: "calculate",
|
|
58
|
+
* description: "Perform a math calculation",
|
|
59
|
+
* parameters: {
|
|
60
|
+
* expression: { type: "string", description: "Math expression" },
|
|
61
|
+
* },
|
|
62
|
+
* required: ["expression"],
|
|
63
|
+
* execute: async (input) => {
|
|
64
|
+
* return { result: eval(input.expression) };
|
|
65
|
+
* },
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* // Access the definition for LLM
|
|
69
|
+
* console.log(calculator.definition);
|
|
70
|
+
*
|
|
71
|
+
* // Execute directly
|
|
72
|
+
* const result = await calculator.execute({ expression: "2 + 2" });
|
|
73
|
+
*/
|
|
74
|
+
export declare function tool<TInput extends Record<string, unknown> = Record<string, unknown>, TOutput = unknown>(config: {
|
|
75
|
+
name: string;
|
|
76
|
+
description: string;
|
|
77
|
+
parameters: ParameterSchema;
|
|
78
|
+
required: (keyof TInput & string)[];
|
|
79
|
+
execute: (input: TInput) => Promise<TOutput>;
|
|
80
|
+
}): DefinedTool<TInput, TOutput>;
|
|
81
|
+
/**
|
|
82
|
+
* Create a tool definition without an execute function.
|
|
83
|
+
* Useful when you only need the schema for the LLM,
|
|
84
|
+
* or when execution is handled elsewhere.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const def = toolDef({
|
|
88
|
+
* name: "search",
|
|
89
|
+
* description: "Search the web",
|
|
90
|
+
* parameters: {
|
|
91
|
+
* query: { type: "string", description: "Search query" },
|
|
92
|
+
* },
|
|
93
|
+
* required: ["query"],
|
|
94
|
+
* });
|
|
95
|
+
*
|
|
96
|
+
* // Pass to LLM
|
|
97
|
+
* await client.complete({ messages, tools: [def] });
|
|
98
|
+
*/
|
|
99
|
+
export declare function toolDef(config: {
|
|
100
|
+
name: string;
|
|
101
|
+
description: string;
|
|
102
|
+
parameters: ParameterSchema;
|
|
103
|
+
required?: string[];
|
|
104
|
+
}): Tool;
|
|
105
|
+
/**
|
|
106
|
+
* Create a tool from a runtime-defined schema.
|
|
107
|
+
* Useful when tool definitions come from an API or database.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* const tools = await fetchToolDefinitionsFromAPI();
|
|
111
|
+
* const dynamicTools = tools.map(dynamicTool);
|
|
112
|
+
* await client.complete({ messages, tools: dynamicTools });
|
|
113
|
+
*/
|
|
114
|
+
export declare function dynamicTool(config: {
|
|
115
|
+
name: string;
|
|
116
|
+
description: string;
|
|
117
|
+
parameters: ToolParameters;
|
|
118
|
+
execute?: (input: Record<string, unknown>) => Promise<unknown>;
|
|
119
|
+
}): Tool | DefinedTool;
|
|
120
|
+
//# sourceMappingURL=tool-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-builder.d.ts","sourceRoot":"","sources":["../../src/types/tool-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAE7E,+EAA+E;AAC/E,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;IACrD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC,CAAC;AAEH,yDAAyD;AACzD,MAAM,WAAW,WAAW,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO;IAC9E,iDAAiD;IACjD,UAAU,EAAE,IAAI,CAAC;IACjB,4CAA4C;IAC5C,WAAW,EAAE,eAAe,CAAC;IAC7B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C;AAED,uEAAuE;AACvE,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,IAAI,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,EAAE,MAAM,EAAE;IAChH,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,eAAe,CAAC;IAC5B,QAAQ,EAAE,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IACpC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CA0B/B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,eAAe,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,GAAG,IAAI,CAmBP;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,cAAc,CAAC;IAC3B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAChE,GAAG,IAAI,GAAG,WAAW,CAoBrB"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Tool Builder
|
|
3
|
+
*
|
|
4
|
+
* Type-safe helpers for defining tools with schemas.
|
|
5
|
+
* Supports both static and runtime-defined tools.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { tool } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* const weatherTool = tool({
|
|
11
|
+
* name: "get_weather",
|
|
12
|
+
* description: "Get the weather for a location",
|
|
13
|
+
* parameters: {
|
|
14
|
+
* location: { type: "string", description: "City name" },
|
|
15
|
+
* unit: { type: "string", enum: ["celsius", "fahrenheit"] },
|
|
16
|
+
* },
|
|
17
|
+
* required: ["location"],
|
|
18
|
+
* execute: async (input) => {
|
|
19
|
+
* return { temp: 22, condition: "sunny" };
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Use with client
|
|
24
|
+
* const result = await client.complete({
|
|
25
|
+
* messages: [...],
|
|
26
|
+
* tools: [weatherTool.definition],
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Or execute directly
|
|
30
|
+
* const weather = await weatherTool.execute({ location: "London" });
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* Define a type-safe tool with schema and execution function.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const calculator = tool({
|
|
37
|
+
* name: "calculate",
|
|
38
|
+
* description: "Perform a math calculation",
|
|
39
|
+
* parameters: {
|
|
40
|
+
* expression: { type: "string", description: "Math expression" },
|
|
41
|
+
* },
|
|
42
|
+
* required: ["expression"],
|
|
43
|
+
* execute: async (input) => {
|
|
44
|
+
* return { result: eval(input.expression) };
|
|
45
|
+
* },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // Access the definition for LLM
|
|
49
|
+
* console.log(calculator.definition);
|
|
50
|
+
*
|
|
51
|
+
* // Execute directly
|
|
52
|
+
* const result = await calculator.execute({ expression: "2 + 2" });
|
|
53
|
+
*/
|
|
54
|
+
export function tool(config) {
|
|
55
|
+
const toolParameters = {
|
|
56
|
+
type: "object",
|
|
57
|
+
properties: {},
|
|
58
|
+
required: config.required,
|
|
59
|
+
};
|
|
60
|
+
for (const [key, def] of Object.entries(config.parameters)) {
|
|
61
|
+
toolParameters.properties[key] = def;
|
|
62
|
+
}
|
|
63
|
+
const definition = {
|
|
64
|
+
type: "function",
|
|
65
|
+
function: {
|
|
66
|
+
name: config.name,
|
|
67
|
+
description: config.description,
|
|
68
|
+
parameters: toolParameters,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
definition,
|
|
73
|
+
inputSchema: config.parameters,
|
|
74
|
+
required: config.required,
|
|
75
|
+
execute: config.execute,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create a tool definition without an execute function.
|
|
80
|
+
* Useful when you only need the schema for the LLM,
|
|
81
|
+
* or when execution is handled elsewhere.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const def = toolDef({
|
|
85
|
+
* name: "search",
|
|
86
|
+
* description: "Search the web",
|
|
87
|
+
* parameters: {
|
|
88
|
+
* query: { type: "string", description: "Search query" },
|
|
89
|
+
* },
|
|
90
|
+
* required: ["query"],
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* // Pass to LLM
|
|
94
|
+
* await client.complete({ messages, tools: [def] });
|
|
95
|
+
*/
|
|
96
|
+
export function toolDef(config) {
|
|
97
|
+
const toolParameters = {
|
|
98
|
+
type: "object",
|
|
99
|
+
properties: {},
|
|
100
|
+
required: config.required ?? [],
|
|
101
|
+
};
|
|
102
|
+
for (const [key, def] of Object.entries(config.parameters)) {
|
|
103
|
+
toolParameters.properties[key] = def;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
type: "function",
|
|
107
|
+
function: {
|
|
108
|
+
name: config.name,
|
|
109
|
+
description: config.description,
|
|
110
|
+
parameters: toolParameters,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create a tool from a runtime-defined schema.
|
|
116
|
+
* Useful when tool definitions come from an API or database.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* const tools = await fetchToolDefinitionsFromAPI();
|
|
120
|
+
* const dynamicTools = tools.map(dynamicTool);
|
|
121
|
+
* await client.complete({ messages, tools: dynamicTools });
|
|
122
|
+
*/
|
|
123
|
+
export function dynamicTool(config) {
|
|
124
|
+
const definition = {
|
|
125
|
+
type: "function",
|
|
126
|
+
function: {
|
|
127
|
+
name: config.name,
|
|
128
|
+
description: config.description,
|
|
129
|
+
parameters: config.parameters,
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
if (config.execute) {
|
|
133
|
+
return {
|
|
134
|
+
definition,
|
|
135
|
+
inputSchema: config.parameters.properties,
|
|
136
|
+
required: config.parameters.required,
|
|
137
|
+
execute: config.execute,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return definition;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=tool-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-builder.js","sourceRoot":"","sources":["../../src/types/tool-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AA0BH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,IAAI,CAAsF,MAMzG;IACC,MAAM,cAAc,GAAmB;QACrC,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE,EAAE;QACd,QAAQ,EAAE,MAAM,CAAC,QAAoB;KACtC,CAAC;IAEF,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;QAC3D,cAAc,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,GAAoB,CAAC;IACxD,CAAC;IAED,MAAM,UAAU,GAAS;QACvB,IAAI,EAAE,UAAU;QAChB,QAAQ,EAAE;YACR,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,UAAU,EAAE,cAAc;SAC3B;KACF,CAAC;IAEF,OAAO;QACL,UAAU;QACV,WAAW,EAAE,MAAM,CAAC,UAAU;QAC9B,QAAQ,EAAE,MAAM,CAAC,QAAoB;QACrC,OAAO,EAAE,MAAM,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for requests, tool calls, and other entities.
|
|
5
|
+
* Uses crypto.randomUUID() when available, falls back to a simple counter.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { generateId, createIdGenerator } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* const id = generateId(); // "a1b2c3d4-e5f6-..."
|
|
11
|
+
* const gen = createIdGenerator("req");
|
|
12
|
+
* const id1 = gen(); // "req_001"
|
|
13
|
+
* const id2 = gen(); // "req_002"
|
|
14
|
+
*/
|
|
15
|
+
/** Generate a unique ID (UUID v4 when available, fallback to random hex) */
|
|
16
|
+
export declare function generateId(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Create a sequential ID generator with a prefix.
|
|
19
|
+
*
|
|
20
|
+
* @param prefix - Optional prefix for generated IDs
|
|
21
|
+
* @param start - Starting number (default: 1)
|
|
22
|
+
* @returns A function that returns incrementing IDs
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const gen = createIdGenerator("req");
|
|
26
|
+
* gen(); // "req_001"
|
|
27
|
+
* gen(); // "req_002"
|
|
28
|
+
* gen(); // "req_003"
|
|
29
|
+
*
|
|
30
|
+
* const gen2 = createIdGenerator("call", 100);
|
|
31
|
+
* gen2(); // "call_100"
|
|
32
|
+
* gen2(); // "call_101"
|
|
33
|
+
*/
|
|
34
|
+
export declare function createIdGenerator(prefix?: string, start?: number): () => string;
|
|
35
|
+
/**
|
|
36
|
+
* Generate a short random ID (8 characters).
|
|
37
|
+
* Useful for correlation IDs, request tags, etc.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const tag = shortId(); // "a1b2c3d4"
|
|
41
|
+
*/
|
|
42
|
+
export declare function shortId(): string;
|
|
43
|
+
//# sourceMappingURL=id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../../src/utils/id.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,4EAA4E;AAC5E,wBAAgB,UAAU,IAAI,MAAM,CAkBnC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,SAAO,EAAE,KAAK,SAAI,GAAG,MAAM,MAAM,CAGxE;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,IAAI,MAAM,CAUhC"}
|
package/dist/utils/id.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — ID Generation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Generate unique IDs for requests, tool calls, and other entities.
|
|
5
|
+
* Uses crypto.randomUUID() when available, falls back to a simple counter.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { generateId, createIdGenerator } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* const id = generateId(); // "a1b2c3d4-e5f6-..."
|
|
11
|
+
* const gen = createIdGenerator("req");
|
|
12
|
+
* const id1 = gen(); // "req_001"
|
|
13
|
+
* const id2 = gen(); // "req_002"
|
|
14
|
+
*/
|
|
15
|
+
/** Generate a unique ID (UUID v4 when available, fallback to random hex) */
|
|
16
|
+
export function generateId() {
|
|
17
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
18
|
+
return crypto.randomUUID();
|
|
19
|
+
}
|
|
20
|
+
// Fallback: generate a random hex string
|
|
21
|
+
const arr = new Uint8Array(16);
|
|
22
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
23
|
+
crypto.getRandomValues(arr);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
for (let i = 0; i < 16; i++) {
|
|
27
|
+
arr[i] = Math.floor(Math.random() * 256);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Set version 4 and variant bits
|
|
31
|
+
arr[6] = (arr[6] & 0x0f) | 0x40;
|
|
32
|
+
arr[8] = (arr[8] & 0x3f) | 0x80;
|
|
33
|
+
const hex = Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
34
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a sequential ID generator with a prefix.
|
|
38
|
+
*
|
|
39
|
+
* @param prefix - Optional prefix for generated IDs
|
|
40
|
+
* @param start - Starting number (default: 1)
|
|
41
|
+
* @returns A function that returns incrementing IDs
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const gen = createIdGenerator("req");
|
|
45
|
+
* gen(); // "req_001"
|
|
46
|
+
* gen(); // "req_002"
|
|
47
|
+
* gen(); // "req_003"
|
|
48
|
+
*
|
|
49
|
+
* const gen2 = createIdGenerator("call", 100);
|
|
50
|
+
* gen2(); // "call_100"
|
|
51
|
+
* gen2(); // "call_101"
|
|
52
|
+
*/
|
|
53
|
+
export function createIdGenerator(prefix = "id", start = 1) {
|
|
54
|
+
let counter = start;
|
|
55
|
+
return () => `${prefix}_${String(counter++).padStart(6, "0")}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Generate a short random ID (8 characters).
|
|
59
|
+
* Useful for correlation IDs, request tags, etc.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const tag = shortId(); // "a1b2c3d4"
|
|
63
|
+
*/
|
|
64
|
+
export function shortId() {
|
|
65
|
+
const arr = new Uint8Array(4);
|
|
66
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
67
|
+
crypto.getRandomValues(arr);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
for (let i = 0; i < 4; i++) {
|
|
71
|
+
arr[i] = Math.floor(Math.random() * 256);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
75
|
+
}
|
|
76
|
+
//# sour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AAC,EAAE,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — SSE (Server-Sent Events) Parser
|
|
3
|
+
*
|
|
4
|
+
* Utilities for parsing SSE streams into structured events.
|
|
5
|
+
* Useful for consuming SSE endpoints directly or building custom adapters.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { parseJsonEventStream, parseSSEStream } from "@hilbras/sdk";
|
|
9
|
+
*
|
|
10
|
+
* // Parse a ReadableStream of SSE events
|
|
11
|
+
* for await (const event of parseSSEStream(response.body)) {
|
|
12
|
+
* console.log(event.event, event.data);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* // Parse SSE and auto-parse JSON data
|
|
16
|
+
* for await (const data of parseJsonEventStream(response.body)) {
|
|
17
|
+
* console.log(data); // parsed JSON object
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
/** A parsed SSE event */
|
|
21
|
+
export interface SSEEvent {
|
|
22
|
+
/** Event type (from "event:" field, defaults to "message") */
|
|
23
|
+
event: string;
|
|
24
|
+
/** Raw data string */
|
|
25
|
+
data: string;
|
|
26
|
+
/** Event ID (from "id:" field, if present) */
|
|
27
|
+
id?: string;
|
|
28
|
+
/** Retry interval (from "retry:" field, if present) */
|
|
29
|
+
retry?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parse a ReadableStream<Uint8Array> into SSE events.
|
|
33
|
+
* Handles multi-line data fields and optional fields.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const events = parseSSEStream(response.body);
|
|
37
|
+
* for await (const event of events) {
|
|
38
|
+
* if (event.event === "message") {
|
|
39
|
+
* const data = JSON.parse(event.data);
|
|
40
|
+
* processMessage(data);
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseSSEStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<SSEEvent>;
|
|
45
|
+
/**
|
|
46
|
+
* Parse SSE stream and auto-parse JSON data.
|
|
47
|
+
* Yields parsed JSON objects for each event with JSON data.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const stream = parseJsonEventStream(response.body);
|
|
51
|
+
* for await (const data of stream) {
|
|
52
|
+
* if (data.type === "text") {
|
|
53
|
+
* processText(data.text);
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
*/
|
|
57
|
+
export declare function parseJsonEventStream<T = unknown>(stream: ReadableStream<Uint8Array>): AsyncGenerator<{
|
|
58
|
+
event: string;
|
|
59
|
+
data: T;
|
|
60
|
+
id?: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Collect all events from an SSE stream into an array.
|
|
64
|
+
* Useful for testing or when you need all events at once.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const events = await collectSSEEvents(response.body);
|
|
68
|
+
* console.log(`Received ${events.length} events`);
|
|
69
|
+
*/
|
|
70
|
+
export declare function collectSSEEvents(stream: ReadableStream<Uint8Array>): Promise<SSEEvent[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Find the first event matching a given event type.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const message = await findSSEEvent(response.body, "message");
|
|
76
|
+
* if (message) {
|
|
77
|
+
* const data = JSON.parse(message.data);
|
|
78
|
+
* }
|
|
79
|
+
*/
|
|
80
|
+
export declare function findSSEEvent(stream: ReadableStream<Uint8Array>, eventType: string): Promise<SSEEvent | null>;
|
|
81
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/utils/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,yBAAyB;AACzB,MAAM,WAAW,QAAQ;IACvB,8DAA8D;IAC9D,KAAK,EAAE,MAAM,CAAC;IACd,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,8CAA8C;IAC9C,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAuB,cAAc,CACnC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,QAAQ,CAAC,CA6B1B;AAgCD;;;;;;;;;;;GAWG;AACH,wBAAuB,oBAAoB,CAAC,CAAC,GAAG,OAAO,EACrD,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,CAAC,CAAC;IAAC,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAUzD;AAED;;;;;;;GAOG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAMrB;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAK1B"}
|