@ag-ui/core 0.0.27

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/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@ag-ui/core",
3
+ "author": "Markus Ecker <markus.ecker@gmail.com>",
4
+ "version": "0.0.27",
5
+ "private": false,
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "dependencies": {
13
+ "rxjs": "7.8.1",
14
+ "zod": "^3.22.4"
15
+ },
16
+ "devDependencies": {
17
+ "@types/jest": "^29.5.12",
18
+ "jest": "^29.7.0",
19
+ "ts-jest": "^29.1.2",
20
+ "tsup": "^8.0.2",
21
+ "typescript": "^5.8.2"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "dev": "tsup --watch",
26
+ "lint": "eslint \"src/**/*.ts*\"",
27
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
28
+ "test": "jest",
29
+ "link:global": "pnpm link --global",
30
+ "unlink:global": "pnpm unlink --global"
31
+ }
32
+ }
@@ -0,0 +1,5 @@
1
+ describe("Core package", () => {
2
+ it("should pass a simple test", () => {
3
+ expect(true).toBe(true);
4
+ });
5
+ });
package/src/events.ts ADDED
@@ -0,0 +1,182 @@
1
+ import { z } from "zod";
2
+ import { MessageSchema, StateSchema } from "./types";
3
+
4
+ export enum EventType {
5
+ TEXT_MESSAGE_START = "TEXT_MESSAGE_START",
6
+ TEXT_MESSAGE_CONTENT = "TEXT_MESSAGE_CONTENT",
7
+ TEXT_MESSAGE_END = "TEXT_MESSAGE_END",
8
+ TOOL_CALL_START = "TOOL_CALL_START",
9
+ TOOL_CALL_ARGS = "TOOL_CALL_ARGS",
10
+ TOOL_CALL_END = "TOOL_CALL_END",
11
+ STATE_SNAPSHOT = "STATE_SNAPSHOT",
12
+ STATE_DELTA = "STATE_DELTA",
13
+ MESSAGES_SNAPSHOT = "MESSAGES_SNAPSHOT",
14
+ RAW = "RAW",
15
+ CUSTOM = "CUSTOM",
16
+ RUN_STARTED = "RUN_STARTED",
17
+ RUN_FINISHED = "RUN_FINISHED",
18
+ RUN_ERROR = "RUN_ERROR",
19
+ STEP_STARTED = "STEP_STARTED",
20
+ STEP_FINISHED = "STEP_FINISHED",
21
+ }
22
+
23
+ const BaseEventSchema = z.object({
24
+ type: z.nativeEnum(EventType),
25
+ timestamp: z.number().optional(),
26
+ rawEvent: z.any().optional(),
27
+ });
28
+
29
+ export const RunStartedSchema = BaseEventSchema.extend({
30
+ type: z.literal(EventType.RUN_STARTED),
31
+ threadId: z.string(),
32
+ runId: z.string(),
33
+ });
34
+
35
+ export const RunFinishedSchema = BaseEventSchema.extend({
36
+ type: z.literal(EventType.RUN_FINISHED),
37
+ threadId: z.string(),
38
+ runId: z.string(),
39
+ });
40
+
41
+ export const RunErrorSchema = BaseEventSchema.extend({
42
+ type: z.literal(EventType.RUN_ERROR),
43
+ message: z.string(),
44
+ code: z.string().optional(),
45
+ });
46
+
47
+ export const StepStartedSchema = BaseEventSchema.extend({
48
+ type: z.literal(EventType.STEP_STARTED),
49
+ stepName: z.string(),
50
+ });
51
+
52
+ export const StepFinishedSchema = BaseEventSchema.extend({
53
+ type: z.literal(EventType.STEP_FINISHED),
54
+ stepName: z.string(),
55
+ });
56
+
57
+ export const TextMessageStartEventSchema = BaseEventSchema.extend({
58
+ type: z.literal(EventType.TEXT_MESSAGE_START),
59
+ messageId: z.string(),
60
+ role: z.literal("assistant"),
61
+ });
62
+
63
+ export const TextMessageContentEventSchema = BaseEventSchema.extend({
64
+ type: z.literal(EventType.TEXT_MESSAGE_CONTENT),
65
+ messageId: z.string(),
66
+ delta: z.string().refine((s) => s.length > 0, "Delta must not be an empty string"),
67
+ });
68
+
69
+ export const TextMessageEndEventSchema = BaseEventSchema.extend({
70
+ type: z.literal(EventType.TEXT_MESSAGE_END),
71
+ messageId: z.string(),
72
+ });
73
+
74
+ export const ToolCallStartEventSchema = BaseEventSchema.extend({
75
+ type: z.literal(EventType.TOOL_CALL_START),
76
+ toolCallId: z.string(),
77
+ toolCallName: z.string(),
78
+ parentMessageId: z.string().optional(),
79
+ });
80
+
81
+ export const ToolCallArgsEventSchema = BaseEventSchema.extend({
82
+ type: z.literal(EventType.TOOL_CALL_ARGS),
83
+ toolCallId: z.string(),
84
+ delta: z.string(),
85
+ });
86
+
87
+ export const ToolCallEndEventSchema = BaseEventSchema.extend({
88
+ type: z.literal(EventType.TOOL_CALL_END),
89
+ toolCallId: z.string(),
90
+ });
91
+
92
+ export const StateSnapshotEventSchema = BaseEventSchema.extend({
93
+ type: z.literal(EventType.STATE_SNAPSHOT),
94
+ snapshot: StateSchema,
95
+ });
96
+
97
+ export const StateDeltaEventSchema = BaseEventSchema.extend({
98
+ type: z.literal(EventType.STATE_DELTA),
99
+ delta: z.array(z.any()), // JSON Patch (RFC 6902)
100
+ });
101
+
102
+ export const MessagesSnapshotEventSchema = BaseEventSchema.extend({
103
+ type: z.literal(EventType.MESSAGES_SNAPSHOT),
104
+ messages: z.array(MessageSchema),
105
+ });
106
+
107
+ export const RawEventSchema = BaseEventSchema.extend({
108
+ type: z.literal(EventType.RAW),
109
+ event: z.any(),
110
+ source: z.string().optional(),
111
+ });
112
+
113
+ export const CustomEventSchema = BaseEventSchema.extend({
114
+ type: z.literal(EventType.CUSTOM),
115
+ name: z.string(),
116
+ value: z.any(),
117
+ });
118
+
119
+ export const RunStartedEventSchema = BaseEventSchema.extend({
120
+ type: z.literal(EventType.RUN_STARTED),
121
+ threadId: z.string(),
122
+ runId: z.string(),
123
+ });
124
+
125
+ export const RunFinishedEventSchema = BaseEventSchema.extend({
126
+ type: z.literal(EventType.RUN_FINISHED),
127
+ threadId: z.string(),
128
+ runId: z.string(),
129
+ });
130
+
131
+ export const RunErrorEventSchema = BaseEventSchema.extend({
132
+ type: z.literal(EventType.RUN_ERROR),
133
+ message: z.string(),
134
+ code: z.string().optional(),
135
+ });
136
+
137
+ export const StepStartedEventSchema = BaseEventSchema.extend({
138
+ type: z.literal(EventType.STEP_STARTED),
139
+ stepName: z.string(),
140
+ });
141
+
142
+ export const StepFinishedEventSchema = BaseEventSchema.extend({
143
+ type: z.literal(EventType.STEP_FINISHED),
144
+ stepName: z.string(),
145
+ });
146
+
147
+ export const EventSchemas = z.discriminatedUnion("type", [
148
+ TextMessageStartEventSchema,
149
+ TextMessageContentEventSchema,
150
+ TextMessageEndEventSchema,
151
+ ToolCallStartEventSchema,
152
+ ToolCallArgsEventSchema,
153
+ ToolCallEndEventSchema,
154
+ StateSnapshotEventSchema,
155
+ StateDeltaEventSchema,
156
+ MessagesSnapshotEventSchema,
157
+ RawEventSchema,
158
+ CustomEventSchema,
159
+ RunStartedEventSchema,
160
+ RunFinishedEventSchema,
161
+ RunErrorEventSchema,
162
+ StepStartedEventSchema,
163
+ StepFinishedEventSchema,
164
+ ]);
165
+
166
+ export type BaseEvent = z.infer<typeof BaseEventSchema>;
167
+ export type TextMessageStartEvent = z.infer<typeof TextMessageStartEventSchema>;
168
+ export type TextMessageContentEvent = z.infer<typeof TextMessageContentEventSchema>;
169
+ export type TextMessageEndEvent = z.infer<typeof TextMessageEndEventSchema>;
170
+ export type ToolCallStartEvent = z.infer<typeof ToolCallStartEventSchema>;
171
+ export type ToolCallArgsEvent = z.infer<typeof ToolCallArgsEventSchema>;
172
+ export type ToolCallEndEvent = z.infer<typeof ToolCallEndEventSchema>;
173
+ export type StateSnapshotEvent = z.infer<typeof StateSnapshotEventSchema>;
174
+ export type StateDeltaEvent = z.infer<typeof StateDeltaEventSchema>;
175
+ export type MessagesSnapshotEvent = z.infer<typeof MessagesSnapshotEventSchema>;
176
+ export type RawEvent = z.infer<typeof RawEventSchema>;
177
+ export type CustomEvent = z.infer<typeof CustomEventSchema>;
178
+ export type RunStartedEvent = z.infer<typeof RunStartedEventSchema>;
179
+ export type RunFinishedEvent = z.infer<typeof RunFinishedEventSchema>;
180
+ export type RunErrorEvent = z.infer<typeof RunErrorEventSchema>;
181
+ export type StepStartedEvent = z.infer<typeof StepStartedEventSchema>;
182
+ export type StepFinishedEvent = z.infer<typeof StepFinishedEventSchema>;
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ // Export all base types and schemas
2
+ export * from "./types";
3
+
4
+ // Export all event-related types and schemas
5
+ export * from "./events";
6
+
7
+ // Export all stream-related types and schemas
8
+ export * from "./stream";
package/src/stream.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Observable } from "rxjs";
2
+ import { Message, State, RunAgentInput } from "./types";
3
+ import { BaseEvent } from "./events";
4
+
5
+ /**
6
+ * Function type for agent runners that process input and return a stream of results.
7
+ */
8
+ export type RunAgent = (input: RunAgentInput) => Observable<BaseEvent>;
9
+
10
+ /**
11
+ * The transformed state of an agent.
12
+ */
13
+ export interface AgentState {
14
+ messages?: Message[];
15
+ state?: State;
16
+ }
17
+
18
+ /**
19
+ * Maps a stream of BaseEvent objects to a stream of AgentState objects.
20
+ * @returns A function that transforms an Observable<BaseEvent> into an Observable<TransformedState>
21
+ */
22
+ export type ApplyEvents = (
23
+ input: RunAgentInput,
24
+ events$: Observable<BaseEvent>,
25
+ ) => Observable<AgentState>;
package/src/types.ts ADDED
@@ -0,0 +1,106 @@
1
+ import { z } from "zod";
2
+
3
+ export const FunctionCallSchema = z.object({
4
+ name: z.string(),
5
+ arguments: z.string(),
6
+ });
7
+
8
+ export const ToolCallSchema = z.object({
9
+ id: z.string(),
10
+ type: z.literal("function"),
11
+ function: FunctionCallSchema,
12
+ });
13
+
14
+ export const BaseMessageSchema = z.object({
15
+ id: z.string(),
16
+ role: z.string(),
17
+ content: z.string().optional(),
18
+ name: z.string().optional(),
19
+ });
20
+
21
+ export const DeveloperMessageSchema = BaseMessageSchema.extend({
22
+ role: z.literal("developer"),
23
+ content: z.string(),
24
+ });
25
+
26
+ export const SystemMessageSchema = BaseMessageSchema.extend({
27
+ role: z.literal("system"),
28
+ content: z.string(),
29
+ });
30
+
31
+ export const AssistantMessageSchema = BaseMessageSchema.extend({
32
+ role: z.literal("assistant"),
33
+ content: z.string().optional(),
34
+ toolCalls: z.array(ToolCallSchema).optional(),
35
+ });
36
+
37
+ export const UserMessageSchema = BaseMessageSchema.extend({
38
+ role: z.literal("user"),
39
+ content: z.string(),
40
+ });
41
+
42
+ export const ToolMessageSchema = z.object({
43
+ id: z.string(),
44
+ content: z.string(),
45
+ role: z.literal("tool"),
46
+ toolCallId: z.string(),
47
+ });
48
+
49
+ export const MessageSchema = z.discriminatedUnion("role", [
50
+ DeveloperMessageSchema,
51
+ SystemMessageSchema,
52
+ AssistantMessageSchema,
53
+ UserMessageSchema,
54
+ ToolMessageSchema,
55
+ ]);
56
+
57
+ export const RoleSchema = z.union([
58
+ z.literal("developer"),
59
+ z.literal("system"),
60
+ z.literal("assistant"),
61
+ z.literal("user"),
62
+ z.literal("tool"),
63
+ ]);
64
+
65
+ export const ContextSchema = z.object({
66
+ description: z.string(),
67
+ value: z.string(),
68
+ });
69
+
70
+ export const ToolSchema = z.object({
71
+ name: z.string(),
72
+ description: z.string(),
73
+ parameters: z.any(), // JSON Schema for the tool parameters
74
+ });
75
+
76
+ export const RunAgentInputSchema = z.object({
77
+ threadId: z.string(),
78
+ runId: z.string(),
79
+ state: z.any(),
80
+ messages: z.array(MessageSchema),
81
+ tools: z.array(ToolSchema),
82
+ context: z.array(ContextSchema),
83
+ forwardedProps: z.any(),
84
+ });
85
+
86
+ export const StateSchema = z.any();
87
+
88
+ export type ToolCall = z.infer<typeof ToolCallSchema>;
89
+ export type FunctionCall = z.infer<typeof FunctionCallSchema>;
90
+ export type DeveloperMessage = z.infer<typeof DeveloperMessageSchema>;
91
+ export type SystemMessage = z.infer<typeof SystemMessageSchema>;
92
+ export type AssistantMessage = z.infer<typeof AssistantMessageSchema>;
93
+ export type UserMessage = z.infer<typeof UserMessageSchema>;
94
+ export type ToolMessage = z.infer<typeof ToolMessageSchema>;
95
+ export type Message = z.infer<typeof MessageSchema>;
96
+ export type Context = z.infer<typeof ContextSchema>;
97
+ export type Tool = z.infer<typeof ToolSchema>;
98
+ export type RunAgentInput = z.infer<typeof RunAgentInputSchema>;
99
+ export type State = z.infer<typeof StateSchema>;
100
+ export type Role = z.infer<typeof RoleSchema>;
101
+
102
+ export class AGUIError extends Error {
103
+ constructor(message: string) {
104
+ super(message);
105
+ }
106
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2017",
4
+ "module": "esnext",
5
+ "lib": ["dom", "dom.iterable", "esnext"],
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "sourceMap": true,
9
+ "moduleResolution": "node",
10
+ "skipLibCheck": true,
11
+ "strict": true,
12
+ "jsx": "react-jsx",
13
+ "esModuleInterop": true,
14
+ "resolveJsonModule": true,
15
+ "isolatedModules": true,
16
+ "baseUrl": ".",
17
+ "paths": {
18
+ "@/*": ["./src/*"]
19
+ }
20
+ },
21
+ "include": ["src"],
22
+ "exclude": ["node_modules", "dist"]
23
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ splitting: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ });