@node-llm/core 1.14.1 → 1.15.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/dist/agent/Agent.d.ts +13 -9
- package/dist/agent/Agent.d.ts.map +1 -1
- package/dist/agent/Agent.js +4 -1
- package/dist/aliases.d.ts +123 -215
- package/dist/aliases.d.ts.map +1 -1
- package/dist/aliases.js +177 -269
- package/dist/chat/Chat.d.ts +6 -0
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +189 -163
- package/dist/chat/ChatOptions.d.ts +4 -2
- package/dist/chat/ChatOptions.d.ts.map +1 -1
- package/dist/chat/ChatStream.d.ts.map +1 -1
- package/dist/chat/ChatStream.js +7 -2
- package/dist/chat/Tool.d.ts +6 -0
- package/dist/chat/Tool.d.ts.map +1 -1
- package/dist/chat/Tool.js +13 -1
- package/dist/executor/Executor.d.ts.map +1 -1
- package/dist/executor/Executor.js +3 -0
- package/dist/middlewares/CostGuardMiddleware.d.ts +6 -2
- package/dist/middlewares/CostGuardMiddleware.d.ts.map +1 -1
- package/dist/middlewares/CostGuardMiddleware.js +6 -0
- package/dist/middlewares/SchemaSelfCorrection.d.ts +26 -0
- package/dist/middlewares/SchemaSelfCorrection.d.ts.map +1 -0
- package/dist/middlewares/SchemaSelfCorrection.js +70 -0
- package/dist/middlewares/UsageLoggerMiddleware.d.ts +6 -2
- package/dist/middlewares/UsageLoggerMiddleware.d.ts.map +1 -1
- package/dist/middlewares/UsageLoggerMiddleware.js +6 -0
- package/dist/middlewares/index.d.ts +1 -0
- package/dist/middlewares/index.d.ts.map +1 -1
- package/dist/middlewares/index.js +1 -0
- package/dist/models/models.json +1057 -2007
- package/dist/providers/Provider.d.ts +3 -0
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/ollama/OllamaProvider.d.ts.map +1 -1
- package/dist/providers/ollama/OllamaProvider.js +1 -0
- package/dist/providers/openai/Capabilities.d.ts +1 -0
- package/dist/providers/openai/Capabilities.d.ts.map +1 -1
- package/dist/providers/openai/Capabilities.js +3 -0
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +38 -5
- package/dist/providers/openai/OpenAIProvider.d.ts +2 -1
- package/dist/providers/openai/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/openai/OpenAIProvider.js +2 -1
- package/dist/providers/openai/Streaming.d.ts.map +1 -1
- package/dist/providers/openai/Streaming.js +33 -2
- package/dist/providers/openrouter/OpenRouterProvider.d.ts +1 -0
- package/dist/providers/openrouter/OpenRouterProvider.d.ts.map +1 -1
- package/dist/providers/openrouter/OpenRouterProvider.js +1 -0
- package/dist/schema/strict.d.ts +2 -0
- package/dist/schema/strict.d.ts.map +1 -0
- package/dist/schema/strict.js +39 -0
- package/dist/types/Middleware.d.ts +16 -2
- package/dist/types/Middleware.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Middleware, MiddlewareContext, RequestDirective } from "../types/Middleware.js";
|
|
2
|
+
import { ChatResponseString } from "../chat/ChatResponse.js";
|
|
3
|
+
import { ToolCall } from "../chat/Tool.js";
|
|
4
|
+
export interface SchemaSelfCorrectionOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of correction attempts.
|
|
7
|
+
* Default is 2.
|
|
8
|
+
*/
|
|
9
|
+
maxRetries?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Middleware that automatically detects schema validation errors in structured outputs
|
|
13
|
+
* and re-prompts the model with specific feedback.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SchemaSelfCorrectionMiddleware implements Middleware {
|
|
16
|
+
readonly name = "SchemaSelfCorrection";
|
|
17
|
+
private readonly maxRetries;
|
|
18
|
+
constructor(options?: SchemaSelfCorrectionOptions);
|
|
19
|
+
onResponse(ctx: MiddlewareContext, result: ChatResponseString): Promise<RequestDirective>;
|
|
20
|
+
onToolCallError(ctx: MiddlewareContext, toolCall: ToolCall, error: unknown): Promise<"CONTINUE" | "STOP" | "RETRY">;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Factory function for creating the self-correction middleware.
|
|
24
|
+
*/
|
|
25
|
+
export declare function SchemaSelfCorrection(options?: SchemaSelfCorrectionOptions): SchemaSelfCorrectionMiddleware;
|
|
26
|
+
//# sourceMappingURL=SchemaSelfCorrection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaSelfCorrection.d.ts","sourceRoot":"","sources":["../../src/middlewares/SchemaSelfCorrection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACzF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,qBAAa,8BAA+B,YAAW,UAAU;IAC/D,SAAgB,IAAI,0BAA0B;IAC9C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,OAAO,GAAE,2BAAgC;IAI/C,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA+CzF,eAAe,CACnB,GAAG,EAAE,iBAAiB,EACtB,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC;CAwB1C;AACD;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,GAAE,2BAAgC,GACxC,8BAA8B,CAEhC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { logger } from "../utils/logger.js";
|
|
2
|
+
/**
|
|
3
|
+
* Middleware that automatically detects schema validation errors in structured outputs
|
|
4
|
+
* and re-prompts the model with specific feedback.
|
|
5
|
+
*/
|
|
6
|
+
export class SchemaSelfCorrectionMiddleware {
|
|
7
|
+
name = "SchemaSelfCorrection";
|
|
8
|
+
maxRetries;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.maxRetries = options.maxRetries ?? 2;
|
|
11
|
+
}
|
|
12
|
+
async onResponse(ctx, result) {
|
|
13
|
+
// Only apply if a schema was provided
|
|
14
|
+
if (!ctx.options?.schema) {
|
|
15
|
+
return "CONTINUE";
|
|
16
|
+
}
|
|
17
|
+
// Check if valid
|
|
18
|
+
if (result.isValid) {
|
|
19
|
+
return "CONTINUE";
|
|
20
|
+
}
|
|
21
|
+
// It's invalid. Get the error.
|
|
22
|
+
const error = result.validationError;
|
|
23
|
+
if (!error) {
|
|
24
|
+
return "CONTINUE";
|
|
25
|
+
}
|
|
26
|
+
// Keep track of correction rounds in context state
|
|
27
|
+
const currentRounds = ctx.state.correctionRounds || 0;
|
|
28
|
+
if (currentRounds < this.maxRetries) {
|
|
29
|
+
ctx.state.correctionRounds = currentRounds + 1;
|
|
30
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
31
|
+
logger.warn(`[SchemaSelfCorrection] Validation failed for ${ctx.provider}/${ctx.model}. ` +
|
|
32
|
+
`Round ${currentRounds + 1}/${this.maxRetries}. Error: ${errorMessage}`);
|
|
33
|
+
// Return RETRY directive with feedback
|
|
34
|
+
return {
|
|
35
|
+
action: "RETRY",
|
|
36
|
+
message: `Your previous response did not match the required JSON schema.
|
|
37
|
+
Validation Error: ${errorMessage}
|
|
38
|
+
|
|
39
|
+
Please correct your output to strictly follow the schema and provided types.`
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// If we reached max retries, we just continue (or the user will get the validation error when calling .data)
|
|
43
|
+
logger.error(`[SchemaSelfCorrection] Max retries (${this.maxRetries}) reached for ${ctx.provider}/${ctx.model}.`);
|
|
44
|
+
return "CONTINUE";
|
|
45
|
+
}
|
|
46
|
+
async onToolCallError(ctx, toolCall, error) {
|
|
47
|
+
// Check if it's a validation error (ZodError)
|
|
48
|
+
if (error && typeof error === "object" && "name" in error && error.name === "ZodError") {
|
|
49
|
+
const currentRounds = ctx.state.correctionRounds || 0;
|
|
50
|
+
if (currentRounds < this.maxRetries) {
|
|
51
|
+
ctx.state.correctionRounds = currentRounds + 1;
|
|
52
|
+
logger.warn(`[SchemaSelfCorrection] Tool validation failed for '${toolCall.function.name}'. ` +
|
|
53
|
+
`Round ${currentRounds + 1}/${this.maxRetries}.`);
|
|
54
|
+
// We return CONTINUE here because Chat.ts already handles pushing the error to history
|
|
55
|
+
// BUT we could potentially change the error message to be more helpful!
|
|
56
|
+
// However, Chat.ts currently just takes the error.message.
|
|
57
|
+
// If we want to override the message, we'd need onToolCallError to return a descriptive RETRY or similar.
|
|
58
|
+
// Currently Chat.ts's onToolCallError only returns "CONTINUE" | "STOP" | "RETRY".
|
|
59
|
+
return "CONTINUE";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return "CONTINUE";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Factory function for creating the self-correction middleware.
|
|
67
|
+
*/
|
|
68
|
+
export function SchemaSelfCorrection(options = {}) {
|
|
69
|
+
return new SchemaSelfCorrectionMiddleware(options);
|
|
70
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Middleware, MiddlewareContext } from "../types/Middleware.js";
|
|
1
|
+
import { Middleware, MiddlewareContext, RequestDirective } from "../types/Middleware.js";
|
|
2
2
|
import { ChatResponseString } from "../chat/ChatResponse.js";
|
|
3
3
|
export interface UsageLoggerOptions {
|
|
4
4
|
/**
|
|
@@ -17,6 +17,10 @@ export declare class UsageLoggerMiddleware implements Middleware {
|
|
|
17
17
|
private options;
|
|
18
18
|
readonly name = "UsageLogger";
|
|
19
19
|
constructor(options?: UsageLoggerOptions);
|
|
20
|
-
onResponse(ctx: MiddlewareContext, result: ChatResponseString): Promise<
|
|
20
|
+
onResponse(ctx: MiddlewareContext, result: ChatResponseString): Promise<RequestDirective>;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Factory function for creating the usage logger middleware.
|
|
24
|
+
*/
|
|
25
|
+
export declare function UsageLogger(options?: UsageLoggerOptions): UsageLoggerMiddleware;
|
|
22
26
|
//# sourceMappingURL=UsageLoggerMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UsageLoggerMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/UsageLoggerMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"UsageLoggerMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/UsageLoggerMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACzF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAG7D,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAC9C;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,qBAAa,qBAAsB,YAAW,UAAU;IAG1C,OAAO,CAAC,OAAO;IAF3B,SAAgB,IAAI,iBAAiB;gBAEjB,OAAO,GAAE,kBAAuB;IAE9C,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAsBhG;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,GAAE,kBAAuB,GAAG,qBAAqB,CAEnF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middlewares/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middlewares/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC"}
|