@aigne/core 1.0.17 → 1.1.0-beta.2
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/lib/cjs/agents/agent.d.ts +55 -0
- package/lib/cjs/agents/agent.js +91 -0
- package/lib/cjs/agents/ai-agent.d.ts +20 -0
- package/lib/cjs/agents/ai-agent.js +86 -0
- package/lib/cjs/agents/mcp-agent.d.ts +27 -0
- package/lib/cjs/agents/mcp-agent.js +95 -0
- package/lib/cjs/agents/types.d.ts +9 -0
- package/lib/cjs/agents/types.js +16 -0
- package/lib/cjs/execution-engine/context.d.ts +10 -0
- package/lib/cjs/execution-engine/index.d.ts +45 -0
- package/lib/cjs/execution-engine/index.js +190 -0
- package/lib/cjs/execution-engine/message-queue.d.ts +5 -0
- package/lib/cjs/execution-engine/message-queue.js +9 -0
- package/lib/cjs/index.d.ts +10 -21
- package/lib/cjs/index.js +10 -21
- package/lib/cjs/models/chat-openai.d.ts +14 -0
- package/lib/cjs/{llm-models/openai-llm-model.js → models/chat-openai.js} +62 -27
- package/lib/cjs/models/chat.d.ts +78 -0
- package/lib/cjs/models/chat.js +91 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +34 -0
- package/lib/cjs/prompt/prompt-builder.js +140 -0
- package/lib/cjs/prompt/template.d.ts +70 -0
- package/lib/cjs/prompt/template.js +172 -0
- package/lib/cjs/utils/logger.d.ts +6 -3
- package/lib/cjs/utils/logger.js +9 -14
- package/lib/cjs/utils/type-utils.d.ts +5 -1
- package/lib/cjs/utils/type-utils.js +23 -0
- package/lib/dts/agents/agent.d.ts +55 -0
- package/lib/dts/agents/ai-agent.d.ts +20 -0
- package/lib/dts/agents/mcp-agent.d.ts +27 -0
- package/lib/dts/agents/types.d.ts +9 -0
- package/lib/dts/execution-engine/context.d.ts +10 -0
- package/lib/dts/execution-engine/index.d.ts +45 -0
- package/lib/dts/execution-engine/message-queue.d.ts +5 -0
- package/lib/dts/index.d.ts +10 -21
- package/lib/dts/models/chat-openai.d.ts +14 -0
- package/lib/dts/models/chat.d.ts +78 -0
- package/lib/dts/prompt/prompt-builder.d.ts +34 -0
- package/lib/dts/prompt/template.d.ts +70 -0
- package/lib/dts/utils/logger.d.ts +6 -3
- package/lib/dts/utils/type-utils.d.ts +5 -1
- package/lib/esm/agents/agent.d.ts +55 -0
- package/lib/esm/agents/agent.js +83 -0
- package/lib/esm/agents/ai-agent.d.ts +20 -0
- package/lib/esm/agents/ai-agent.js +82 -0
- package/lib/esm/agents/mcp-agent.d.ts +27 -0
- package/lib/esm/agents/mcp-agent.js +90 -0
- package/lib/esm/agents/types.d.ts +9 -0
- package/lib/esm/agents/types.js +11 -0
- package/lib/esm/execution-engine/context.d.ts +10 -0
- package/lib/esm/execution-engine/index.d.ts +45 -0
- package/lib/esm/execution-engine/index.js +182 -0
- package/lib/esm/execution-engine/message-queue.d.ts +5 -0
- package/lib/esm/execution-engine/message-queue.js +5 -0
- package/lib/esm/index.d.ts +10 -21
- package/lib/esm/index.js +10 -21
- package/lib/esm/models/chat-openai.d.ts +14 -0
- package/lib/esm/{llm-models/openai-llm-model.js → models/chat-openai.js} +59 -24
- package/lib/esm/models/chat.d.ts +78 -0
- package/lib/esm/models/chat.js +87 -0
- package/lib/esm/prompt/prompt-builder.d.ts +34 -0
- package/lib/esm/prompt/prompt-builder.js +131 -0
- package/lib/esm/prompt/template.d.ts +70 -0
- package/lib/esm/prompt/template.js +158 -0
- package/lib/esm/utils/logger.d.ts +6 -3
- package/lib/esm/utils/logger.js +6 -14
- package/lib/esm/utils/type-utils.d.ts +5 -1
- package/lib/esm/utils/type-utils.js +17 -1
- package/package.json +18 -14
- package/lib/cjs/agent.d.ts +0 -96
- package/lib/cjs/agent.js +0 -165
- package/lib/cjs/constants.d.ts +0 -7
- package/lib/cjs/constants.js +0 -10
- package/lib/cjs/context.d.ts +0 -23
- package/lib/cjs/definitions/data-schema.d.ts +0 -61
- package/lib/cjs/definitions/data-schema.js +0 -47
- package/lib/cjs/definitions/data-type.d.ts +0 -32
- package/lib/cjs/definitions/data-type.js +0 -2
- package/lib/cjs/definitions/memory.d.ts +0 -53
- package/lib/cjs/definitions/memory.js +0 -23
- package/lib/cjs/definitions/open-api.d.ts +0 -36
- package/lib/cjs/definitions/open-api.js +0 -2
- package/lib/cjs/definitions/preload.d.ts +0 -43
- package/lib/cjs/definitions/preload.js +0 -34
- package/lib/cjs/function-agent.d.ts +0 -23
- package/lib/cjs/function-agent.js +0 -62
- package/lib/cjs/llm-agent.d.ts +0 -53
- package/lib/cjs/llm-agent.js +0 -140
- package/lib/cjs/llm-decision-agent.d.ts +0 -53
- package/lib/cjs/llm-decision-agent.js +0 -144
- package/lib/cjs/llm-model.d.ts +0 -80
- package/lib/cjs/llm-model.js +0 -27
- package/lib/cjs/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/cjs/llm-models/gemini-llm-model.js +0 -199
- package/lib/cjs/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/cjs/memorable.d.ts +0 -198
- package/lib/cjs/memorable.js +0 -33
- package/lib/cjs/open-api-agent.d.ts +0 -26
- package/lib/cjs/open-api-agent.js +0 -68
- package/lib/cjs/pipeline-agent.d.ts +0 -56
- package/lib/cjs/pipeline-agent.js +0 -206
- package/lib/cjs/runnable.d.ts +0 -76
- package/lib/cjs/runnable.js +0 -37
- package/lib/cjs/runtime.d.ts +0 -64
- package/lib/cjs/runtime.js +0 -149
- package/lib/cjs/sandbox-function-agent.d.ts +0 -21
- package/lib/cjs/sandbox-function-agent.js +0 -78
- package/lib/cjs/sandbox-function-runner.d.ts +0 -16
- package/lib/cjs/sandbox-function-runner.js +0 -32
- package/lib/cjs/utils/constants.d.ts +0 -1
- package/lib/cjs/utils/constants.js +0 -4
- package/lib/cjs/utils/fetch-open-api.d.ts +0 -2
- package/lib/cjs/utils/fetch-open-api.js +0 -31
- package/lib/cjs/utils/fetch.d.ts +0 -1
- package/lib/cjs/utils/fetch.js +0 -21
- package/lib/cjs/utils/index.d.ts +0 -12
- package/lib/cjs/utils/index.js +0 -28
- package/lib/cjs/utils/is-non-nullable.d.ts +0 -2
- package/lib/cjs/utils/is-non-nullable.js +0 -20
- package/lib/cjs/utils/message-utils.d.ts +0 -20
- package/lib/cjs/utils/message-utils.js +0 -79
- package/lib/cjs/utils/mustache-utils.d.ts +0 -3
- package/lib/cjs/utils/mustache-utils.js +0 -14
- package/lib/cjs/utils/nullable.d.ts +0 -7
- package/lib/cjs/utils/nullable.js +0 -2
- package/lib/cjs/utils/omit.d.ts +0 -1
- package/lib/cjs/utils/omit.js +0 -2
- package/lib/cjs/utils/open-api-parameter.d.ts +0 -7
- package/lib/cjs/utils/open-api-parameter.js +0 -81
- package/lib/cjs/utils/ordered-map.d.ts +0 -37
- package/lib/cjs/utils/ordered-map.js +0 -103
- package/lib/cjs/utils/partial.d.ts +0 -3
- package/lib/cjs/utils/partial.js +0 -2
- package/lib/cjs/utils/runnable-type.d.ts +0 -5
- package/lib/cjs/utils/runnable-type.js +0 -2
- package/lib/cjs/utils/stream-utils.d.ts +0 -20
- package/lib/cjs/utils/stream-utils.js +0 -86
- package/lib/cjs/utils/structured-output-schema.d.ts +0 -3
- package/lib/cjs/utils/structured-output-schema.js +0 -43
- package/lib/cjs/utils/union.d.ts +0 -1
- package/lib/cjs/utils/union.js +0 -2
- package/lib/dts/agent.d.ts +0 -96
- package/lib/dts/constants.d.ts +0 -7
- package/lib/dts/context.d.ts +0 -23
- package/lib/dts/definitions/data-schema.d.ts +0 -61
- package/lib/dts/definitions/data-type.d.ts +0 -32
- package/lib/dts/definitions/memory.d.ts +0 -53
- package/lib/dts/definitions/open-api.d.ts +0 -36
- package/lib/dts/definitions/preload.d.ts +0 -43
- package/lib/dts/function-agent.d.ts +0 -23
- package/lib/dts/llm-agent.d.ts +0 -53
- package/lib/dts/llm-decision-agent.d.ts +0 -53
- package/lib/dts/llm-model.d.ts +0 -80
- package/lib/dts/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/dts/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/dts/memorable.d.ts +0 -198
- package/lib/dts/open-api-agent.d.ts +0 -26
- package/lib/dts/pipeline-agent.d.ts +0 -56
- package/lib/dts/runnable.d.ts +0 -76
- package/lib/dts/runtime.d.ts +0 -64
- package/lib/dts/sandbox-function-agent.d.ts +0 -21
- package/lib/dts/sandbox-function-runner.d.ts +0 -16
- package/lib/dts/utils/constants.d.ts +0 -1
- package/lib/dts/utils/fetch-open-api.d.ts +0 -2
- package/lib/dts/utils/fetch.d.ts +0 -1
- package/lib/dts/utils/index.d.ts +0 -12
- package/lib/dts/utils/is-non-nullable.d.ts +0 -2
- package/lib/dts/utils/message-utils.d.ts +0 -20
- package/lib/dts/utils/mustache-utils.d.ts +0 -3
- package/lib/dts/utils/nullable.d.ts +0 -7
- package/lib/dts/utils/omit.d.ts +0 -1
- package/lib/dts/utils/open-api-parameter.d.ts +0 -7
- package/lib/dts/utils/ordered-map.d.ts +0 -37
- package/lib/dts/utils/partial.d.ts +0 -3
- package/lib/dts/utils/runnable-type.d.ts +0 -5
- package/lib/dts/utils/stream-utils.d.ts +0 -20
- package/lib/dts/utils/structured-output-schema.d.ts +0 -3
- package/lib/dts/utils/union.d.ts +0 -1
- package/lib/esm/agent.d.ts +0 -96
- package/lib/esm/agent.js +0 -161
- package/lib/esm/constants.d.ts +0 -7
- package/lib/esm/constants.js +0 -7
- package/lib/esm/context.d.ts +0 -23
- package/lib/esm/definitions/data-schema.d.ts +0 -61
- package/lib/esm/definitions/data-schema.js +0 -44
- package/lib/esm/definitions/data-type.d.ts +0 -32
- package/lib/esm/definitions/data-type.js +0 -1
- package/lib/esm/definitions/memory.d.ts +0 -53
- package/lib/esm/definitions/memory.js +0 -20
- package/lib/esm/definitions/open-api.d.ts +0 -36
- package/lib/esm/definitions/open-api.js +0 -1
- package/lib/esm/definitions/preload.d.ts +0 -43
- package/lib/esm/definitions/preload.js +0 -31
- package/lib/esm/function-agent.d.ts +0 -23
- package/lib/esm/function-agent.js +0 -59
- package/lib/esm/llm-agent.d.ts +0 -53
- package/lib/esm/llm-agent.js +0 -137
- package/lib/esm/llm-decision-agent.d.ts +0 -53
- package/lib/esm/llm-decision-agent.js +0 -141
- package/lib/esm/llm-model.d.ts +0 -80
- package/lib/esm/llm-model.js +0 -23
- package/lib/esm/llm-models/gemini-llm-model.d.ts +0 -24
- package/lib/esm/llm-models/gemini-llm-model.js +0 -195
- package/lib/esm/llm-models/openai-llm-model.d.ts +0 -26
- package/lib/esm/memorable.d.ts +0 -198
- package/lib/esm/memorable.js +0 -28
- package/lib/esm/open-api-agent.d.ts +0 -26
- package/lib/esm/open-api-agent.js +0 -65
- package/lib/esm/pipeline-agent.d.ts +0 -56
- package/lib/esm/pipeline-agent.js +0 -203
- package/lib/esm/runnable.d.ts +0 -76
- package/lib/esm/runnable.js +0 -30
- package/lib/esm/runtime.d.ts +0 -64
- package/lib/esm/runtime.js +0 -146
- package/lib/esm/sandbox-function-agent.d.ts +0 -21
- package/lib/esm/sandbox-function-agent.js +0 -74
- package/lib/esm/sandbox-function-runner.d.ts +0 -16
- package/lib/esm/sandbox-function-runner.js +0 -28
- package/lib/esm/utils/constants.d.ts +0 -1
- package/lib/esm/utils/constants.js +0 -1
- package/lib/esm/utils/fetch-open-api.d.ts +0 -2
- package/lib/esm/utils/fetch-open-api.js +0 -28
- package/lib/esm/utils/fetch.d.ts +0 -1
- package/lib/esm/utils/fetch.js +0 -18
- package/lib/esm/utils/index.d.ts +0 -12
- package/lib/esm/utils/index.js +0 -12
- package/lib/esm/utils/is-non-nullable.d.ts +0 -2
- package/lib/esm/utils/is-non-nullable.js +0 -13
- package/lib/esm/utils/message-utils.d.ts +0 -20
- package/lib/esm/utils/message-utils.js +0 -71
- package/lib/esm/utils/mustache-utils.d.ts +0 -3
- package/lib/esm/utils/mustache-utils.js +0 -8
- package/lib/esm/utils/nullable.d.ts +0 -7
- package/lib/esm/utils/nullable.js +0 -1
- package/lib/esm/utils/omit.d.ts +0 -1
- package/lib/esm/utils/omit.js +0 -1
- package/lib/esm/utils/open-api-parameter.d.ts +0 -7
- package/lib/esm/utils/open-api-parameter.js +0 -78
- package/lib/esm/utils/ordered-map.d.ts +0 -37
- package/lib/esm/utils/ordered-map.js +0 -100
- package/lib/esm/utils/partial.d.ts +0 -3
- package/lib/esm/utils/partial.js +0 -1
- package/lib/esm/utils/runnable-type.d.ts +0 -5
- package/lib/esm/utils/runnable-type.js +0 -1
- package/lib/esm/utils/stream-utils.d.ts +0 -20
- package/lib/esm/utils/stream-utils.js +0 -79
- package/lib/esm/utils/structured-output-schema.d.ts +0 -3
- package/lib/esm/utils/structured-output-schema.js +0 -37
- package/lib/esm/utils/union.d.ts +0 -1
- package/lib/esm/utils/union.js +0 -1
- /package/lib/cjs/{context.js → execution-engine/context.js} +0 -0
- /package/lib/esm/{context.js → execution-engine/context.js} +0 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import Mustache from "mustache";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export class PromptTemplate {
|
|
4
|
+
template;
|
|
5
|
+
static from(template) {
|
|
6
|
+
return new PromptTemplate(template);
|
|
7
|
+
}
|
|
8
|
+
constructor(template) {
|
|
9
|
+
this.template = template;
|
|
10
|
+
}
|
|
11
|
+
format(variables) {
|
|
12
|
+
return Mustache.render(this.template, variables, undefined, {
|
|
13
|
+
escape: (v) => {
|
|
14
|
+
return typeof v === "object" ? JSON.stringify(v) : v;
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class ChatMessageTemplate {
|
|
20
|
+
role;
|
|
21
|
+
content;
|
|
22
|
+
name;
|
|
23
|
+
constructor(role, content, name) {
|
|
24
|
+
this.role = role;
|
|
25
|
+
this.content = content;
|
|
26
|
+
this.name = name;
|
|
27
|
+
}
|
|
28
|
+
format(variables) {
|
|
29
|
+
let { content } = this;
|
|
30
|
+
if (Array.isArray(content)) {
|
|
31
|
+
content = content.map((i) => {
|
|
32
|
+
if (i.type === "text")
|
|
33
|
+
return { ...i, text: PromptTemplate.from(i.text).format(variables) };
|
|
34
|
+
return i;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if (typeof content === "string") {
|
|
38
|
+
content = PromptTemplate.from(content).format(variables);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
role: this.role,
|
|
42
|
+
content,
|
|
43
|
+
name: this.name,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class SystemMessageTemplate extends ChatMessageTemplate {
|
|
48
|
+
static from(content, name) {
|
|
49
|
+
return new SystemMessageTemplate("system", content, name);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export class UserMessageTemplate extends ChatMessageTemplate {
|
|
53
|
+
static from(template, name) {
|
|
54
|
+
return new UserMessageTemplate("user", template, name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export class AgentMessageTemplate extends ChatMessageTemplate {
|
|
58
|
+
toolCalls;
|
|
59
|
+
static from(template, name) {
|
|
60
|
+
return typeof template === "string"
|
|
61
|
+
? new AgentMessageTemplate(template, undefined, name)
|
|
62
|
+
: new AgentMessageTemplate(undefined, template, name);
|
|
63
|
+
}
|
|
64
|
+
constructor(content, toolCalls, name) {
|
|
65
|
+
super("agent", content, name);
|
|
66
|
+
this.toolCalls = toolCalls;
|
|
67
|
+
}
|
|
68
|
+
format(variables) {
|
|
69
|
+
return {
|
|
70
|
+
...super.format(variables),
|
|
71
|
+
toolCalls: this.toolCalls,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export class ToolMessageTemplate extends ChatMessageTemplate {
|
|
76
|
+
toolCallId;
|
|
77
|
+
static from(content, toolCallId, name) {
|
|
78
|
+
return new ToolMessageTemplate(content, toolCallId, name);
|
|
79
|
+
}
|
|
80
|
+
constructor(content, toolCallId, name) {
|
|
81
|
+
super("tool", typeof content === "string" ? content : JSON.stringify(content), name);
|
|
82
|
+
this.toolCallId = toolCallId;
|
|
83
|
+
}
|
|
84
|
+
format(variables) {
|
|
85
|
+
return {
|
|
86
|
+
...super.format(variables),
|
|
87
|
+
toolCallId: this.toolCallId,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export class ChatMessagesTemplate {
|
|
92
|
+
messages;
|
|
93
|
+
static from(messages) {
|
|
94
|
+
return new ChatMessagesTemplate(typeof messages === "string" ? [UserMessageTemplate.from(messages)] : messages);
|
|
95
|
+
}
|
|
96
|
+
constructor(messages) {
|
|
97
|
+
this.messages = messages;
|
|
98
|
+
}
|
|
99
|
+
format(variables) {
|
|
100
|
+
return this.messages.map((message) => message.format(variables));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const systemChatMessageSchema = z.object({
|
|
104
|
+
role: z.literal("system"),
|
|
105
|
+
content: z.string(),
|
|
106
|
+
name: z.string().optional(),
|
|
107
|
+
});
|
|
108
|
+
const userChatMessageSchema = z.object({
|
|
109
|
+
role: z.literal("user"),
|
|
110
|
+
content: z.string(),
|
|
111
|
+
name: z.string().optional(),
|
|
112
|
+
});
|
|
113
|
+
const chatModelOutputToolCallSchema = z.object({
|
|
114
|
+
id: z.string(),
|
|
115
|
+
type: z.literal("function"),
|
|
116
|
+
function: z.object({
|
|
117
|
+
name: z.string(),
|
|
118
|
+
arguments: z.record(z.unknown()),
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
const agentChatMessageSchema = z.object({
|
|
122
|
+
role: z.literal("agent"),
|
|
123
|
+
content: z.string().optional(),
|
|
124
|
+
toolCalls: z.array(chatModelOutputToolCallSchema).optional(),
|
|
125
|
+
name: z.string().optional(),
|
|
126
|
+
});
|
|
127
|
+
const toolChatMessageSchema = z.object({
|
|
128
|
+
role: z.literal("tool"),
|
|
129
|
+
content: z
|
|
130
|
+
.union([z.string(), z.record(z.unknown())])
|
|
131
|
+
.transform((val) => (typeof val !== "string" ? JSON.stringify(val) : val)),
|
|
132
|
+
toolCallId: z.string(),
|
|
133
|
+
name: z.string().optional(),
|
|
134
|
+
});
|
|
135
|
+
const chatMessageSchema = z.union([
|
|
136
|
+
systemChatMessageSchema,
|
|
137
|
+
userChatMessageSchema,
|
|
138
|
+
agentChatMessageSchema,
|
|
139
|
+
toolChatMessageSchema,
|
|
140
|
+
]);
|
|
141
|
+
const chatMessagesSchema = z.array(chatMessageSchema);
|
|
142
|
+
export function parseChatMessages(messages) {
|
|
143
|
+
const result = chatMessagesSchema.safeParse(messages);
|
|
144
|
+
if (!result.success)
|
|
145
|
+
return undefined;
|
|
146
|
+
return result.data.map((message) => {
|
|
147
|
+
switch (message.role) {
|
|
148
|
+
case "system":
|
|
149
|
+
return SystemMessageTemplate.from(message.content, message.name);
|
|
150
|
+
case "user":
|
|
151
|
+
return UserMessageTemplate.from(message.content, message.name);
|
|
152
|
+
case "agent":
|
|
153
|
+
return new AgentMessageTemplate(message.content, message.toolCalls, message.name);
|
|
154
|
+
case "tool":
|
|
155
|
+
return ToolMessageTemplate.from(message.content, message.toolCallId, message.name);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const logger:
|
|
3
|
-
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
export declare const logger: {
|
|
3
|
+
base: debug.Debugger;
|
|
4
|
+
debug: debug.Debugger;
|
|
5
|
+
error: debug.Debugger;
|
|
6
|
+
};
|
package/lib/esm/utils/logger.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
info: level === "debug" || level === "info" ? logger.info : () => { },
|
|
8
|
-
warn: level === "debug" || level === "info" || level === "warn"
|
|
9
|
-
? logger.warn
|
|
10
|
-
: () => { },
|
|
11
|
-
error: logger.error,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
export const logger = createLogger(console);
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
export const logger = {
|
|
3
|
+
base: debug("@aigne/core"),
|
|
4
|
+
debug: debug("@aigne/core:debug"),
|
|
5
|
+
error: debug("@aigne/core:error"),
|
|
6
|
+
};
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
|
|
2
|
+
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
3
|
+
export declare function get(obj: unknown, path: string | string[], type?: undefined): unknown | undefined;
|
|
4
|
+
export declare function get(obj: unknown, path: string | string[], type: "string"): string | undefined;
|
|
5
|
+
export declare function get(obj: unknown, path: string | string[], type: "number"): number | undefined;
|
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { get as _get } from "lodash";
|
|
2
|
+
import isNil from "lodash/isNil";
|
|
3
|
+
export function isNonNullable(value) {
|
|
4
|
+
return !isNil(value);
|
|
5
|
+
}
|
|
6
|
+
export function orArrayToArray(value) {
|
|
7
|
+
if (isNil(value))
|
|
8
|
+
return [];
|
|
9
|
+
return Array.isArray(value) ? value : [value];
|
|
10
|
+
}
|
|
11
|
+
export function get(obj, path, type) {
|
|
12
|
+
const v = _get(obj, path);
|
|
13
|
+
if (type === "string" && typeof v === "string")
|
|
14
|
+
return v;
|
|
15
|
+
if (type === "number" && typeof v === "number")
|
|
16
|
+
return v;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.1.0-beta.2",
|
|
4
4
|
"description": "AIGNE core library",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -24,32 +24,36 @@
|
|
|
24
24
|
"url": "git+https://github.com/blocklet/ai-studio.git"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@google/generative-ai": "^0.
|
|
27
|
+
"@google/generative-ai": "^0.24.0",
|
|
28
|
+
"@modelcontextprotocol/sdk": "^1.6.1",
|
|
29
|
+
"@n8n/json-schema-to-zod": "^1.1.0",
|
|
30
|
+
"@types/debug": "^4.1.12",
|
|
28
31
|
"@types/mustache": "^4.2.5",
|
|
32
|
+
"debug": "^4.4.0",
|
|
29
33
|
"lodash": "^4.17.21",
|
|
30
34
|
"mustache": "^4.2.0",
|
|
31
|
-
"nanoid": "^5.
|
|
32
|
-
"openai": "^4.
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
+
"nanoid": "^5.1.3",
|
|
36
|
+
"openai": "^4.86.2",
|
|
37
|
+
"zod": "^3.24.2",
|
|
38
|
+
"zod-to-json-schema": "^3.24.3"
|
|
35
39
|
},
|
|
36
40
|
"peerDependencies": {
|
|
37
41
|
"@google/generative-ai": "^0.21.0",
|
|
38
42
|
"openai": "^4.79.1"
|
|
39
43
|
},
|
|
40
44
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@types/
|
|
43
|
-
"@types/lodash": "^4.17.14",
|
|
44
|
-
"core-js": "^3.40.0",
|
|
45
|
+
"@types/bun": "^1.2.4",
|
|
46
|
+
"@types/lodash": "^4.17.16",
|
|
45
47
|
"npm-run-all": "^4.1.5",
|
|
46
|
-
"reflect-metadata": "^0.2.2",
|
|
47
48
|
"rimraf": "^6.0.1",
|
|
48
|
-
"typescript": "^5.
|
|
49
|
+
"typescript": "^5.8.2"
|
|
49
50
|
},
|
|
50
51
|
"scripts": {
|
|
52
|
+
"lint": "biome check",
|
|
53
|
+
"lint:fix": "biome check --write",
|
|
51
54
|
"build": "tsc --build scripts/tsconfig.build.json",
|
|
52
|
-
"clean": "rimraf lib",
|
|
53
|
-
"test": "bun test"
|
|
55
|
+
"clean": "rimraf lib coverage",
|
|
56
|
+
"test": "bun test",
|
|
57
|
+
"test:coverage": "bun test --coverage --coverage-reporter lcov"
|
|
54
58
|
}
|
|
55
59
|
}
|
package/lib/cjs/agent.d.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { Context, ContextState } from "./context";
|
|
2
|
-
import type { DataSchema } from "./definitions/data-schema";
|
|
3
|
-
import type { AgentMemory, CreateRunnableMemory } from "./definitions/memory";
|
|
4
|
-
import type { AgentPreload, BindAgentInputs, BoundAgent, PreloadCreator } from "./definitions/preload";
|
|
5
|
-
import type { MemorableSearchOutput, MemoryItemWithScore, MemoryMessage } from "./memorable";
|
|
6
|
-
import { type RunOptions, Runnable, type RunnableDefinition, type RunnableInput, type RunnableOutput, type RunnableResponse, type RunnableResponseChunk, type RunnableResponseStream } from "./runnable";
|
|
7
|
-
import { OrderedRecord } from "./utils";
|
|
8
|
-
import type { ExtractRunnableOutputType } from "./utils/runnable-type";
|
|
9
|
-
export type AgentPreloads = Record<string, unknown>;
|
|
10
|
-
export type AgentMemories = Record<string, MemoryItemWithScore[]>;
|
|
11
|
-
export declare abstract class Agent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Runnable<I, O, State> {
|
|
12
|
-
definition: AgentDefinition;
|
|
13
|
-
context?: Context<State> | undefined;
|
|
14
|
-
constructor(definition: AgentDefinition, context?: Context<State> | undefined);
|
|
15
|
-
private getMemoryQuery;
|
|
16
|
-
/**
|
|
17
|
-
* Load memories that are defined in the agent definition.
|
|
18
|
-
* @param input The agent input.
|
|
19
|
-
* @param context The AIGNE context.
|
|
20
|
-
* @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
|
|
21
|
-
*/
|
|
22
|
-
protected loadMemories(input: I): Promise<Memories>;
|
|
23
|
-
/**
|
|
24
|
-
* Update memories by user messages and assistant responses.
|
|
25
|
-
* @param messages Messages to be added to memories.
|
|
26
|
-
*/
|
|
27
|
-
protected updateMemories(messages: MemoryMessage[]): Promise<void>;
|
|
28
|
-
protected loadPreloads(input: I): Promise<Preloads>;
|
|
29
|
-
run(input: I, options: RunOptions & {
|
|
30
|
-
stream: true;
|
|
31
|
-
}): Promise<RunnableResponseStream<O>>;
|
|
32
|
-
run(input: I, options?: RunOptions & {
|
|
33
|
-
stream?: false;
|
|
34
|
-
}): Promise<O>;
|
|
35
|
-
private addHistory;
|
|
36
|
-
/**
|
|
37
|
-
* Hook that is called before the agent result is returned.
|
|
38
|
-
* @param _result The agent result.
|
|
39
|
-
*/
|
|
40
|
-
protected onResult(_result: O): Promise<void>;
|
|
41
|
-
abstract process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions): Promise<RunnableResponse<O> | AsyncGenerator<RunnableResponseChunk<O>, void>> | AsyncGenerator<RunnableResponseChunk<O>, void>;
|
|
42
|
-
/**
|
|
43
|
-
* Bind some inputs to the agent, used for process of `PipelineAgent` or case of `LLMDecisionAgent`.
|
|
44
|
-
* @param options The bind options.
|
|
45
|
-
* @returns The bound agent.
|
|
46
|
-
*/
|
|
47
|
-
bind<Input extends BindAgentInputs<Record<string, never>, typeof this>>(options: {
|
|
48
|
-
description?: string;
|
|
49
|
-
input?: Input;
|
|
50
|
-
}): BoundAgent<Record<string, never>, typeof this, Readonly<Input>>;
|
|
51
|
-
}
|
|
52
|
-
export type AgentProcessOptions<Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> = {
|
|
53
|
-
preloads: Preloads;
|
|
54
|
-
memories: Memories;
|
|
55
|
-
};
|
|
56
|
-
export type AgentProcessInput<I extends RunnableInput, Preloads extends AgentPreloads, Memories extends AgentMemories> = I & Preloads & Memories;
|
|
57
|
-
export interface AgentDefinition extends RunnableDefinition {
|
|
58
|
-
preloads?: OrderedRecord<AgentPreload>;
|
|
59
|
-
memories?: OrderedRecord<AgentMemory>;
|
|
60
|
-
}
|
|
61
|
-
export type CreateAgentInputSchema = Record<string, DataSchema>;
|
|
62
|
-
export type CreateAgentOutputSchema<Extra extends Record<string, unknown> = Record<string, unknown>> = Record<string, DataSchema & Extra>;
|
|
63
|
-
export type CreateAgentPreloadsSchema<I extends CreateAgentInputSchema> = Record<string, PreloadCreator<I>>;
|
|
64
|
-
export type CreateAgentPreloadsType<I extends CreateAgentInputSchema, Preloads extends CreateAgentPreloadsSchema<I>> = {
|
|
65
|
-
[name in keyof Preloads]: ExtractRunnableOutputType<ReturnType<Preloads[name]>["runnable"]>;
|
|
66
|
-
};
|
|
67
|
-
export type CreateAgentMemoriesSchema<I extends CreateAgentInputSchema, Extras extends Record<string, unknown> = Record<string, unknown>> = Record<string, CreateRunnableMemory<I> & Extras>;
|
|
68
|
-
export type CreateAgentMemoriesType<I extends CreateAgentInputSchema, Memories extends CreateAgentMemoriesSchema<I, Record<string, unknown>>> = {
|
|
69
|
-
[key in keyof Memories]: MemorableSearchOutput<Memories[key]["memory"]>;
|
|
70
|
-
};
|
|
71
|
-
/**
|
|
72
|
-
* Common options to create Agent.
|
|
73
|
-
*/
|
|
74
|
-
export interface CreateAgentOptions<I extends CreateAgentInputSchema, O extends CreateAgentOutputSchema, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<I>, Memories extends CreateAgentMemoriesSchema<I, Record<string, unknown>>> {
|
|
75
|
-
context?: Context<State>;
|
|
76
|
-
/**
|
|
77
|
-
* Agent name, used to identify the agent.
|
|
78
|
-
*/
|
|
79
|
-
name?: string;
|
|
80
|
-
/**
|
|
81
|
-
* Input variables for this agent.
|
|
82
|
-
*/
|
|
83
|
-
inputs: I;
|
|
84
|
-
/**
|
|
85
|
-
* Output variables for this agent.
|
|
86
|
-
*/
|
|
87
|
-
outputs: O;
|
|
88
|
-
/**
|
|
89
|
-
* Preload runnables before running this agent. the preloaded data are available in the agent as input variables.
|
|
90
|
-
*/
|
|
91
|
-
preloads?: Preloads;
|
|
92
|
-
/**
|
|
93
|
-
* Memories to be used in this agent.
|
|
94
|
-
*/
|
|
95
|
-
memories?: Memories;
|
|
96
|
-
}
|
package/lib/cjs/agent.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Agent = void 0;
|
|
4
|
-
const runnable_1 = require("./runnable");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
const logger_1 = require("./utils/logger");
|
|
7
|
-
class Agent extends runnable_1.Runnable {
|
|
8
|
-
definition;
|
|
9
|
-
context;
|
|
10
|
-
constructor(definition, context) {
|
|
11
|
-
super(definition, context);
|
|
12
|
-
this.definition = definition;
|
|
13
|
-
this.context = context;
|
|
14
|
-
}
|
|
15
|
-
async getMemoryQuery(input, query) {
|
|
16
|
-
if (query?.from === "variable") {
|
|
17
|
-
const i = utils_1.OrderedRecord.find(this.definition.inputs, (i) => i.id === query.fromVariableId);
|
|
18
|
-
if (!i)
|
|
19
|
-
throw new Error(`Input variable ${query.fromVariableId} not found`);
|
|
20
|
-
const value = i.name ? input[i.name] : undefined;
|
|
21
|
-
return (0, utils_1.renderMessage)("{{value}}", { value });
|
|
22
|
-
}
|
|
23
|
-
return Object.entries(input)
|
|
24
|
-
.map(([key, value]) => `${key} ${value}`)
|
|
25
|
-
.join("\n");
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Load memories that are defined in the agent definition.
|
|
29
|
-
* @param input The agent input.
|
|
30
|
-
* @param context The AIGNE context.
|
|
31
|
-
* @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
|
|
32
|
-
*/
|
|
33
|
-
async loadMemories(input) {
|
|
34
|
-
const { definition: { memories }, context, } = this;
|
|
35
|
-
if (!memories?.$indexes.length)
|
|
36
|
-
return {};
|
|
37
|
-
if (!context)
|
|
38
|
-
throw new Error("Context is required");
|
|
39
|
-
const { userId, sessionId } = context.state;
|
|
40
|
-
return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ id, name, memory, query, options }) => {
|
|
41
|
-
if (!name || !memory)
|
|
42
|
-
return null;
|
|
43
|
-
const q = await this.getMemoryQuery(input, query);
|
|
44
|
-
const { results: memories } = await memory.search(q, {
|
|
45
|
-
...options,
|
|
46
|
-
userId,
|
|
47
|
-
sessionId,
|
|
48
|
-
});
|
|
49
|
-
return [
|
|
50
|
-
[id, memories],
|
|
51
|
-
[name, memories],
|
|
52
|
-
];
|
|
53
|
-
})))
|
|
54
|
-
.flat()
|
|
55
|
-
.filter(utils_1.isNonNullable));
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Update memories by user messages and assistant responses.
|
|
59
|
-
* @param messages Messages to be added to memories.
|
|
60
|
-
*/
|
|
61
|
-
async updateMemories(messages) {
|
|
62
|
-
const { context, definition: { memories }, } = this;
|
|
63
|
-
if (!context)
|
|
64
|
-
throw new Error("Context is required");
|
|
65
|
-
const { userId, sessionId } = context.state ?? {};
|
|
66
|
-
await Promise.all(utils_1.OrderedRecord.map(memories, async ({ memory }) => {
|
|
67
|
-
if (!memory) {
|
|
68
|
-
logger_1.logger.warn(`Memory is not defined in agent ${this.name || this.id}`);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
await memory.add(messages, { userId, sessionId });
|
|
72
|
-
}));
|
|
73
|
-
}
|
|
74
|
-
async loadPreloads(input) {
|
|
75
|
-
const { context, definition: { preloads }, } = this;
|
|
76
|
-
if (!preloads?.$indexes.length)
|
|
77
|
-
return {};
|
|
78
|
-
if (!context)
|
|
79
|
-
throw new Error("Context is required");
|
|
80
|
-
return Object.fromEntries(await Promise.all(utils_1.OrderedRecord.map(preloads, async (preload) => {
|
|
81
|
-
if (!preload.runnable?.id)
|
|
82
|
-
throw new Error("Runnable id in preload is required");
|
|
83
|
-
const runnable = await context.resolve(preload.runnable.id);
|
|
84
|
-
const runnableInput = Object.fromEntries(utils_1.OrderedRecord.map(runnable.definition.inputs, (i) => {
|
|
85
|
-
if (!i.name)
|
|
86
|
-
return null;
|
|
87
|
-
const bind = preload.input?.[i.id];
|
|
88
|
-
if (!bind)
|
|
89
|
-
return null;
|
|
90
|
-
if (bind.from !== "input")
|
|
91
|
-
throw new Error(`Unsupported bind from ${bind.from}`);
|
|
92
|
-
const from = this.definition.inputs[bind.fromInput];
|
|
93
|
-
if (!from)
|
|
94
|
-
throw new Error(`Input ${bind.fromInput} not found`);
|
|
95
|
-
if (!from.name)
|
|
96
|
-
return null;
|
|
97
|
-
const v = input[from.name];
|
|
98
|
-
return [i.name, v];
|
|
99
|
-
}).filter(utils_1.isNonNullable));
|
|
100
|
-
const result = await runnable.run(runnableInput);
|
|
101
|
-
return [preload.id, result];
|
|
102
|
-
})));
|
|
103
|
-
}
|
|
104
|
-
async run(input, options) {
|
|
105
|
-
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
106
|
-
input,
|
|
107
|
-
});
|
|
108
|
-
const preloads = await this.loadPreloads(input);
|
|
109
|
-
const memories = await this.loadMemories(input);
|
|
110
|
-
const processResult = await this.process({ ...input, ...preloads, ...memories }, { preloads, memories });
|
|
111
|
-
if (options?.stream) {
|
|
112
|
-
const stream = processResult instanceof ReadableStream ||
|
|
113
|
-
(0, utils_1.isAsyncGenerator)(processResult)
|
|
114
|
-
? processResult
|
|
115
|
-
: (0, utils_1.objectToRunnableResponseStream)(processResult);
|
|
116
|
-
return (0, utils_1.extractOutputsFromRunnableOutput)(stream, async (result) => {
|
|
117
|
-
// TODO: validate result against outputs schema
|
|
118
|
-
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
119
|
-
result,
|
|
120
|
-
});
|
|
121
|
-
await this.addHistory({ input, output: result });
|
|
122
|
-
await this.onResult(result);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
const result = processResult instanceof ReadableStream
|
|
126
|
-
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
127
|
-
: Symbol.asyncIterator in processResult
|
|
128
|
-
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
129
|
-
: processResult;
|
|
130
|
-
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
131
|
-
result,
|
|
132
|
-
});
|
|
133
|
-
// TODO: validate result against outputs schema
|
|
134
|
-
await this.addHistory({ input, output: result });
|
|
135
|
-
await this.onResult(result);
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
async addHistory({ input, output }) {
|
|
139
|
-
await this.context?.historyManager?.create({ input, output }, {
|
|
140
|
-
userId: this.context?.state.userId,
|
|
141
|
-
sessionId: this.context?.state.sessionId,
|
|
142
|
-
agentId: this.id,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Hook that is called before the agent result is returned.
|
|
147
|
-
* @param _result The agent result.
|
|
148
|
-
*/
|
|
149
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
150
|
-
async onResult(_result) {
|
|
151
|
-
// Override this method to perform additional operations before the result is returned
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Bind some inputs to the agent, used for process of `PipelineAgent` or case of `LLMDecisionAgent`.
|
|
155
|
-
* @param options The bind options.
|
|
156
|
-
* @returns The bound agent.
|
|
157
|
-
*/
|
|
158
|
-
bind(options) {
|
|
159
|
-
return {
|
|
160
|
-
...options,
|
|
161
|
-
runnable: this,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
exports.Agent = Agent;
|
package/lib/cjs/constants.d.ts
DELETED
package/lib/cjs/constants.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StreamTextOutputName = exports.TYPES = void 0;
|
|
4
|
-
exports.TYPES = {
|
|
5
|
-
context: Symbol.for("AIGNE_CONTEXT"),
|
|
6
|
-
definition: Symbol.for("AIGNE_DEFINITION"),
|
|
7
|
-
llmModel: Symbol.for("AIGNE_LLM_MODEL"),
|
|
8
|
-
sandboxFunctionRunner: Symbol.for("AIGNE_SANDBOX_FUNCTION_RUNNER"),
|
|
9
|
-
};
|
|
10
|
-
exports.StreamTextOutputName = "$text";
|
package/lib/cjs/context.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { LLMModelConfiguration } from "./llm-model";
|
|
2
|
-
import type { Memorable } from "./memorable";
|
|
3
|
-
import type { Runnable, RunnableDefinition } from "./runnable";
|
|
4
|
-
export interface ContextState {
|
|
5
|
-
userId?: string;
|
|
6
|
-
sessionId?: string;
|
|
7
|
-
[key: string]: unknown;
|
|
8
|
-
}
|
|
9
|
-
export interface ContextConfig {
|
|
10
|
-
llmModel?: LLMModelConfiguration;
|
|
11
|
-
}
|
|
12
|
-
export interface Context<State extends ContextState = ContextState, Config extends ContextConfig = ContextConfig> {
|
|
13
|
-
id: string;
|
|
14
|
-
state: State;
|
|
15
|
-
config: Config;
|
|
16
|
-
historyManager?: Memorable<{
|
|
17
|
-
input: object;
|
|
18
|
-
output: object;
|
|
19
|
-
}>;
|
|
20
|
-
resolve<T extends Runnable>(id: string | RunnableDefinition | T): Promise<T>;
|
|
21
|
-
register<R extends Array<RunnableDefinition | Runnable> = []>(...definition: R): void;
|
|
22
|
-
resolveDependency<T>(token: string | symbol): T;
|
|
23
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { MakeNullablePropertyOptional } from "../utils/nullable";
|
|
2
|
-
import { OrderedRecord } from "../utils/ordered-map";
|
|
3
|
-
import type { ArrayElement } from "../utils/type-utils";
|
|
4
|
-
import type { DataType } from "./data-type";
|
|
5
|
-
export declare function schemaToDataType(dataType: {
|
|
6
|
-
[name: string]: DataSchema;
|
|
7
|
-
}): OrderedRecord<DataType>;
|
|
8
|
-
export type DataSchema = DataSchemaString | DataSchemaNumber | DataSchemaBoolean | DataSchemaObject | DataSchemaArray;
|
|
9
|
-
export interface DataSchemaBase {
|
|
10
|
-
description?: string;
|
|
11
|
-
required?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export interface DataSchemaString extends DataSchemaBase {
|
|
14
|
-
type: "string";
|
|
15
|
-
}
|
|
16
|
-
export interface DataSchemaNumber extends DataSchemaBase {
|
|
17
|
-
type: "number";
|
|
18
|
-
}
|
|
19
|
-
export interface DataSchemaBoolean extends DataSchemaBase {
|
|
20
|
-
type: "boolean";
|
|
21
|
-
}
|
|
22
|
-
export interface DataSchemaObject extends DataSchemaBase {
|
|
23
|
-
type: "object";
|
|
24
|
-
properties?: {
|
|
25
|
-
[key: string]: DataSchema;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
export interface DataSchemaArray extends DataSchemaBase {
|
|
29
|
-
type: "array";
|
|
30
|
-
items?: DataSchema;
|
|
31
|
-
}
|
|
32
|
-
type SchemaTypeInner<T extends DataSchema> = T extends DataSchemaString ? string : T extends DataSchemaNumber ? number : T extends DataSchemaBoolean ? boolean : T extends DataSchemaObject ? MakeNullablePropertyOptional<{
|
|
33
|
-
[K in keyof T["properties"]]: SchemaType<NonNullable<T["properties"]>[K]>;
|
|
34
|
-
}> : T extends DataSchemaArray ? Extract<T["items"], null | undefined> extends never ? SchemaType<NonNullable<T["items"]>>[] : SchemaType<{
|
|
35
|
-
type: "object";
|
|
36
|
-
}>[] : never;
|
|
37
|
-
type SchemaType<T extends DataSchema> = T["required"] extends true ? SchemaTypeInner<T> : SchemaTypeInner<T> | undefined | null;
|
|
38
|
-
export type SchemaToType<T extends Record<string, DataSchema>> = SchemaType<{
|
|
39
|
-
type: "object";
|
|
40
|
-
required: true;
|
|
41
|
-
properties: T;
|
|
42
|
-
}>;
|
|
43
|
-
type TypeToSchemaInner<T> = Extract<T, string | undefined> extends string | undefined ? DataSchemaString & {
|
|
44
|
-
required: Extract<T, undefined> extends undefined ? false : true;
|
|
45
|
-
} : Extract<T, number | undefined> extends number | undefined ? DataSchemaNumber & {
|
|
46
|
-
required: Extract<T, undefined> extends undefined ? false : true;
|
|
47
|
-
} : Extract<T, boolean | undefined> extends boolean | undefined ? DataSchemaBoolean & {
|
|
48
|
-
required: Extract<T, undefined> extends undefined ? false : true;
|
|
49
|
-
} : Extract<T, object | undefined> extends object | undefined ? DataSchemaObject & {
|
|
50
|
-
properties: {
|
|
51
|
-
[K in keyof T]: TypeToSchemaInner<NonNullable<T[K]>>;
|
|
52
|
-
};
|
|
53
|
-
required: Extract<T, undefined> extends undefined ? false : true;
|
|
54
|
-
} : Extract<T, [] | undefined> extends [] | undefined ? DataSchemaArray & {
|
|
55
|
-
items: Extract<T, undefined> extends undefined ? undefined : TypeToSchemaInner<NonNullable<ArrayElement<T>>>;
|
|
56
|
-
required: Extract<T, undefined> extends undefined ? false : true;
|
|
57
|
-
} : never;
|
|
58
|
-
export type TypeToSchema<T extends Record<string, unknown>> = {
|
|
59
|
-
[K in keyof T]: TypeToSchemaInner<T[K]>;
|
|
60
|
-
};
|
|
61
|
-
export {};
|