beeai-framework 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -43
- package/dist/adapters/openai/backend/client.cjs +9 -2
- package/dist/adapters/openai/backend/client.cjs.map +1 -1
- package/dist/adapters/openai/backend/client.js +9 -2
- package/dist/adapters/openai/backend/client.js.map +1 -1
- package/dist/agents/base.cjs +1 -4
- package/dist/agents/base.cjs.map +1 -1
- package/dist/agents/base.js +1 -4
- package/dist/agents/base.js.map +1 -1
- package/dist/agents/react/prompts.d.cts +4 -4
- package/dist/agents/react/prompts.d.ts +4 -4
- package/dist/agents/react/runners/deep-think/prompts.d.cts +4 -4
- package/dist/agents/react/runners/deep-think/prompts.d.ts +4 -4
- package/dist/agents/react/runners/deep-think/runner.d.cts +4 -4
- package/dist/agents/react/runners/deep-think/runner.d.ts +4 -4
- package/dist/agents/react/runners/default/runner.d.cts +4 -4
- package/dist/agents/react/runners/default/runner.d.ts +4 -4
- package/dist/agents/react/runners/granite/prompts.d.cts +4 -4
- package/dist/agents/react/runners/granite/prompts.d.ts +4 -4
- package/dist/agents/react/runners/granite/runner.d.cts +6 -6
- package/dist/agents/react/runners/granite/runner.d.ts +6 -6
- package/dist/agents/toolCalling/prompts.cjs +3 -4
- package/dist/agents/toolCalling/prompts.cjs.map +1 -1
- package/dist/agents/toolCalling/prompts.js +3 -4
- package/dist/agents/toolCalling/prompts.js.map +1 -1
- package/dist/backend/chat.cjs +1 -3
- package/dist/backend/chat.cjs.map +1 -1
- package/dist/backend/chat.d.cts +2 -2
- package/dist/backend/chat.d.ts +2 -2
- package/dist/backend/chat.js +2 -4
- package/dist/backend/chat.js.map +1 -1
- package/dist/backend/embedding.d.cts +1 -1
- package/dist/backend/embedding.d.ts +1 -1
- package/dist/index.cjs +21 -0
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -0
- package/dist/tools/base.cjs +1 -3
- package/dist/tools/base.cjs.map +1 -1
- package/dist/tools/base.js +2 -4
- package/dist/tools/base.js.map +1 -1
- package/dist/tools/database/milvus.d.cts +4 -4
- package/dist/tools/database/milvus.d.ts +4 -4
- package/dist/version.cjs +1 -1
- package/dist/version.js +1 -1
- package/dist/workflows/agent.cjs +28 -20
- package/dist/workflows/agent.cjs.map +1 -1
- package/dist/workflows/agent.d.cts +221 -41
- package/dist/workflows/agent.d.ts +221 -41
- package/dist/workflows/agent.js +29 -21
- package/dist/workflows/agent.js.map +1 -1
- package/package.json +5 -12
- package/dist/instrumentation/config.cjs +0 -12
- package/dist/instrumentation/config.cjs.map +0 -1
- package/dist/instrumentation/config.d.cts +0 -19
- package/dist/instrumentation/config.d.ts +0 -19
- package/dist/instrumentation/config.js +0 -9
- package/dist/instrumentation/config.js.map +0 -1
- package/dist/instrumentation/create-telemetry-middleware.cjs +0 -239
- package/dist/instrumentation/create-telemetry-middleware.cjs.map +0 -1
- package/dist/instrumentation/create-telemetry-middleware.d.cts +0 -26
- package/dist/instrumentation/create-telemetry-middleware.d.ts +0 -26
- package/dist/instrumentation/create-telemetry-middleware.js +0 -237
- package/dist/instrumentation/create-telemetry-middleware.js.map +0 -1
- package/dist/instrumentation/helpers/create-span.cjs +0 -36
- package/dist/instrumentation/helpers/create-span.cjs.map +0 -1
- package/dist/instrumentation/helpers/create-span.d.cts +0 -34
- package/dist/instrumentation/helpers/create-span.d.ts +0 -34
- package/dist/instrumentation/helpers/create-span.js +0 -34
- package/dist/instrumentation/helpers/create-span.js.map +0 -1
- package/dist/instrumentation/helpers/get-error-safe.cjs +0 -20
- package/dist/instrumentation/helpers/get-error-safe.cjs.map +0 -1
- package/dist/instrumentation/helpers/get-error-safe.d.cts +0 -18
- package/dist/instrumentation/helpers/get-error-safe.d.ts +0 -18
- package/dist/instrumentation/helpers/get-error-safe.js +0 -18
- package/dist/instrumentation/helpers/get-error-safe.js.map +0 -1
- package/dist/instrumentation/helpers/get-serialized-object-safe.cjs +0 -27
- package/dist/instrumentation/helpers/get-serialized-object-safe.cjs.map +0 -1
- package/dist/instrumentation/helpers/get-serialized-object-safe.d.cts +0 -18
- package/dist/instrumentation/helpers/get-serialized-object-safe.d.ts +0 -18
- package/dist/instrumentation/helpers/get-serialized-object-safe.js +0 -25
- package/dist/instrumentation/helpers/get-serialized-object-safe.js.map +0 -1
- package/dist/instrumentation/helpers/id-name-manager.cjs +0 -34
- package/dist/instrumentation/helpers/id-name-manager.cjs.map +0 -1
- package/dist/instrumentation/helpers/id-name-manager.d.cts +0 -31
- package/dist/instrumentation/helpers/id-name-manager.d.ts +0 -31
- package/dist/instrumentation/helpers/id-name-manager.js +0 -32
- package/dist/instrumentation/helpers/id-name-manager.js.map +0 -1
- package/dist/instrumentation/helpers/trace-serializer.cjs +0 -30
- package/dist/instrumentation/helpers/trace-serializer.cjs.map +0 -1
- package/dist/instrumentation/helpers/trace-serializer.d.cts +0 -20
- package/dist/instrumentation/helpers/trace-serializer.d.ts +0 -20
- package/dist/instrumentation/helpers/trace-serializer.js +0 -28
- package/dist/instrumentation/helpers/trace-serializer.js.map +0 -1
- package/dist/instrumentation/helpers/utils.cjs +0 -14
- package/dist/instrumentation/helpers/utils.cjs.map +0 -1
- package/dist/instrumentation/helpers/utils.d.cts +0 -40
- package/dist/instrumentation/helpers/utils.d.ts +0 -40
- package/dist/instrumentation/helpers/utils.js +0 -12
- package/dist/instrumentation/helpers/utils.js.map +0 -1
- package/dist/instrumentation/logger.cjs +0 -12
- package/dist/instrumentation/logger.cjs.map +0 -1
- package/dist/instrumentation/logger.d.cts +0 -26
- package/dist/instrumentation/logger.d.ts +0 -26
- package/dist/instrumentation/logger.js +0 -10
- package/dist/instrumentation/logger.js.map +0 -1
- package/dist/instrumentation/tracer.cjs +0 -84
- package/dist/instrumentation/tracer.cjs.map +0 -1
- package/dist/instrumentation/tracer.d.cts +0 -36
- package/dist/instrumentation/tracer.d.ts +0 -36
- package/dist/instrumentation/tracer.js +0 -76
- package/dist/instrumentation/tracer.js.map +0 -1
- package/dist/instrumentation/types.cjs +0 -4
- package/dist/instrumentation/types.cjs.map +0 -1
- package/dist/instrumentation/types.d.cts +0 -39
- package/dist/instrumentation/types.d.ts +0 -39
- package/dist/instrumentation/types.js +0 -3
- package/dist/instrumentation/types.js.map +0 -1
|
@@ -51,12 +51,12 @@ declare class DefaultRunner extends BaseRunner {
|
|
|
51
51
|
}, z.ZodTypeAny, "passthrough">>, "many">;
|
|
52
52
|
createdAt: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
53
53
|
}, "strip", z.ZodTypeAny, {
|
|
54
|
-
instructions: string;
|
|
55
54
|
tools: z.objectOutputType<{
|
|
56
55
|
name: z.ZodString;
|
|
57
56
|
description: z.ZodString;
|
|
58
57
|
schema: z.ZodString;
|
|
59
58
|
}, z.ZodTypeAny, "passthrough">[];
|
|
59
|
+
instructions: string;
|
|
60
60
|
createdAt?: string | null | undefined;
|
|
61
61
|
}, {
|
|
62
62
|
tools: z.objectInputType<{
|
|
@@ -64,8 +64,8 @@ declare class DefaultRunner extends BaseRunner {
|
|
|
64
64
|
description: z.ZodString;
|
|
65
65
|
schema: z.ZodString;
|
|
66
66
|
}, z.ZodTypeAny, "passthrough">[];
|
|
67
|
-
createdAt?: string | null | undefined;
|
|
68
67
|
instructions?: string | undefined;
|
|
68
|
+
createdAt?: string | null | undefined;
|
|
69
69
|
}>>;
|
|
70
70
|
assistant: PromptTemplate<z.ZodObject<{
|
|
71
71
|
thought: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
@@ -74,14 +74,14 @@ declare class DefaultRunner extends BaseRunner {
|
|
|
74
74
|
toolOutput: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
75
75
|
finalAnswer: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
76
76
|
}, "strip", z.ZodTypeAny, {
|
|
77
|
-
toolName?: string[] | undefined;
|
|
78
77
|
thought?: string[] | undefined;
|
|
78
|
+
toolName?: string[] | undefined;
|
|
79
79
|
toolInput?: string[] | undefined;
|
|
80
80
|
toolOutput?: string[] | undefined;
|
|
81
81
|
finalAnswer?: string[] | undefined;
|
|
82
82
|
}, {
|
|
83
|
-
toolName?: string[] | undefined;
|
|
84
83
|
thought?: string[] | undefined;
|
|
84
|
+
toolName?: string[] | undefined;
|
|
85
85
|
toolInput?: string[] | undefined;
|
|
86
86
|
toolOutput?: string[] | undefined;
|
|
87
87
|
finalAnswer?: string[] | undefined;
|
|
@@ -28,14 +28,14 @@ declare const GraniteReActAgentAssistantPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
28
28
|
toolOutput: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
29
29
|
finalAnswer: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
30
30
|
}, "strip", zod.ZodTypeAny, {
|
|
31
|
-
toolName?: string[] | undefined;
|
|
32
31
|
thought?: string[] | undefined;
|
|
32
|
+
toolName?: string[] | undefined;
|
|
33
33
|
toolInput?: string[] | undefined;
|
|
34
34
|
toolOutput?: string[] | undefined;
|
|
35
35
|
finalAnswer?: string[] | undefined;
|
|
36
36
|
}, {
|
|
37
|
-
toolName?: string[] | undefined;
|
|
38
37
|
thought?: string[] | undefined;
|
|
38
|
+
toolName?: string[] | undefined;
|
|
39
39
|
toolInput?: string[] | undefined;
|
|
40
40
|
toolOutput?: string[] | undefined;
|
|
41
41
|
finalAnswer?: string[] | undefined;
|
|
@@ -57,12 +57,12 @@ declare const GraniteReActAgentSystemPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
57
57
|
}, zod.ZodTypeAny, "passthrough">>, "many">;
|
|
58
58
|
createdAt: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
59
59
|
}, "strip", zod.ZodTypeAny, {
|
|
60
|
-
instructions: string;
|
|
61
60
|
tools: zod.objectOutputType<{
|
|
62
61
|
name: zod.ZodString;
|
|
63
62
|
description: zod.ZodString;
|
|
64
63
|
schema: zod.ZodString;
|
|
65
64
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
65
|
+
instructions: string;
|
|
66
66
|
createdAt?: string | null | undefined;
|
|
67
67
|
}, {
|
|
68
68
|
tools: zod.objectInputType<{
|
|
@@ -70,8 +70,8 @@ declare const GraniteReActAgentSystemPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
70
70
|
description: zod.ZodString;
|
|
71
71
|
schema: zod.ZodString;
|
|
72
72
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
73
|
-
createdAt?: string | null | undefined;
|
|
74
73
|
instructions?: string | undefined;
|
|
74
|
+
createdAt?: string | null | undefined;
|
|
75
75
|
}>>;
|
|
76
76
|
declare const GraniteReActAgentSchemaErrorPrompt: PromptTemplate<zod.ZodObject<{}, "passthrough", zod.ZodTypeAny, zod.objectOutputType<{}, zod.ZodTypeAny, "passthrough">, zod.objectInputType<{}, zod.ZodTypeAny, "passthrough">>>;
|
|
77
77
|
declare const GraniteReActAgentUserPrompt: PromptTemplate<zod.ZodObject<{
|
|
@@ -28,14 +28,14 @@ declare const GraniteReActAgentAssistantPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
28
28
|
toolOutput: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
29
29
|
finalAnswer: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
30
30
|
}, "strip", zod.ZodTypeAny, {
|
|
31
|
-
toolName?: string[] | undefined;
|
|
32
31
|
thought?: string[] | undefined;
|
|
32
|
+
toolName?: string[] | undefined;
|
|
33
33
|
toolInput?: string[] | undefined;
|
|
34
34
|
toolOutput?: string[] | undefined;
|
|
35
35
|
finalAnswer?: string[] | undefined;
|
|
36
36
|
}, {
|
|
37
|
-
toolName?: string[] | undefined;
|
|
38
37
|
thought?: string[] | undefined;
|
|
38
|
+
toolName?: string[] | undefined;
|
|
39
39
|
toolInput?: string[] | undefined;
|
|
40
40
|
toolOutput?: string[] | undefined;
|
|
41
41
|
finalAnswer?: string[] | undefined;
|
|
@@ -57,12 +57,12 @@ declare const GraniteReActAgentSystemPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
57
57
|
}, zod.ZodTypeAny, "passthrough">>, "many">;
|
|
58
58
|
createdAt: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
59
59
|
}, "strip", zod.ZodTypeAny, {
|
|
60
|
-
instructions: string;
|
|
61
60
|
tools: zod.objectOutputType<{
|
|
62
61
|
name: zod.ZodString;
|
|
63
62
|
description: zod.ZodString;
|
|
64
63
|
schema: zod.ZodString;
|
|
65
64
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
65
|
+
instructions: string;
|
|
66
66
|
createdAt?: string | null | undefined;
|
|
67
67
|
}, {
|
|
68
68
|
tools: zod.objectInputType<{
|
|
@@ -70,8 +70,8 @@ declare const GraniteReActAgentSystemPrompt: PromptTemplate<zod.ZodObject<{
|
|
|
70
70
|
description: zod.ZodString;
|
|
71
71
|
schema: zod.ZodString;
|
|
72
72
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
73
|
-
createdAt?: string | null | undefined;
|
|
74
73
|
instructions?: string | undefined;
|
|
74
|
+
createdAt?: string | null | undefined;
|
|
75
75
|
}>>;
|
|
76
76
|
declare const GraniteReActAgentSchemaErrorPrompt: PromptTemplate<zod.ZodObject<{}, "passthrough", zod.ZodTypeAny, zod.objectOutputType<{}, zod.ZodTypeAny, "passthrough">, zod.objectInputType<{}, zod.ZodTypeAny, "passthrough">>>;
|
|
77
77
|
declare const GraniteReActAgentUserPrompt: PromptTemplate<zod.ZodObject<{
|
|
@@ -24,13 +24,13 @@ import '../../../../internals/helpers/promise.cjs';
|
|
|
24
24
|
import '../../../../backend/message.cjs';
|
|
25
25
|
import 'ai';
|
|
26
26
|
import '../../../../memory/base.cjs';
|
|
27
|
-
import '
|
|
27
|
+
import '../../prompts.cjs';
|
|
28
28
|
import '../../../../backend/chat.cjs';
|
|
29
29
|
import '../../../../backend/utils.cjs';
|
|
30
30
|
import '../../../../backend/constants.cjs';
|
|
31
31
|
import '@ai-sdk/provider';
|
|
32
|
+
import '../../../../base-daM6fHUP.cjs';
|
|
32
33
|
import '../../../../internals/helpers/counter.cjs';
|
|
33
|
-
import '../../prompts.cjs';
|
|
34
34
|
|
|
35
35
|
declare class GraniteRunner extends DefaultRunner {
|
|
36
36
|
protected useNativeToolCalling: boolean;
|
|
@@ -52,12 +52,12 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
52
52
|
}, zod.ZodTypeAny, "passthrough">>, "many">;
|
|
53
53
|
createdAt: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
54
54
|
}, "strip", zod.ZodTypeAny, {
|
|
55
|
-
instructions: string;
|
|
56
55
|
tools: zod.objectOutputType<{
|
|
57
56
|
name: zod.ZodString;
|
|
58
57
|
description: zod.ZodString;
|
|
59
58
|
schema: zod.ZodString;
|
|
60
59
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
60
|
+
instructions: string;
|
|
61
61
|
createdAt?: string | null | undefined;
|
|
62
62
|
}, {
|
|
63
63
|
tools: zod.objectInputType<{
|
|
@@ -65,8 +65,8 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
65
65
|
description: zod.ZodString;
|
|
66
66
|
schema: zod.ZodString;
|
|
67
67
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
68
|
-
createdAt?: string | null | undefined;
|
|
69
68
|
instructions?: string | undefined;
|
|
69
|
+
createdAt?: string | null | undefined;
|
|
70
70
|
}>>;
|
|
71
71
|
assistant: PromptTemplate<zod.ZodObject<{
|
|
72
72
|
thought: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
@@ -75,14 +75,14 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
75
75
|
toolOutput: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
76
76
|
finalAnswer: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
77
77
|
}, "strip", zod.ZodTypeAny, {
|
|
78
|
-
toolName?: string[] | undefined;
|
|
79
78
|
thought?: string[] | undefined;
|
|
79
|
+
toolName?: string[] | undefined;
|
|
80
80
|
toolInput?: string[] | undefined;
|
|
81
81
|
toolOutput?: string[] | undefined;
|
|
82
82
|
finalAnswer?: string[] | undefined;
|
|
83
83
|
}, {
|
|
84
|
-
toolName?: string[] | undefined;
|
|
85
84
|
thought?: string[] | undefined;
|
|
85
|
+
toolName?: string[] | undefined;
|
|
86
86
|
toolInput?: string[] | undefined;
|
|
87
87
|
toolOutput?: string[] | undefined;
|
|
88
88
|
finalAnswer?: string[] | undefined;
|
|
@@ -24,13 +24,13 @@ import '../../../../internals/helpers/promise.js';
|
|
|
24
24
|
import '../../../../backend/message.js';
|
|
25
25
|
import 'ai';
|
|
26
26
|
import '../../../../memory/base.js';
|
|
27
|
-
import '
|
|
27
|
+
import '../../prompts.js';
|
|
28
28
|
import '../../../../backend/chat.js';
|
|
29
29
|
import '../../../../backend/utils.js';
|
|
30
30
|
import '../../../../backend/constants.js';
|
|
31
31
|
import '@ai-sdk/provider';
|
|
32
|
+
import '../../../../base-mmG-8Pk9.js';
|
|
32
33
|
import '../../../../internals/helpers/counter.js';
|
|
33
|
-
import '../../prompts.js';
|
|
34
34
|
|
|
35
35
|
declare class GraniteRunner extends DefaultRunner {
|
|
36
36
|
protected useNativeToolCalling: boolean;
|
|
@@ -52,12 +52,12 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
52
52
|
}, zod.ZodTypeAny, "passthrough">>, "many">;
|
|
53
53
|
createdAt: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
54
54
|
}, "strip", zod.ZodTypeAny, {
|
|
55
|
-
instructions: string;
|
|
56
55
|
tools: zod.objectOutputType<{
|
|
57
56
|
name: zod.ZodString;
|
|
58
57
|
description: zod.ZodString;
|
|
59
58
|
schema: zod.ZodString;
|
|
60
59
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
60
|
+
instructions: string;
|
|
61
61
|
createdAt?: string | null | undefined;
|
|
62
62
|
}, {
|
|
63
63
|
tools: zod.objectInputType<{
|
|
@@ -65,8 +65,8 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
65
65
|
description: zod.ZodString;
|
|
66
66
|
schema: zod.ZodString;
|
|
67
67
|
}, zod.ZodTypeAny, "passthrough">[];
|
|
68
|
-
createdAt?: string | null | undefined;
|
|
69
68
|
instructions?: string | undefined;
|
|
69
|
+
createdAt?: string | null | undefined;
|
|
70
70
|
}>>;
|
|
71
71
|
assistant: PromptTemplate<zod.ZodObject<{
|
|
72
72
|
thought: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
@@ -75,14 +75,14 @@ declare class GraniteRunner extends DefaultRunner {
|
|
|
75
75
|
toolOutput: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
76
76
|
finalAnswer: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
|
|
77
77
|
}, "strip", zod.ZodTypeAny, {
|
|
78
|
-
toolName?: string[] | undefined;
|
|
79
78
|
thought?: string[] | undefined;
|
|
79
|
+
toolName?: string[] | undefined;
|
|
80
80
|
toolInput?: string[] | undefined;
|
|
81
81
|
toolOutput?: string[] | undefined;
|
|
82
82
|
finalAnswer?: string[] | undefined;
|
|
83
83
|
}, {
|
|
84
|
-
toolName?: string[] | undefined;
|
|
85
84
|
thought?: string[] | undefined;
|
|
85
|
+
toolName?: string[] | undefined;
|
|
86
86
|
toolInput?: string[] | undefined;
|
|
87
87
|
toolOutput?: string[] | undefined;
|
|
88
88
|
finalAnswer?: string[] | undefined;
|
|
@@ -12,10 +12,7 @@ const ToolCallingAgentSystemPrompt = new template_cjs.PromptTemplate({
|
|
|
12
12
|
}),
|
|
13
13
|
functions: {
|
|
14
14
|
formatDate: /* @__PURE__ */ __name(function() {
|
|
15
|
-
return new
|
|
16
|
-
dateStyle: "full",
|
|
17
|
-
timeStyle: "medium"
|
|
18
|
-
}).format(/* @__PURE__ */ new Date());
|
|
15
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
19
16
|
}, "formatDate")
|
|
20
17
|
},
|
|
21
18
|
defaults: {
|
|
@@ -37,7 +34,9 @@ You can use tools to improve your answers if available.
|
|
|
37
34
|
- If the provided task is unclear, ask the user for clarification.
|
|
38
35
|
- Do not refer to tools or tool outputs by name when responding.
|
|
39
36
|
|
|
37
|
+
# Date and Time
|
|
40
38
|
The current date and time is: {{formatDate}}
|
|
39
|
+
You do not need a tool to get the current Date and Time. Use the information available here.
|
|
41
40
|
`
|
|
42
41
|
});
|
|
43
42
|
const ToolCallingAgentTaskPrompt = new template_cjs.PromptTemplate({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["prompts.ts"],"names":["ToolCallingAgentSystemPrompt","PromptTemplate","schema","z","object","role","string","instructions","optional","functions","formatDate","
|
|
1
|
+
{"version":3,"sources":["prompts.ts"],"names":["ToolCallingAgentSystemPrompt","PromptTemplate","schema","z","object","role","string","instructions","optional","functions","formatDate","Date","toISOString","defaults","template","ToolCallingAgentTaskPrompt","prompt","context","expectedOutput"],"mappings":";;;;;;;AAmBaA,MAAAA,4BAAAA,GAA+B,IAAIC,2BAAe,CAAA;AAC7DC,EAAAA,MAAAA,EAAQC,MAAEC,MAAO,CAAA;AACfC,IAAAA,IAAAA,EAAMF,MAAEG,MAAM,EAAA;IACdC,YAAcJ,EAAAA,KAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ;GACnC,CAAA;EACAC,SAAW,EAAA;AACTC,IAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,MAAO,OAAA,iBAAA,IAAIC,IAAAA,EAAAA,EAAOC,WAAW,EAAA;KADnB,EAAA,YAAA;AAGd,GAAA;EACAC,QAAU,EAAA;IAAER,IAAM,EAAA,wBAAA;IAA0BE,YAAc,EAAA;AAAG,GAAA;EAC7DO,QAAU,EAAA,CAAA;;;;;;;;;;;;;;;;;;;AAmBZ,CAAA;AAEaC,MAAAA,0BAAAA,GAA6B,IAAId,2BAAe,CAAA;AAC3DC,EAAAA,MAAAA,EAAQC,MAAEC,MAAO,CAAA;AACfY,IAAAA,MAAAA,EAAQb,MAAEG,MAAM,EAAA;IAChBW,OAASd,EAAAA,KAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ,EAAA;IAC5BU,cAAgBf,EAAAA,KAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ;GACrC,CAAA;EACAM,QAAU,EAAA,CAAA;;;;;;;;;;;AAWZ,CAAA","file":"prompts.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate } from \"@/template.js\";\nimport { z } from \"zod\";\n\nexport const ToolCallingAgentSystemPrompt = new PromptTemplate({\n schema: z.object({\n role: z.string(),\n instructions: z.string().optional(),\n }),\n functions: {\n formatDate: function () {\n return new Date().toISOString();\n },\n },\n defaults: { role: \"A helpful AI assistant\", instructions: \"\" },\n template: `Assume the role of {{role}}.\n{{#instructions}}\n\nYour instructions are:\n{{.}}\n{{/instructions}}\n\nWhen the user sends a message, figure out a solution and provide a final answer.\nYou can use tools to improve your answers if available.\n\n# Best practices\n- Use markdown syntax to format code snippets, links, JSON, tables, images, and files.\n- If the provided task is unclear, ask the user for clarification.\n- Do not refer to tools or tool outputs by name when responding.\n\n# Date and Time\nThe current date and time is: {{formatDate}}\nYou do not need a tool to get the current Date and Time. Use the information available here.\n`,\n});\n\nexport const ToolCallingAgentTaskPrompt = new PromptTemplate({\n schema: z.object({\n prompt: z.string(),\n context: z.string().optional(),\n expectedOutput: z.string().optional(),\n }),\n template: `{{#context}}This is the context that you are working with:\n{{.}}\n\n{{/context}}\n{{#expectedOutput}}\nThis is the expected criteria for your output:\n{{.}}\n\n{{/expectedOutput}}\nYour task: {{prompt}}\n`,\n});\n"]}
|
|
@@ -10,10 +10,7 @@ const ToolCallingAgentSystemPrompt = new PromptTemplate({
|
|
|
10
10
|
}),
|
|
11
11
|
functions: {
|
|
12
12
|
formatDate: /* @__PURE__ */ __name(function() {
|
|
13
|
-
return new
|
|
14
|
-
dateStyle: "full",
|
|
15
|
-
timeStyle: "medium"
|
|
16
|
-
}).format(/* @__PURE__ */ new Date());
|
|
13
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
17
14
|
}, "formatDate")
|
|
18
15
|
},
|
|
19
16
|
defaults: {
|
|
@@ -35,7 +32,9 @@ You can use tools to improve your answers if available.
|
|
|
35
32
|
- If the provided task is unclear, ask the user for clarification.
|
|
36
33
|
- Do not refer to tools or tool outputs by name when responding.
|
|
37
34
|
|
|
35
|
+
# Date and Time
|
|
38
36
|
The current date and time is: {{formatDate}}
|
|
37
|
+
You do not need a tool to get the current Date and Time. Use the information available here.
|
|
39
38
|
`
|
|
40
39
|
});
|
|
41
40
|
const ToolCallingAgentTaskPrompt = new PromptTemplate({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["prompts.ts"],"names":["ToolCallingAgentSystemPrompt","PromptTemplate","schema","z","object","role","string","instructions","optional","functions","formatDate","
|
|
1
|
+
{"version":3,"sources":["prompts.ts"],"names":["ToolCallingAgentSystemPrompt","PromptTemplate","schema","z","object","role","string","instructions","optional","functions","formatDate","Date","toISOString","defaults","template","ToolCallingAgentTaskPrompt","prompt","context","expectedOutput"],"mappings":";;;;;AAmBaA,MAAAA,4BAAAA,GAA+B,IAAIC,cAAe,CAAA;AAC7DC,EAAAA,MAAAA,EAAQC,EAAEC,MAAO,CAAA;AACfC,IAAAA,IAAAA,EAAMF,EAAEG,MAAM,EAAA;IACdC,YAAcJ,EAAAA,CAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ;GACnC,CAAA;EACAC,SAAW,EAAA;AACTC,IAAAA,UAAAA,kBAAY,MAAA,CAAA,WAAA;AACV,MAAO,OAAA,iBAAA,IAAIC,IAAAA,EAAAA,EAAOC,WAAW,EAAA;KADnB,EAAA,YAAA;AAGd,GAAA;EACAC,QAAU,EAAA;IAAER,IAAM,EAAA,wBAAA;IAA0BE,YAAc,EAAA;AAAG,GAAA;EAC7DO,QAAU,EAAA,CAAA;;;;;;;;;;;;;;;;;;;AAmBZ,CAAA;AAEaC,MAAAA,0BAAAA,GAA6B,IAAId,cAAe,CAAA;AAC3DC,EAAAA,MAAAA,EAAQC,EAAEC,MAAO,CAAA;AACfY,IAAAA,MAAAA,EAAQb,EAAEG,MAAM,EAAA;IAChBW,OAASd,EAAAA,CAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ,EAAA;IAC5BU,cAAgBf,EAAAA,CAAAA,CAAEG,MAAM,EAAA,CAAGE,QAAQ;GACrC,CAAA;EACAM,QAAU,EAAA,CAAA;;;;;;;;;;;AAWZ,CAAA","file":"prompts.js","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate } from \"@/template.js\";\nimport { z } from \"zod\";\n\nexport const ToolCallingAgentSystemPrompt = new PromptTemplate({\n schema: z.object({\n role: z.string(),\n instructions: z.string().optional(),\n }),\n functions: {\n formatDate: function () {\n return new Date().toISOString();\n },\n },\n defaults: { role: \"A helpful AI assistant\", instructions: \"\" },\n template: `Assume the role of {{role}}.\n{{#instructions}}\n\nYour instructions are:\n{{.}}\n{{/instructions}}\n\nWhen the user sends a message, figure out a solution and provide a final answer.\nYou can use tools to improve your answers if available.\n\n# Best practices\n- Use markdown syntax to format code snippets, links, JSON, tables, images, and files.\n- If the provided task is unclear, ask the user for clarification.\n- Do not refer to tools or tool outputs by name when responding.\n\n# Date and Time\nThe current date and time is: {{formatDate}}\nYou do not need a tool to get the current Date and Time. Use the information available here.\n`,\n});\n\nexport const ToolCallingAgentTaskPrompt = new PromptTemplate({\n schema: z.object({\n prompt: z.string(),\n context: z.string().optional(),\n expectedOutput: z.string().optional(),\n }),\n template: `{{#context}}This is the context that you are working with:\n{{.}}\n\n{{/context}}\n{{#expectedOutput}}\nThis is the expected criteria for your output:\n{{.}}\n\n{{/expectedOutput}}\nYour task: {{prompt}}\n`,\n});\n"]}
|
package/dist/backend/chat.cjs
CHANGED
|
@@ -5,8 +5,6 @@ var utils_cjs = require('../serializer/utils.cjs');
|
|
|
5
5
|
var object_cjs = require('../internals/helpers/object.cjs');
|
|
6
6
|
var number_cjs = require('../internals/helpers/number.cjs');
|
|
7
7
|
var context_cjs = require('../context.cjs');
|
|
8
|
-
var config_cjs = require('../instrumentation/config.cjs');
|
|
9
|
-
var createTelemetryMiddleware_cjs = require('../instrumentation/create-telemetry-middleware.cjs');
|
|
10
8
|
var remeda = require('remeda');
|
|
11
9
|
var decoratorCache_cjs = require('../cache/decoratorCache.cjs');
|
|
12
10
|
var promiseBasedTask = require('promise-based-task');
|
|
@@ -79,7 +77,7 @@ class ChatModel extends serializable_cjs.Serializable {
|
|
|
79
77
|
} finally {
|
|
80
78
|
await run.emitter.emit("finish", null);
|
|
81
79
|
}
|
|
82
|
-
})
|
|
80
|
+
});
|
|
83
81
|
}
|
|
84
82
|
createStructure(input) {
|
|
85
83
|
return context_cjs.RunContext.enter(this, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["chat.ts"],"names":["ChatModel","Serializable","cache","NullCache","parameters","create","input","shallowCopy","RunContext","enter","params","signal","abortSignal","run","cacheEntry","createCacheAccessor","emitter","emit","chunks","generator","value","stream","_createStream","toAsyncGenerator","_create","controller","AbortController","push","callbacks","abort","aborted","resolve","result","ChatModelOutput","fromChunks","error","reject","ChatModelError","middleware","INSTRUMENTATION_ENABLED","createTelemetryMiddleware","doNothing","createStructure","_createStructure","config","isFunction","fromName","name","options","providerId","modelId","parseModel","Target","loadModel","undefined","schema","jsonSchema","toJsonSchema","systemTemplate","PromptTemplate","z","object","string","min","template","messages","SystemMessage","render","JSON","stringify","errorTemplate","errors","expected","received","Retryable","executor","response","responseFormat","type","textResponse","getTextContent","parseBrokenJson","pair","validator","createSchemaValidator","success","context","UserMessage","maxRetries","get","createSnapshot","destroy","_","tools","key","ObjectHashKeyFn","Serializer","serialize","map","msg","toPlain","isNew","task","Task","set","delete","constructor","usage","finishReason","final","forEach","cur","merge","other","customMerge","totalTokens","takeBigger","promptTokens","completionTokens","getToolCalls","filter","r","AssistantMessage","flatMap","Boolean","getTextMessages","text","join","toString","loadSnapshot","snapshot","Object","assign"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAuIO,MAAeA,kBAAkBC,6BAAAA,CAAAA;EAvIxC;;;AAyISC,EAAAA,KAAAA,GAAwB,IAAIC,uBAAAA,EAAAA;AAC5BC,EAAAA,UAAAA,GAAkC,EAAC;AAK1CC,EAAAA,MAAAA,CAAOC,KAA8C,EAAA;AACnDA,IAAAA,KAAAA,GAAQC,sBAAYD,KAAAA,CAAAA;AAEpB,IAAOE,OAAAA,sBAAAA,CAAWC,MAChB,IACA,EAAA;MAAEC,MAAQ,EAAA;AAACJ,QAAAA;;AAAiBK,MAAAA,MAAAA,EAAQL,KAAOM,EAAAA;AAAY,KAAA,EACvD,OAAOC,GAAAA,KAAAA;AACL,MAAA,MAAMC,UAAa,GAAA,MAAM,IAAKC,CAAAA,mBAAAA,CAAoBT,KAAAA,CAAAA;AAElD,MAAI,IAAA;AACF,QAAMO,MAAAA,GA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0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Serializable } from \"@/internals/serializable.js\";\nimport { shallowCopy } from \"@/serializer/utils.js\";\nimport { customMerge } from \"@/internals/helpers/object.js\";\nimport { takeBigger } from \"@/internals/helpers/number.js\";\nimport { Callback } from \"@/emitter/types.js\";\nimport { FrameworkError } from \"@/errors.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport { GetRunContext, RunContext } from \"@/context.js\";\nimport { INSTRUMENTATION_ENABLED } from \"@/instrumentation/config.js\";\nimport { createTelemetryMiddleware } from \"@/instrumentation/create-telemetry-middleware.js\";\nimport { doNothing, isFunction } from \"remeda\";\nimport { ObjectHashKeyFn } from \"@/cache/decoratorCache.js\";\nimport { Task } from \"promise-based-task\";\nimport { NullCache } from \"@/cache/nullCache.js\";\nimport { BaseCache } from \"@/cache/base.js\";\nimport { FullModelName, loadModel, parseModel } from \"@/backend/utils.js\";\nimport { ProviderName } from \"@/backend/constants.js\";\nimport { AnyTool } from \"@/tools/base.js\";\nimport { AssistantMessage, Message, SystemMessage, UserMessage } from \"@/backend/message.js\";\nimport {\n JSONSchema7,\n LanguageModelV1FunctionTool,\n LanguageModelV1ProviderDefinedTool,\n LanguageModelV1ToolChoice,\n} from \"@ai-sdk/provider\";\nimport { ChatModelError } from \"@/backend/errors.js\";\nimport { z } from \"zod\";\nimport {\n createSchemaValidator,\n parseBrokenJson,\n toJsonSchema,\n} from \"@/internals/helpers/schema.js\";\nimport { Retryable } from \"@/internals/helpers/retryable.js\";\nimport type { ValidateFunction } from \"ajv\";\nimport { PromptTemplate } from \"@/template.js\";\nimport { toAsyncGenerator } from \"@/internals/helpers/promise.js\";\nimport { Serializer } from \"@/serializer/serializer.js\";\n\nexport interface ChatModelParameters {\n maxTokens?: number;\n topP?: number;\n frequencyPenalty?: number;\n temperature?: number;\n topK?: number;\n n?: number;\n presencePenalty?: number;\n seed?: number;\n stopSequences?: string[];\n}\n\nexport interface ChatModelObjectInput<T> extends ChatModelParameters {\n schema: z.ZodSchema<T>;\n messages: Message[];\n abortSignal?: AbortSignal;\n maxRetries?: number;\n}\n\nexport interface ChatModelObjectOutput<T> {\n object: T;\n}\n\nexport interface ChatModelInput extends ChatModelParameters {\n tools?: AnyTool[];\n abortSignal?: AbortSignal;\n stopSequences?: string[];\n responseFormat?:\n | {\n type: \"regular\";\n tools?: (LanguageModelV1FunctionTool | LanguageModelV1ProviderDefinedTool)[];\n toolChoice?: LanguageModelV1ToolChoice;\n }\n | {\n type: \"object-json\";\n schema?: JSONSchema7;\n name?: string;\n description?: string;\n }\n | {\n type: \"object-tool\";\n tool: LanguageModelV1FunctionTool;\n };\n toolChoice?: never; // TODO\n messages: Message[];\n}\n\nexport type ChatModelFinishReason =\n | \"stop\"\n | \"length\"\n | \"content-filter\"\n | \"tool-calls\"\n | \"error\"\n | \"other\"\n | \"unknown\";\n\nexport interface ChatModelUsage {\n promptTokens: number;\n completionTokens: number;\n totalTokens: number;\n}\n\nexport interface ChatModelEvents {\n newToken?: Callback<{ value: ChatModelOutput; callbacks: { abort: () => void } }>;\n success?: Callback<{ value: ChatModelOutput }>;\n start?: Callback<{ input: ChatModelInput }>;\n error?: Callback<{ input: ChatModelInput; error: FrameworkError }>;\n finish?: Callback<null>;\n}\n\nexport type ChatModelEmitter<A = Record<never, never>> = Emitter<\n ChatModelEvents & Omit<A, keyof ChatModelEvents>\n>;\n\nexport type ChatModelCache = BaseCache<Task<ChatModelOutput[]>>;\nexport type ConfigFn<T> = (value: T) => T;\nexport interface ChatConfig {\n cache?: ChatModelCache | ConfigFn<ChatModelCache>;\n parameters?: ChatModelParameters | ConfigFn<ChatModelParameters>;\n}\n\nexport abstract class ChatModel extends Serializable {\n public abstract readonly emitter: Emitter<ChatModelEvents>;\n public cache: ChatModelCache = new NullCache();\n public parameters: ChatModelParameters = {};\n\n abstract get modelId(): string;\n abstract get providerId(): string;\n\n create(input: ChatModelInput & { stream?: boolean }) {\n input = shallowCopy(input);\n\n return RunContext.enter(\n this,\n { params: [input] as const, signal: input?.abortSignal },\n async (run) => {\n const cacheEntry = await this.createCacheAccessor(input);\n\n try {\n await run.emitter.emit(\"start\", { input });\n const chunks: ChatModelOutput[] = [];\n\n const generator =\n cacheEntry.value ??\n (input.stream\n ? this._createStream(input, run)\n : toAsyncGenerator(this._create(input, run)));\n\n const controller = new AbortController();\n for await (const value of generator) {\n chunks.push(value);\n await run.emitter.emit(\"newToken\", {\n value,\n callbacks: { abort: () => controller.abort() },\n });\n if (controller.signal.aborted) {\n break;\n }\n }\n\n cacheEntry.resolve(chunks);\n const result = ChatModelOutput.fromChunks(chunks);\n await run.emitter.emit(\"success\", { value: result });\n return result;\n } catch (error) {\n await run.emitter.emit(\"error\", { input, error });\n await cacheEntry.reject(error);\n if (error instanceof ChatModelError) {\n throw error;\n } else {\n throw new ChatModelError(`The Chat Model has encountered an error.`, [error]);\n }\n } finally {\n await run.emitter.emit(\"finish\", null);\n }\n },\n ).middleware(INSTRUMENTATION_ENABLED ? createTelemetryMiddleware() : doNothing());\n }\n\n createStructure<T>(input: ChatModelObjectInput<T>) {\n return RunContext.enter(\n this,\n { params: [input] as const, signal: input?.abortSignal },\n async (run) => {\n return await this._createStructure<T>(input, run);\n },\n );\n }\n\n config({ cache, parameters }: ChatConfig): void {\n if (cache) {\n this.cache = isFunction(cache) ? cache(this.cache) : cache;\n }\n if (parameters) {\n this.parameters = isFunction(parameters) ? parameters(this.parameters) : parameters;\n }\n }\n\n static async fromName(name: FullModelName | ProviderName, options?: ChatModelParameters) {\n const { providerId, modelId } = parseModel(name);\n const Target = await loadModel<ChatModel>(providerId, \"chat\");\n return new Target(modelId || undefined, options);\n }\n\n protected abstract _create(\n input: ChatModelInput,\n run: GetRunContext<typeof this>,\n ): Promise<ChatModelOutput>;\n protected abstract _createStream(\n input: ChatModelInput,\n run: GetRunContext<typeof this>,\n ): AsyncGenerator<ChatModelOutput, void>;\n\n protected async _createStructure<T>(\n input: ChatModelObjectInput<T>,\n run: GetRunContext<typeof this>,\n ): Promise<ChatModelObjectOutput<T>> {\n const { schema, ...options } = input;\n const jsonSchema = toJsonSchema(schema);\n\n const systemTemplate = new PromptTemplate({\n schema: z.object({\n schema: z.string().min(1),\n }),\n template: `You are a helpful assistant that generates only valid JSON adhering to the following JSON Schema.\n\n\\`\\`\\`\n{{schema}}\n\\`\\`\\`\n\nIMPORTANT: You MUST answer with a JSON object that matches the JSON schema above.`,\n });\n\n const messages: Message[] = [\n new SystemMessage(systemTemplate.render({ schema: JSON.stringify(jsonSchema, null, 2) })),\n ...input.messages,\n ];\n\n const errorTemplate = new PromptTemplate({\n schema: z.object({\n errors: z.string(),\n expected: z.string(),\n received: z.string(),\n }),\n template: `Generated object does not match the expected JSON schema!\n\nValidation Errors: {{errors}}`,\n });\n\n return new Retryable<ChatModelObjectOutput<T>>({\n executor: async () => {\n const response = await this._create(\n {\n ...options,\n messages,\n responseFormat: { type: \"object-json\" },\n },\n run,\n );\n\n const textResponse = response.getTextContent();\n const object: T = parseBrokenJson(textResponse, { pair: [\"{\", \"}\"] });\n const validator = createSchemaValidator(schema) as ValidateFunction<T>;\n\n const success = validator(object);\n if (!success) {\n const context = {\n expected: JSON.stringify(jsonSchema),\n received: textResponse,\n errors: JSON.stringify(validator.errors ?? []),\n };\n\n messages.push(new UserMessage(errorTemplate.render(context)));\n throw new ChatModelError(`LLM did not produce a valid output.`, [], {\n context,\n });\n }\n\n return { object };\n },\n config: {\n signal: run.signal,\n maxRetries: input?.maxRetries || 1,\n },\n }).get();\n }\n\n createSnapshot() {\n return { cache: this.cache, emitter: this.emitter, parameters: shallowCopy(this.parameters) };\n }\n\n destroy() {\n this.emitter.destroy();\n }\n\n protected async createCacheAccessor({\n abortSignal: _,\n messages,\n tools = [],\n ...input\n }: ChatModelInput) {\n const key = ObjectHashKeyFn({\n ...input,\n messages: await Serializer.serialize(messages.map((msg) => msg.toPlain())),\n tools: await Serializer.serialize(tools),\n });\n const value = await this.cache.get(key);\n const isNew = value === undefined;\n\n let task: Task<ChatModelOutput[]> | null = null;\n if (isNew) {\n task = new Task();\n await this.cache.set(key, task);\n }\n\n return {\n key,\n value,\n resolve: <T2 extends ChatModelOutput>(value: T2[]) => {\n task?.resolve?.(value);\n },\n reject: async (error: Error) => {\n task?.reject?.(error);\n if (isNew) {\n await this.cache.delete(key);\n }\n },\n };\n }\n}\n\nexport class ChatModelOutput extends Serializable {\n constructor(\n public readonly messages: Message[],\n public usage?: ChatModelUsage,\n public finishReason?: ChatModelFinishReason,\n ) {\n super();\n }\n\n static fromChunks(chunks: ChatModelOutput[]) {\n const final = new ChatModelOutput([]);\n chunks.forEach((cur) => final.merge(cur));\n return final;\n }\n\n merge(other: ChatModelOutput) {\n this.messages.push(...other.messages);\n this.finishReason = other.finishReason;\n if (this.usage && other.usage) {\n this.usage = customMerge([this.usage, other.usage], {\n totalTokens: takeBigger,\n promptTokens: takeBigger,\n completionTokens: takeBigger,\n });\n } else if (other.usage) {\n this.usage = shallowCopy(other.usage);\n }\n }\n\n getToolCalls() {\n return this.messages\n .filter((r) => r instanceof AssistantMessage)\n .flatMap((r) => r.getToolCalls())\n .filter(Boolean);\n }\n\n getTextMessages(): AssistantMessage[] {\n return this.messages.filter((r) => r instanceof AssistantMessage).filter((r) => r.text);\n }\n\n getTextContent(): string {\n return this.messages\n .filter((r) => r instanceof AssistantMessage)\n .flatMap((r) => r.text)\n .filter(Boolean)\n .join(\"\");\n }\n\n toString() {\n return this.getTextContent();\n }\n\n createSnapshot() {\n return {\n messages: shallowCopy(this.messages),\n usage: shallowCopy(this.usage),\n finishReason: this.finishReason,\n };\n }\n\n loadSnapshot(snapshot: ReturnType<typeof this.createSnapshot>) {\n Object.assign(this, snapshot);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["chat.ts"],"names":["ChatModel","Serializable","cache","NullCache","parameters","create","input","shallowCopy","RunContext","enter","params","signal","abortSignal","run","cacheEntry","createCacheAccessor","emitter","emit","chunks","generator","value","stream","_createStream","toAsyncGenerator","_create","controller","AbortController","push","callbacks","abort","aborted","resolve","result","ChatModelOutput","fromChunks","error","reject","ChatModelError","createStructure","_createStructure","config","isFunction","fromName","name","options","providerId","modelId","parseModel","Target","loadModel","undefined","schema","jsonSchema","toJsonSchema","systemTemplate","PromptTemplate","z","object","string","min","template","messages","SystemMessage","render","JSON","stringify","errorTemplate","errors","expected","received","Retryable","executor","response","responseFormat","type","textResponse","getTextContent","parseBrokenJson","pair","validator","createSchemaValidator","success","context","UserMessage","maxRetries","get","createSnapshot","destroy","_","tools","key","ObjectHashKeyFn","Serializer","serialize","map","msg","toPlain","isNew","task","Task","set","delete","constructor","usage","finishReason","final","forEach","cur","merge","other","customMerge","totalTokens","takeBigger","promptTokens","completionTokens","getToolCalls","filter","r","AssistantMessage","flatMap","Boolean","getTextMessages","text","join","toString","loadSnapshot","snapshot","Object","assign"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAqIO,MAAeA,kBAAkBC,6BAAAA,CAAAA;EArIxC;;;AAuISC,EAAAA,KAAAA,GAAwB,IAAIC,uBAAAA,EAAAA;AAC5BC,EAAAA,UAAAA,GAAkC,EAAC;AAK1CC,EAAAA,MAAAA,CAAOC,KAA8C,EAAA;AACnDA,IAAAA,KAAAA,GAAQC,sBAAYD,KAAAA,CAAAA;AAEpB,IAAOE,OAAAA,sBAAAA,CAAWC,MAChB,IACA,EAAA;MAAEC,MAAQ,EAAA;AAACJ,QAAAA;;AAAiBK,MAAAA,MAAAA,EAAQL,KAAOM,EAAAA;AAAY,KAAA,EACvD,OAAOC,GAAAA,KAAAA;AACL,MAAA,MAAMC,UAAa,GAAA,MAAM,IAAKC,CAAAA,mBAAAA,CAA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AAA,KAAAA,GAAQhG,qBAAYsG,CAAAA,KAAAA,CAAMN,KAAK,CAAA;AACtC;AACF;EAEAY,YAAe,GAAA;AACb,IAAA,OAAO,KAAKtD,QACTuD,CAAAA,MAAAA,CAAO,CAACC,CAAAA,KAAMA,aAAaC,4BAAAA,CAAAA,CAC3BC,OAAQ,CAAA,CAACF,MAAMA,CAAEF,CAAAA,YAAAA,EAAY,CAAA,CAC7BC,OAAOI,OAAAA,CAAAA;AACZ;EAEAC,eAAsC,GAAA;AACpC,IAAA,OAAO,IAAK5D,CAAAA,QAAAA,CAASuD,MAAO,CAAA,CAACC,CAAMA,KAAAA,CAAAA,YAAaC,4BAAAA,CAAAA,CAAkBF,MAAO,CAAA,CAACC,CAAMA,KAAAA,CAAAA,CAAEK,IAAI,CAAA;AACxF;EAEA9C,cAAyB,GAAA;AACvB,IAAA,OAAO,KAAKf,QACTuD,CAAAA,MAAAA,CAAO,CAACC,CAAMA,KAAAA,CAAAA,YAAaC,4BAAAA,CAC3BC,CAAAA,OAAAA,CAAQ,CAACF,CAAAA,KAAMA,EAAEK,IAAI,CAAA,CACrBN,OAAOI,OAAAA,CAAAA,CACPG,KAAK,EAAA,CAAA;AACV;EAEAC,QAAW,GAAA;AACT,IAAA,OAAO,KAAKhD,cAAc,EAAA;AAC5B;EAEAU,cAAiB,GAAA;AACf,IAAO,OAAA;MACLzB,QAAUtD,EAAAA,qBAAAA,CAAY,KAAKsD,QAAQ,CAAA;MACnC0C,KAAOhG,EAAAA,qBAAAA,CAAY,KAAKgG,KAAK,CAAA;AAC7BC,MAAAA,YAAAA,EAAc,IAAKA,CAAAA;AACrB,KAAA;AACF;AAEAqB,EAAAA,YAAAA,CAAaC,QAAkD,EAAA;AAC7DC,IAAOC,MAAAA,CAAAA,MAAAA,CAAO,MAAMF,QAAAA,CAAAA;AACtB;AACF","file":"chat.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Serializable } from \"@/internals/serializable.js\";\nimport { shallowCopy } from \"@/serializer/utils.js\";\nimport { customMerge } from \"@/internals/helpers/object.js\";\nimport { takeBigger } from \"@/internals/helpers/number.js\";\nimport { Callback } from \"@/emitter/types.js\";\nimport { FrameworkError } from \"@/errors.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport { GetRunContext, RunContext } from \"@/context.js\";\nimport { isFunction } from \"remeda\";\nimport { ObjectHashKeyFn } from \"@/cache/decoratorCache.js\";\nimport { Task } from \"promise-based-task\";\nimport { NullCache } from \"@/cache/nullCache.js\";\nimport { BaseCache } from \"@/cache/base.js\";\nimport { FullModelName, loadModel, parseModel } from \"@/backend/utils.js\";\nimport { ProviderName } from \"@/backend/constants.js\";\nimport { AnyTool } from \"@/tools/base.js\";\nimport { AssistantMessage, Message, SystemMessage, UserMessage } from \"@/backend/message.js\";\nimport {\n JSONSchema7,\n LanguageModelV1FunctionTool,\n LanguageModelV1ProviderDefinedTool,\n LanguageModelV1ToolChoice,\n} from \"@ai-sdk/provider\";\nimport { ChatModelError } from \"@/backend/errors.js\";\nimport { z } from \"zod\";\nimport {\n createSchemaValidator,\n parseBrokenJson,\n toJsonSchema,\n} from \"@/internals/helpers/schema.js\";\nimport { Retryable } from \"@/internals/helpers/retryable.js\";\nimport type { ValidateFunction } from \"ajv\";\nimport { PromptTemplate } from \"@/template.js\";\nimport { toAsyncGenerator } from \"@/internals/helpers/promise.js\";\nimport { Serializer } from \"@/serializer/serializer.js\";\n\nexport interface ChatModelParameters {\n maxTokens?: number;\n topP?: number;\n frequencyPenalty?: number;\n temperature?: number;\n topK?: number;\n n?: number;\n presencePenalty?: number;\n seed?: number;\n stopSequences?: string[];\n}\n\nexport interface ChatModelObjectInput<T> extends ChatModelParameters {\n schema: z.ZodSchema<T>;\n messages: Message[];\n abortSignal?: AbortSignal;\n maxRetries?: number;\n}\n\nexport interface ChatModelObjectOutput<T> {\n object: T;\n}\n\nexport interface ChatModelInput extends ChatModelParameters {\n tools?: AnyTool[];\n abortSignal?: AbortSignal;\n stopSequences?: string[];\n responseFormat?:\n | {\n type: \"regular\";\n tools?: (LanguageModelV1FunctionTool | LanguageModelV1ProviderDefinedTool)[];\n toolChoice?: LanguageModelV1ToolChoice;\n }\n | {\n type: \"object-json\";\n schema?: JSONSchema7;\n name?: string;\n description?: string;\n }\n | {\n type: \"object-tool\";\n tool: LanguageModelV1FunctionTool;\n };\n toolChoice?: never; // TODO\n messages: Message[];\n}\n\nexport type ChatModelFinishReason =\n | \"stop\"\n | \"length\"\n | \"content-filter\"\n | \"tool-calls\"\n | \"error\"\n | \"other\"\n | \"unknown\";\n\nexport interface ChatModelUsage {\n promptTokens: number;\n completionTokens: number;\n totalTokens: number;\n}\n\nexport interface ChatModelEvents {\n newToken?: Callback<{ value: ChatModelOutput; callbacks: { abort: () => void } }>;\n success?: Callback<{ value: ChatModelOutput }>;\n start?: Callback<{ input: ChatModelInput }>;\n error?: Callback<{ input: ChatModelInput; error: FrameworkError }>;\n finish?: Callback<null>;\n}\n\nexport type ChatModelEmitter<A = Record<never, never>> = Emitter<\n ChatModelEvents & Omit<A, keyof ChatModelEvents>\n>;\n\nexport type ChatModelCache = BaseCache<Task<ChatModelOutput[]>>;\nexport type ConfigFn<T> = (value: T) => T;\nexport interface ChatConfig {\n cache?: ChatModelCache | ConfigFn<ChatModelCache>;\n parameters?: ChatModelParameters | ConfigFn<ChatModelParameters>;\n}\n\nexport abstract class ChatModel extends Serializable {\n public abstract readonly emitter: Emitter<ChatModelEvents>;\n public cache: ChatModelCache = new NullCache();\n public parameters: ChatModelParameters = {};\n\n abstract get modelId(): string;\n abstract get providerId(): string;\n\n create(input: ChatModelInput & { stream?: boolean }) {\n input = shallowCopy(input);\n\n return RunContext.enter(\n this,\n { params: [input] as const, signal: input?.abortSignal },\n async (run) => {\n const cacheEntry = await this.createCacheAccessor(input);\n\n try {\n await run.emitter.emit(\"start\", { input });\n const chunks: ChatModelOutput[] = [];\n\n const generator =\n cacheEntry.value ??\n (input.stream\n ? this._createStream(input, run)\n : toAsyncGenerator(this._create(input, run)));\n\n const controller = new AbortController();\n for await (const value of generator) {\n chunks.push(value);\n await run.emitter.emit(\"newToken\", {\n value,\n callbacks: { abort: () => controller.abort() },\n });\n if (controller.signal.aborted) {\n break;\n }\n }\n\n cacheEntry.resolve(chunks);\n const result = ChatModelOutput.fromChunks(chunks);\n await run.emitter.emit(\"success\", { value: result });\n return result;\n } catch (error) {\n await run.emitter.emit(\"error\", { input, error });\n await cacheEntry.reject(error);\n if (error instanceof ChatModelError) {\n throw error;\n } else {\n throw new ChatModelError(`The Chat Model has encountered an error.`, [error]);\n }\n } finally {\n await run.emitter.emit(\"finish\", null);\n }\n },\n );\n }\n\n createStructure<T>(input: ChatModelObjectInput<T>) {\n return RunContext.enter(\n this,\n { params: [input] as const, signal: input?.abortSignal },\n async (run) => {\n return await this._createStructure<T>(input, run);\n },\n );\n }\n\n config({ cache, parameters }: ChatConfig): void {\n if (cache) {\n this.cache = isFunction(cache) ? cache(this.cache) : cache;\n }\n if (parameters) {\n this.parameters = isFunction(parameters) ? parameters(this.parameters) : parameters;\n }\n }\n\n static async fromName(name: FullModelName | ProviderName, options?: ChatModelParameters) {\n const { providerId, modelId } = parseModel(name);\n const Target = await loadModel<ChatModel>(providerId, \"chat\");\n return new Target(modelId || undefined, options);\n }\n\n protected abstract _create(\n input: ChatModelInput,\n run: GetRunContext<typeof this>,\n ): Promise<ChatModelOutput>;\n protected abstract _createStream(\n input: ChatModelInput,\n run: GetRunContext<typeof this>,\n ): AsyncGenerator<ChatModelOutput, void>;\n\n protected async _createStructure<T>(\n input: ChatModelObjectInput<T>,\n run: GetRunContext<typeof this>,\n ): Promise<ChatModelObjectOutput<T>> {\n const { schema, ...options } = input;\n const jsonSchema = toJsonSchema(schema);\n\n const systemTemplate = new PromptTemplate({\n schema: z.object({\n schema: z.string().min(1),\n }),\n template: `You are a helpful assistant that generates only valid JSON adhering to the following JSON Schema.\n\n\\`\\`\\`\n{{schema}}\n\\`\\`\\`\n\nIMPORTANT: You MUST answer with a JSON object that matches the JSON schema above.`,\n });\n\n const messages: Message[] = [\n new SystemMessage(systemTemplate.render({ schema: JSON.stringify(jsonSchema, null, 2) })),\n ...input.messages,\n ];\n\n const errorTemplate = new PromptTemplate({\n schema: z.object({\n errors: z.string(),\n expected: z.string(),\n received: z.string(),\n }),\n template: `Generated object does not match the expected JSON schema!\n\nValidation Errors: {{errors}}`,\n });\n\n return new Retryable<ChatModelObjectOutput<T>>({\n executor: async () => {\n const response = await this._create(\n {\n ...options,\n messages,\n responseFormat: { type: \"object-json\" },\n },\n run,\n );\n\n const textResponse = response.getTextContent();\n const object: T = parseBrokenJson(textResponse, { pair: [\"{\", \"}\"] });\n const validator = createSchemaValidator(schema) as ValidateFunction<T>;\n\n const success = validator(object);\n if (!success) {\n const context = {\n expected: JSON.stringify(jsonSchema),\n received: textResponse,\n errors: JSON.stringify(validator.errors ?? []),\n };\n\n messages.push(new UserMessage(errorTemplate.render(context)));\n throw new ChatModelError(`LLM did not produce a valid output.`, [], {\n context,\n });\n }\n\n return { object };\n },\n config: {\n signal: run.signal,\n maxRetries: input?.maxRetries || 1,\n },\n }).get();\n }\n\n createSnapshot() {\n return { cache: this.cache, emitter: this.emitter, parameters: shallowCopy(this.parameters) };\n }\n\n destroy() {\n this.emitter.destroy();\n }\n\n protected async createCacheAccessor({\n abortSignal: _,\n messages,\n tools = [],\n ...input\n }: ChatModelInput) {\n const key = ObjectHashKeyFn({\n ...input,\n messages: await Serializer.serialize(messages.map((msg) => msg.toPlain())),\n tools: await Serializer.serialize(tools),\n });\n const value = await this.cache.get(key);\n const isNew = value === undefined;\n\n let task: Task<ChatModelOutput[]> | null = null;\n if (isNew) {\n task = new Task();\n await this.cache.set(key, task);\n }\n\n return {\n key,\n value,\n resolve: <T2 extends ChatModelOutput>(value: T2[]) => {\n task?.resolve?.(value);\n },\n reject: async (error: Error) => {\n task?.reject?.(error);\n if (isNew) {\n await this.cache.delete(key);\n }\n },\n };\n }\n}\n\nexport class ChatModelOutput extends Serializable {\n constructor(\n public readonly messages: Message[],\n public usage?: ChatModelUsage,\n public finishReason?: ChatModelFinishReason,\n ) {\n super();\n }\n\n static fromChunks(chunks: ChatModelOutput[]) {\n const final = new ChatModelOutput([]);\n chunks.forEach((cur) => final.merge(cur));\n return final;\n }\n\n merge(other: ChatModelOutput) {\n this.messages.push(...other.messages);\n this.finishReason = other.finishReason;\n if (this.usage && other.usage) {\n this.usage = customMerge([this.usage, other.usage], {\n totalTokens: takeBigger,\n promptTokens: takeBigger,\n completionTokens: takeBigger,\n });\n } else if (other.usage) {\n this.usage = shallowCopy(other.usage);\n }\n }\n\n getToolCalls() {\n return this.messages\n .filter((r) => r instanceof AssistantMessage)\n .flatMap((r) => r.getToolCalls())\n .filter(Boolean);\n }\n\n getTextMessages(): AssistantMessage[] {\n return this.messages.filter((r) => r instanceof AssistantMessage).filter((r) => r.text);\n }\n\n getTextContent(): string {\n return this.messages\n .filter((r) => r instanceof AssistantMessage)\n .flatMap((r) => r.text)\n .filter(Boolean)\n .join(\"\");\n }\n\n toString() {\n return this.getTextContent();\n }\n\n createSnapshot() {\n return {\n messages: shallowCopy(this.messages),\n usage: shallowCopy(this.usage),\n finishReason: this.finishReason,\n };\n }\n\n loadSnapshot(snapshot: ReturnType<typeof this.createSnapshot>) {\n Object.assign(this, snapshot);\n }\n}\n"]}
|
package/dist/backend/chat.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { Message, AssistantMessage, MessageContentPart } from './message.cjs';
|
|
|
2
2
|
import * as ai from 'ai';
|
|
3
3
|
import { Run, GetRunContext } from '../context.cjs';
|
|
4
4
|
import { Serializable } from '../internals/serializable.cjs';
|
|
5
|
-
import {
|
|
5
|
+
import { C as Callback, E as Emitter } from '../emitter-C9EN5B0j.cjs';
|
|
6
6
|
import { FrameworkError } from '../errors.cjs';
|
|
7
7
|
import { Task } from 'promise-based-task';
|
|
8
8
|
import { BaseCache } from '../cache/base.cjs';
|
|
@@ -11,9 +11,9 @@ import { ProviderName } from './constants.cjs';
|
|
|
11
11
|
import { AnyTool } from '../tools/base.cjs';
|
|
12
12
|
import { LanguageModelV1FunctionTool, LanguageModelV1ProviderDefinedTool, LanguageModelV1ToolChoice, JSONSchema7 } from '@ai-sdk/provider';
|
|
13
13
|
import { z } from 'zod';
|
|
14
|
+
import '../internals/helpers/promise.cjs';
|
|
14
15
|
import '../internals/types.cjs';
|
|
15
16
|
import '../internals/helpers/guards.cjs';
|
|
16
|
-
import '../internals/helpers/promise.cjs';
|
|
17
17
|
import 'ajv';
|
|
18
18
|
import '../internals/helpers/schema.cjs';
|
|
19
19
|
import 'zod-to-json-schema';
|
package/dist/backend/chat.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Message, AssistantMessage, MessageContentPart } from './message.js';
|
|
|
2
2
|
import * as ai from 'ai';
|
|
3
3
|
import { Run, GetRunContext } from '../context.js';
|
|
4
4
|
import { Serializable } from '../internals/serializable.js';
|
|
5
|
-
import {
|
|
5
|
+
import { C as Callback, E as Emitter } from '../emitter-BxVxGBbJ.js';
|
|
6
6
|
import { FrameworkError } from '../errors.js';
|
|
7
7
|
import { Task } from 'promise-based-task';
|
|
8
8
|
import { BaseCache } from '../cache/base.js';
|
|
@@ -11,9 +11,9 @@ import { ProviderName } from './constants.js';
|
|
|
11
11
|
import { AnyTool } from '../tools/base.js';
|
|
12
12
|
import { LanguageModelV1FunctionTool, LanguageModelV1ProviderDefinedTool, LanguageModelV1ToolChoice, JSONSchema7 } from '@ai-sdk/provider';
|
|
13
13
|
import { z } from 'zod';
|
|
14
|
+
import '../internals/helpers/promise.js';
|
|
14
15
|
import '../internals/types.js';
|
|
15
16
|
import '../internals/helpers/guards.js';
|
|
16
|
-
import '../internals/helpers/promise.js';
|
|
17
17
|
import 'ajv';
|
|
18
18
|
import '../internals/helpers/schema.js';
|
|
19
19
|
import 'zod-to-json-schema';
|
package/dist/backend/chat.js
CHANGED
|
@@ -3,9 +3,7 @@ import { shallowCopy } from '../serializer/utils.js';
|
|
|
3
3
|
import { customMerge } from '../internals/helpers/object.js';
|
|
4
4
|
import { takeBigger } from '../internals/helpers/number.js';
|
|
5
5
|
import { RunContext } from '../context.js';
|
|
6
|
-
import {
|
|
7
|
-
import { createTelemetryMiddleware } from '../instrumentation/create-telemetry-middleware.js';
|
|
8
|
-
import { doNothing, isFunction } from 'remeda';
|
|
6
|
+
import { isFunction } from 'remeda';
|
|
9
7
|
import { ObjectHashKeyFn } from '../cache/decoratorCache.js';
|
|
10
8
|
import { Task } from 'promise-based-task';
|
|
11
9
|
import { NullCache } from '../cache/nullCache.js';
|
|
@@ -77,7 +75,7 @@ class ChatModel extends Serializable {
|
|
|
77
75
|
} finally {
|
|
78
76
|
await run.emitter.emit("finish", null);
|
|
79
77
|
}
|
|
80
|
-
})
|
|
78
|
+
});
|
|
81
79
|
}
|
|
82
80
|
createStructure(input) {
|
|
83
81
|
return RunContext.enter(this, {
|