@agentforge/core 0.16.9 → 0.16.11
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/index.cjs +65 -15
- package/dist/index.d.cts +35 -29
- package/dist/index.d.ts +35 -29
- package/dist/index.js +65 -15
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -494,10 +494,50 @@ function validateTool(tool) {
|
|
|
494
494
|
}
|
|
495
495
|
|
|
496
496
|
// src/tools/builder.ts
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
497
|
+
function cloneRelations(relations) {
|
|
498
|
+
if (!relations) {
|
|
499
|
+
return void 0;
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
requires: relations.requires ? [...relations.requires] : void 0,
|
|
503
|
+
suggests: relations.suggests ? [...relations.suggests] : void 0,
|
|
504
|
+
conflicts: relations.conflicts ? [...relations.conflicts] : void 0,
|
|
505
|
+
follows: relations.follows ? [...relations.follows] : void 0,
|
|
506
|
+
precedes: relations.precedes ? [...relations.precedes] : void 0
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
function cloneExampleValue(value, exampleIndex, field) {
|
|
510
|
+
try {
|
|
511
|
+
return structuredClone(value);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
514
|
+
throw new TypeError(
|
|
515
|
+
`Invalid tool example at index ${exampleIndex}: "${field}" must be a structured-cloneable value. Received a non-cloneable value while building the tool metadata. Original error: ${reason}`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
function cloneExamples(examples) {
|
|
520
|
+
return examples?.map((example, index) => ({
|
|
521
|
+
...example,
|
|
522
|
+
input: cloneExampleValue(example.input, index, "input"),
|
|
523
|
+
output: example.output === void 0 ? void 0 : cloneExampleValue(example.output, index, "output")
|
|
524
|
+
}));
|
|
525
|
+
}
|
|
526
|
+
function cloneMetadata(metadata) {
|
|
527
|
+
return {
|
|
528
|
+
...metadata,
|
|
529
|
+
tags: metadata.tags ? [...metadata.tags] : void 0,
|
|
530
|
+
examples: cloneExamples(metadata.examples),
|
|
531
|
+
limitations: metadata.limitations ? [...metadata.limitations] : void 0,
|
|
532
|
+
relations: cloneRelations(metadata.relations)
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
var ToolBuilder = class _ToolBuilder {
|
|
536
|
+
constructor(metadata = {}, _schema, _invoke) {
|
|
537
|
+
this.metadata = metadata;
|
|
538
|
+
this._schema = _schema;
|
|
539
|
+
this._invoke = _invoke;
|
|
540
|
+
}
|
|
501
541
|
/**
|
|
502
542
|
* Set the tool name (required)
|
|
503
543
|
*
|
|
@@ -697,14 +737,13 @@ var ToolBuilder = class {
|
|
|
697
737
|
}
|
|
698
738
|
/**
|
|
699
739
|
* Set the input schema (required)
|
|
700
|
-
*
|
|
740
|
+
*
|
|
701
741
|
* All fields MUST have .describe() for LLM understanding!
|
|
702
|
-
*
|
|
742
|
+
*
|
|
703
743
|
* @param schema - Zod schema for input validation
|
|
704
744
|
*/
|
|
705
745
|
schema(schema) {
|
|
706
|
-
this.
|
|
707
|
-
return this;
|
|
746
|
+
return new _ToolBuilder(cloneMetadata(this.metadata), schema, this._invoke);
|
|
708
747
|
}
|
|
709
748
|
/**
|
|
710
749
|
* Set the implementation function (required)
|
|
@@ -712,8 +751,10 @@ var ToolBuilder = class {
|
|
|
712
751
|
* @param invoke - Async function that implements the tool
|
|
713
752
|
*/
|
|
714
753
|
implement(invoke) {
|
|
715
|
-
|
|
716
|
-
|
|
754
|
+
const wrappedInvoke = async function(input) {
|
|
755
|
+
return invoke.call(this, input);
|
|
756
|
+
};
|
|
757
|
+
return new _ToolBuilder(cloneMetadata(this.metadata), this._schema, wrappedInvoke);
|
|
717
758
|
}
|
|
718
759
|
/**
|
|
719
760
|
* Set the implementation function with automatic error handling
|
|
@@ -739,9 +780,9 @@ var ToolBuilder = class {
|
|
|
739
780
|
* ```
|
|
740
781
|
*/
|
|
741
782
|
implementSafe(invoke) {
|
|
742
|
-
const safeInvoke = async (input)
|
|
783
|
+
const safeInvoke = async function(input) {
|
|
743
784
|
try {
|
|
744
|
-
const data = await invoke(input);
|
|
785
|
+
const data = await invoke.call(this, input);
|
|
745
786
|
return { success: true, data };
|
|
746
787
|
} catch (error) {
|
|
747
788
|
return {
|
|
@@ -750,8 +791,14 @@ var ToolBuilder = class {
|
|
|
750
791
|
};
|
|
751
792
|
}
|
|
752
793
|
};
|
|
753
|
-
|
|
754
|
-
|
|
794
|
+
const wrappedInvoke = async function(input) {
|
|
795
|
+
return safeInvoke.call(this, input);
|
|
796
|
+
};
|
|
797
|
+
return new _ToolBuilder(
|
|
798
|
+
cloneMetadata(this.metadata),
|
|
799
|
+
this._schema,
|
|
800
|
+
wrappedInvoke
|
|
801
|
+
);
|
|
755
802
|
}
|
|
756
803
|
/**
|
|
757
804
|
* Build the tool with validation
|
|
@@ -780,10 +827,13 @@ var ToolBuilder = class {
|
|
|
780
827
|
if (!this._invoke) {
|
|
781
828
|
throw new Error("Tool implementation is required. Use .implement() to set it.");
|
|
782
829
|
}
|
|
830
|
+
const invoke = this._invoke;
|
|
783
831
|
return createTool(
|
|
784
832
|
this.metadata,
|
|
785
833
|
this._schema,
|
|
786
|
-
|
|
834
|
+
async function(input) {
|
|
835
|
+
return invoke.call(this, input);
|
|
836
|
+
}
|
|
787
837
|
);
|
|
788
838
|
}
|
|
789
839
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -941,6 +941,7 @@ declare function validateTool(tool: Tool): {
|
|
|
941
941
|
* ```
|
|
942
942
|
*/
|
|
943
943
|
|
|
944
|
+
type ToolInvoke<TOutput> = (this: unknown, input: unknown) => Promise<TOutput>;
|
|
944
945
|
/**
|
|
945
946
|
* Builder for creating tools with a fluent API
|
|
946
947
|
*
|
|
@@ -951,6 +952,7 @@ declare class ToolBuilder<TInput = unknown, TOutput = unknown> {
|
|
|
951
952
|
private metadata;
|
|
952
953
|
private _schema?;
|
|
953
954
|
private _invoke?;
|
|
955
|
+
constructor(metadata?: Partial<ToolMetadata>, _schema?: z.ZodSchema<TInput> | undefined, _invoke?: ToolInvoke<TOutput> | undefined);
|
|
954
956
|
/**
|
|
955
957
|
* Set the tool name (required)
|
|
956
958
|
*
|
|
@@ -4180,6 +4182,7 @@ declare function createErrorReporter(options: ErrorReporterOptions): ErrorReport
|
|
|
4180
4182
|
* Types for LangGraph interrupt handling
|
|
4181
4183
|
* @module langgraph/interrupts/types
|
|
4182
4184
|
*/
|
|
4185
|
+
|
|
4183
4186
|
/**
|
|
4184
4187
|
* Priority level for human requests
|
|
4185
4188
|
*/
|
|
@@ -4203,7 +4206,7 @@ interface HumanRequest {
|
|
|
4203
4206
|
/**
|
|
4204
4207
|
* Optional context
|
|
4205
4208
|
*/
|
|
4206
|
-
context?:
|
|
4209
|
+
context?: JsonObject;
|
|
4207
4210
|
/**
|
|
4208
4211
|
* Priority level
|
|
4209
4212
|
*/
|
|
@@ -4241,14 +4244,22 @@ interface HumanRequest {
|
|
|
4241
4244
|
* Interrupt type - identifies what kind of interrupt occurred
|
|
4242
4245
|
*/
|
|
4243
4246
|
type InterruptType = 'human_request' | 'approval_required' | 'custom';
|
|
4247
|
+
/**
|
|
4248
|
+
* Shared interrupt metadata contract.
|
|
4249
|
+
*/
|
|
4250
|
+
type InterruptMetadata = JsonObject;
|
|
4251
|
+
/**
|
|
4252
|
+
* JSON-safe payload allowed in generic interrupt and resume flows.
|
|
4253
|
+
*/
|
|
4254
|
+
type InterruptPayload = JsonValue;
|
|
4244
4255
|
/**
|
|
4245
4256
|
* Interrupt data stored in the checkpoint
|
|
4246
4257
|
*/
|
|
4247
|
-
interface InterruptData {
|
|
4258
|
+
interface InterruptData<TType extends InterruptType = InterruptType, TData = unknown, TMetadata extends InterruptMetadata = InterruptMetadata> {
|
|
4248
4259
|
/**
|
|
4249
4260
|
* Type of interrupt
|
|
4250
4261
|
*/
|
|
4251
|
-
type:
|
|
4262
|
+
type: TType;
|
|
4252
4263
|
/**
|
|
4253
4264
|
* Unique ID for this interrupt
|
|
4254
4265
|
*/
|
|
@@ -4260,37 +4271,32 @@ interface InterruptData {
|
|
|
4260
4271
|
/**
|
|
4261
4272
|
* The data associated with this interrupt
|
|
4262
4273
|
*/
|
|
4263
|
-
data:
|
|
4274
|
+
data: TData;
|
|
4264
4275
|
/**
|
|
4265
4276
|
* Optional metadata
|
|
4266
4277
|
*/
|
|
4267
|
-
metadata?:
|
|
4278
|
+
metadata?: TMetadata;
|
|
4268
4279
|
}
|
|
4269
4280
|
/**
|
|
4270
|
-
*
|
|
4281
|
+
* Approval request payload.
|
|
4271
4282
|
*/
|
|
4272
|
-
interface
|
|
4273
|
-
|
|
4274
|
-
|
|
4283
|
+
interface ApprovalRequiredData {
|
|
4284
|
+
action: string;
|
|
4285
|
+
description: string;
|
|
4286
|
+
context?: JsonObject;
|
|
4275
4287
|
}
|
|
4288
|
+
/**
|
|
4289
|
+
* Human request interrupt data
|
|
4290
|
+
*/
|
|
4291
|
+
type HumanRequestInterrupt = InterruptData<'human_request', HumanRequest>;
|
|
4276
4292
|
/**
|
|
4277
4293
|
* Approval required interrupt data
|
|
4278
4294
|
*/
|
|
4279
|
-
|
|
4280
|
-
type: 'approval_required';
|
|
4281
|
-
data: {
|
|
4282
|
-
action: string;
|
|
4283
|
-
description: string;
|
|
4284
|
-
context?: Record<string, any>;
|
|
4285
|
-
};
|
|
4286
|
-
}
|
|
4295
|
+
type ApprovalRequiredInterrupt = InterruptData<'approval_required', ApprovalRequiredData>;
|
|
4287
4296
|
/**
|
|
4288
4297
|
* Custom interrupt data
|
|
4289
4298
|
*/
|
|
4290
|
-
|
|
4291
|
-
type: 'custom';
|
|
4292
|
-
data: any;
|
|
4293
|
-
}
|
|
4299
|
+
type CustomInterrupt<TData extends InterruptPayload = InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata> = InterruptData<'custom', TData, TMetadata>;
|
|
4294
4300
|
/**
|
|
4295
4301
|
* Union type of all interrupt types
|
|
4296
4302
|
*/
|
|
@@ -4298,15 +4304,15 @@ type AnyInterrupt = HumanRequestInterrupt | ApprovalRequiredInterrupt | CustomIn
|
|
|
4298
4304
|
/**
|
|
4299
4305
|
* Resume command for continuing after an interrupt
|
|
4300
4306
|
*/
|
|
4301
|
-
interface ResumeCommand {
|
|
4307
|
+
interface ResumeCommand<TResume extends InterruptPayload = InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata> {
|
|
4302
4308
|
/**
|
|
4303
4309
|
* The response to the interrupt
|
|
4304
4310
|
*/
|
|
4305
|
-
resume:
|
|
4311
|
+
resume: TResume;
|
|
4306
4312
|
/**
|
|
4307
4313
|
* Optional metadata about the response
|
|
4308
4314
|
*/
|
|
4309
|
-
metadata?:
|
|
4315
|
+
metadata?: TMetadata;
|
|
4310
4316
|
}
|
|
4311
4317
|
/**
|
|
4312
4318
|
* Thread status
|
|
@@ -4335,7 +4341,7 @@ interface ThreadInfo {
|
|
|
4335
4341
|
/**
|
|
4336
4342
|
* Optional metadata
|
|
4337
4343
|
*/
|
|
4338
|
-
metadata?:
|
|
4344
|
+
metadata?: InterruptMetadata;
|
|
4339
4345
|
}
|
|
4340
4346
|
/**
|
|
4341
4347
|
* Options for checking interrupt status
|
|
@@ -4365,11 +4371,11 @@ interface ResumeOptions {
|
|
|
4365
4371
|
/**
|
|
4366
4372
|
* The response/value to resume with
|
|
4367
4373
|
*/
|
|
4368
|
-
value:
|
|
4374
|
+
value: InterruptPayload;
|
|
4369
4375
|
/**
|
|
4370
4376
|
* Optional metadata
|
|
4371
4377
|
*/
|
|
4372
|
-
metadata?:
|
|
4378
|
+
metadata?: InterruptMetadata;
|
|
4373
4379
|
}
|
|
4374
4380
|
|
|
4375
4381
|
/**
|
|
@@ -4413,7 +4419,7 @@ declare function createHumanRequestInterrupt(request: HumanRequest): HumanReques
|
|
|
4413
4419
|
* );
|
|
4414
4420
|
* ```
|
|
4415
4421
|
*/
|
|
4416
|
-
declare function createApprovalRequiredInterrupt(action: string, description: string, context?:
|
|
4422
|
+
declare function createApprovalRequiredInterrupt(action: string, description: string, context?: JsonObject): ApprovalRequiredInterrupt;
|
|
4417
4423
|
/**
|
|
4418
4424
|
* Create a custom interrupt
|
|
4419
4425
|
*
|
|
@@ -4430,7 +4436,7 @@ declare function createApprovalRequiredInterrupt(action: string, description: st
|
|
|
4430
4436
|
* );
|
|
4431
4437
|
* ```
|
|
4432
4438
|
*/
|
|
4433
|
-
declare function createCustomInterrupt(id: string, data:
|
|
4439
|
+
declare function createCustomInterrupt<TData extends InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata>(id: string, data: TData, metadata?: TMetadata): CustomInterrupt<TData, TMetadata>;
|
|
4434
4440
|
/**
|
|
4435
4441
|
* Check if an interrupt is a human request
|
|
4436
4442
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -941,6 +941,7 @@ declare function validateTool(tool: Tool): {
|
|
|
941
941
|
* ```
|
|
942
942
|
*/
|
|
943
943
|
|
|
944
|
+
type ToolInvoke<TOutput> = (this: unknown, input: unknown) => Promise<TOutput>;
|
|
944
945
|
/**
|
|
945
946
|
* Builder for creating tools with a fluent API
|
|
946
947
|
*
|
|
@@ -951,6 +952,7 @@ declare class ToolBuilder<TInput = unknown, TOutput = unknown> {
|
|
|
951
952
|
private metadata;
|
|
952
953
|
private _schema?;
|
|
953
954
|
private _invoke?;
|
|
955
|
+
constructor(metadata?: Partial<ToolMetadata>, _schema?: z.ZodSchema<TInput> | undefined, _invoke?: ToolInvoke<TOutput> | undefined);
|
|
954
956
|
/**
|
|
955
957
|
* Set the tool name (required)
|
|
956
958
|
*
|
|
@@ -4180,6 +4182,7 @@ declare function createErrorReporter(options: ErrorReporterOptions): ErrorReport
|
|
|
4180
4182
|
* Types for LangGraph interrupt handling
|
|
4181
4183
|
* @module langgraph/interrupts/types
|
|
4182
4184
|
*/
|
|
4185
|
+
|
|
4183
4186
|
/**
|
|
4184
4187
|
* Priority level for human requests
|
|
4185
4188
|
*/
|
|
@@ -4203,7 +4206,7 @@ interface HumanRequest {
|
|
|
4203
4206
|
/**
|
|
4204
4207
|
* Optional context
|
|
4205
4208
|
*/
|
|
4206
|
-
context?:
|
|
4209
|
+
context?: JsonObject;
|
|
4207
4210
|
/**
|
|
4208
4211
|
* Priority level
|
|
4209
4212
|
*/
|
|
@@ -4241,14 +4244,22 @@ interface HumanRequest {
|
|
|
4241
4244
|
* Interrupt type - identifies what kind of interrupt occurred
|
|
4242
4245
|
*/
|
|
4243
4246
|
type InterruptType = 'human_request' | 'approval_required' | 'custom';
|
|
4247
|
+
/**
|
|
4248
|
+
* Shared interrupt metadata contract.
|
|
4249
|
+
*/
|
|
4250
|
+
type InterruptMetadata = JsonObject;
|
|
4251
|
+
/**
|
|
4252
|
+
* JSON-safe payload allowed in generic interrupt and resume flows.
|
|
4253
|
+
*/
|
|
4254
|
+
type InterruptPayload = JsonValue;
|
|
4244
4255
|
/**
|
|
4245
4256
|
* Interrupt data stored in the checkpoint
|
|
4246
4257
|
*/
|
|
4247
|
-
interface InterruptData {
|
|
4258
|
+
interface InterruptData<TType extends InterruptType = InterruptType, TData = unknown, TMetadata extends InterruptMetadata = InterruptMetadata> {
|
|
4248
4259
|
/**
|
|
4249
4260
|
* Type of interrupt
|
|
4250
4261
|
*/
|
|
4251
|
-
type:
|
|
4262
|
+
type: TType;
|
|
4252
4263
|
/**
|
|
4253
4264
|
* Unique ID for this interrupt
|
|
4254
4265
|
*/
|
|
@@ -4260,37 +4271,32 @@ interface InterruptData {
|
|
|
4260
4271
|
/**
|
|
4261
4272
|
* The data associated with this interrupt
|
|
4262
4273
|
*/
|
|
4263
|
-
data:
|
|
4274
|
+
data: TData;
|
|
4264
4275
|
/**
|
|
4265
4276
|
* Optional metadata
|
|
4266
4277
|
*/
|
|
4267
|
-
metadata?:
|
|
4278
|
+
metadata?: TMetadata;
|
|
4268
4279
|
}
|
|
4269
4280
|
/**
|
|
4270
|
-
*
|
|
4281
|
+
* Approval request payload.
|
|
4271
4282
|
*/
|
|
4272
|
-
interface
|
|
4273
|
-
|
|
4274
|
-
|
|
4283
|
+
interface ApprovalRequiredData {
|
|
4284
|
+
action: string;
|
|
4285
|
+
description: string;
|
|
4286
|
+
context?: JsonObject;
|
|
4275
4287
|
}
|
|
4288
|
+
/**
|
|
4289
|
+
* Human request interrupt data
|
|
4290
|
+
*/
|
|
4291
|
+
type HumanRequestInterrupt = InterruptData<'human_request', HumanRequest>;
|
|
4276
4292
|
/**
|
|
4277
4293
|
* Approval required interrupt data
|
|
4278
4294
|
*/
|
|
4279
|
-
|
|
4280
|
-
type: 'approval_required';
|
|
4281
|
-
data: {
|
|
4282
|
-
action: string;
|
|
4283
|
-
description: string;
|
|
4284
|
-
context?: Record<string, any>;
|
|
4285
|
-
};
|
|
4286
|
-
}
|
|
4295
|
+
type ApprovalRequiredInterrupt = InterruptData<'approval_required', ApprovalRequiredData>;
|
|
4287
4296
|
/**
|
|
4288
4297
|
* Custom interrupt data
|
|
4289
4298
|
*/
|
|
4290
|
-
|
|
4291
|
-
type: 'custom';
|
|
4292
|
-
data: any;
|
|
4293
|
-
}
|
|
4299
|
+
type CustomInterrupt<TData extends InterruptPayload = InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata> = InterruptData<'custom', TData, TMetadata>;
|
|
4294
4300
|
/**
|
|
4295
4301
|
* Union type of all interrupt types
|
|
4296
4302
|
*/
|
|
@@ -4298,15 +4304,15 @@ type AnyInterrupt = HumanRequestInterrupt | ApprovalRequiredInterrupt | CustomIn
|
|
|
4298
4304
|
/**
|
|
4299
4305
|
* Resume command for continuing after an interrupt
|
|
4300
4306
|
*/
|
|
4301
|
-
interface ResumeCommand {
|
|
4307
|
+
interface ResumeCommand<TResume extends InterruptPayload = InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata> {
|
|
4302
4308
|
/**
|
|
4303
4309
|
* The response to the interrupt
|
|
4304
4310
|
*/
|
|
4305
|
-
resume:
|
|
4311
|
+
resume: TResume;
|
|
4306
4312
|
/**
|
|
4307
4313
|
* Optional metadata about the response
|
|
4308
4314
|
*/
|
|
4309
|
-
metadata?:
|
|
4315
|
+
metadata?: TMetadata;
|
|
4310
4316
|
}
|
|
4311
4317
|
/**
|
|
4312
4318
|
* Thread status
|
|
@@ -4335,7 +4341,7 @@ interface ThreadInfo {
|
|
|
4335
4341
|
/**
|
|
4336
4342
|
* Optional metadata
|
|
4337
4343
|
*/
|
|
4338
|
-
metadata?:
|
|
4344
|
+
metadata?: InterruptMetadata;
|
|
4339
4345
|
}
|
|
4340
4346
|
/**
|
|
4341
4347
|
* Options for checking interrupt status
|
|
@@ -4365,11 +4371,11 @@ interface ResumeOptions {
|
|
|
4365
4371
|
/**
|
|
4366
4372
|
* The response/value to resume with
|
|
4367
4373
|
*/
|
|
4368
|
-
value:
|
|
4374
|
+
value: InterruptPayload;
|
|
4369
4375
|
/**
|
|
4370
4376
|
* Optional metadata
|
|
4371
4377
|
*/
|
|
4372
|
-
metadata?:
|
|
4378
|
+
metadata?: InterruptMetadata;
|
|
4373
4379
|
}
|
|
4374
4380
|
|
|
4375
4381
|
/**
|
|
@@ -4413,7 +4419,7 @@ declare function createHumanRequestInterrupt(request: HumanRequest): HumanReques
|
|
|
4413
4419
|
* );
|
|
4414
4420
|
* ```
|
|
4415
4421
|
*/
|
|
4416
|
-
declare function createApprovalRequiredInterrupt(action: string, description: string, context?:
|
|
4422
|
+
declare function createApprovalRequiredInterrupt(action: string, description: string, context?: JsonObject): ApprovalRequiredInterrupt;
|
|
4417
4423
|
/**
|
|
4418
4424
|
* Create a custom interrupt
|
|
4419
4425
|
*
|
|
@@ -4430,7 +4436,7 @@ declare function createApprovalRequiredInterrupt(action: string, description: st
|
|
|
4430
4436
|
* );
|
|
4431
4437
|
* ```
|
|
4432
4438
|
*/
|
|
4433
|
-
declare function createCustomInterrupt(id: string, data:
|
|
4439
|
+
declare function createCustomInterrupt<TData extends InterruptPayload, TMetadata extends InterruptMetadata = InterruptMetadata>(id: string, data: TData, metadata?: TMetadata): CustomInterrupt<TData, TMetadata>;
|
|
4434
4440
|
/**
|
|
4435
4441
|
* Check if an interrupt is a human request
|
|
4436
4442
|
*
|
package/dist/index.js
CHANGED
|
@@ -319,10 +319,50 @@ function validateTool(tool) {
|
|
|
319
319
|
}
|
|
320
320
|
|
|
321
321
|
// src/tools/builder.ts
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
322
|
+
function cloneRelations(relations) {
|
|
323
|
+
if (!relations) {
|
|
324
|
+
return void 0;
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
requires: relations.requires ? [...relations.requires] : void 0,
|
|
328
|
+
suggests: relations.suggests ? [...relations.suggests] : void 0,
|
|
329
|
+
conflicts: relations.conflicts ? [...relations.conflicts] : void 0,
|
|
330
|
+
follows: relations.follows ? [...relations.follows] : void 0,
|
|
331
|
+
precedes: relations.precedes ? [...relations.precedes] : void 0
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function cloneExampleValue(value, exampleIndex, field) {
|
|
335
|
+
try {
|
|
336
|
+
return structuredClone(value);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
339
|
+
throw new TypeError(
|
|
340
|
+
`Invalid tool example at index ${exampleIndex}: "${field}" must be a structured-cloneable value. Received a non-cloneable value while building the tool metadata. Original error: ${reason}`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function cloneExamples(examples) {
|
|
345
|
+
return examples?.map((example, index) => ({
|
|
346
|
+
...example,
|
|
347
|
+
input: cloneExampleValue(example.input, index, "input"),
|
|
348
|
+
output: example.output === void 0 ? void 0 : cloneExampleValue(example.output, index, "output")
|
|
349
|
+
}));
|
|
350
|
+
}
|
|
351
|
+
function cloneMetadata(metadata) {
|
|
352
|
+
return {
|
|
353
|
+
...metadata,
|
|
354
|
+
tags: metadata.tags ? [...metadata.tags] : void 0,
|
|
355
|
+
examples: cloneExamples(metadata.examples),
|
|
356
|
+
limitations: metadata.limitations ? [...metadata.limitations] : void 0,
|
|
357
|
+
relations: cloneRelations(metadata.relations)
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
var ToolBuilder = class _ToolBuilder {
|
|
361
|
+
constructor(metadata = {}, _schema, _invoke) {
|
|
362
|
+
this.metadata = metadata;
|
|
363
|
+
this._schema = _schema;
|
|
364
|
+
this._invoke = _invoke;
|
|
365
|
+
}
|
|
326
366
|
/**
|
|
327
367
|
* Set the tool name (required)
|
|
328
368
|
*
|
|
@@ -522,14 +562,13 @@ var ToolBuilder = class {
|
|
|
522
562
|
}
|
|
523
563
|
/**
|
|
524
564
|
* Set the input schema (required)
|
|
525
|
-
*
|
|
565
|
+
*
|
|
526
566
|
* All fields MUST have .describe() for LLM understanding!
|
|
527
|
-
*
|
|
567
|
+
*
|
|
528
568
|
* @param schema - Zod schema for input validation
|
|
529
569
|
*/
|
|
530
570
|
schema(schema) {
|
|
531
|
-
this.
|
|
532
|
-
return this;
|
|
571
|
+
return new _ToolBuilder(cloneMetadata(this.metadata), schema, this._invoke);
|
|
533
572
|
}
|
|
534
573
|
/**
|
|
535
574
|
* Set the implementation function (required)
|
|
@@ -537,8 +576,10 @@ var ToolBuilder = class {
|
|
|
537
576
|
* @param invoke - Async function that implements the tool
|
|
538
577
|
*/
|
|
539
578
|
implement(invoke) {
|
|
540
|
-
|
|
541
|
-
|
|
579
|
+
const wrappedInvoke = async function(input) {
|
|
580
|
+
return invoke.call(this, input);
|
|
581
|
+
};
|
|
582
|
+
return new _ToolBuilder(cloneMetadata(this.metadata), this._schema, wrappedInvoke);
|
|
542
583
|
}
|
|
543
584
|
/**
|
|
544
585
|
* Set the implementation function with automatic error handling
|
|
@@ -564,9 +605,9 @@ var ToolBuilder = class {
|
|
|
564
605
|
* ```
|
|
565
606
|
*/
|
|
566
607
|
implementSafe(invoke) {
|
|
567
|
-
const safeInvoke = async (input)
|
|
608
|
+
const safeInvoke = async function(input) {
|
|
568
609
|
try {
|
|
569
|
-
const data = await invoke(input);
|
|
610
|
+
const data = await invoke.call(this, input);
|
|
570
611
|
return { success: true, data };
|
|
571
612
|
} catch (error) {
|
|
572
613
|
return {
|
|
@@ -575,8 +616,14 @@ var ToolBuilder = class {
|
|
|
575
616
|
};
|
|
576
617
|
}
|
|
577
618
|
};
|
|
578
|
-
|
|
579
|
-
|
|
619
|
+
const wrappedInvoke = async function(input) {
|
|
620
|
+
return safeInvoke.call(this, input);
|
|
621
|
+
};
|
|
622
|
+
return new _ToolBuilder(
|
|
623
|
+
cloneMetadata(this.metadata),
|
|
624
|
+
this._schema,
|
|
625
|
+
wrappedInvoke
|
|
626
|
+
);
|
|
580
627
|
}
|
|
581
628
|
/**
|
|
582
629
|
* Build the tool with validation
|
|
@@ -605,10 +652,13 @@ var ToolBuilder = class {
|
|
|
605
652
|
if (!this._invoke) {
|
|
606
653
|
throw new Error("Tool implementation is required. Use .implement() to set it.");
|
|
607
654
|
}
|
|
655
|
+
const invoke = this._invoke;
|
|
608
656
|
return createTool(
|
|
609
657
|
this.metadata,
|
|
610
658
|
this._schema,
|
|
611
|
-
|
|
659
|
+
async function(input) {
|
|
660
|
+
return invoke.call(this, input);
|
|
661
|
+
}
|
|
612
662
|
);
|
|
613
663
|
}
|
|
614
664
|
};
|
package/package.json
CHANGED