@copilotkitnext/core 0.0.2 → 0.0.4
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 +2 -2
- package/.turbo/turbo-build.log +0 -23
- package/.turbo/turbo-check-types.log +0 -4
- package/.turbo/turbo-lint.log +0 -12
- package/.turbo/turbo-test.log +0 -96
- package/src/__tests__/core-agent-constraints.test.ts +0 -86
- package/src/__tests__/core-basic-functionality.test.ts +0 -158
- package/src/__tests__/core-basic.test.ts +0 -27
- package/src/__tests__/core-edge-cases.test.ts +0 -166
- package/src/__tests__/core-follow-up.test.ts +0 -216
- package/src/__tests__/core-full.test.ts +0 -320
- package/src/__tests__/core-simple.test.ts +0 -24
- package/src/__tests__/core-tool-minimal.test.ts +0 -41
- package/src/__tests__/core-tool-simple.test.ts +0 -40
- package/src/__tests__/core-wildcard.test.ts +0 -45
- package/src/__tests__/import.test.ts +0 -13
- package/src/__tests__/simple.test.ts +0 -7
- package/src/__tests__/test-utils.ts +0 -162
- package/src/agent.ts +0 -37
- package/src/core.ts +0 -336
- package/src/index.ts +0 -4
- package/src/types.ts +0 -23
- package/src/utils/markdown.ts +0 -271
- package/tsconfig.json +0 -12
- package/tsup.config.ts +0 -11
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { Message } from "@ag-ui/client";
|
|
2
|
-
import { vi } from "vitest";
|
|
3
|
-
import { FrontendTool } from "../types";
|
|
4
|
-
|
|
5
|
-
export interface MockAgentOptions {
|
|
6
|
-
messages?: Message[];
|
|
7
|
-
newMessages?: Message[];
|
|
8
|
-
error?: Error | string;
|
|
9
|
-
runAgentDelay?: number;
|
|
10
|
-
runAgentCallback?: (input: any) => void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class MockAgent {
|
|
14
|
-
public messages: Message[] = [];
|
|
15
|
-
public addMessages = vi.fn((messages: Message[]) => {
|
|
16
|
-
this.messages.push(...messages);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
private newMessages: Message[];
|
|
20
|
-
private error?: Error | string;
|
|
21
|
-
private runAgentDelay: number;
|
|
22
|
-
public runAgentCallback?: (input: any) => void;
|
|
23
|
-
public runAgentCalls: any[] = [];
|
|
24
|
-
|
|
25
|
-
constructor(options: MockAgentOptions = {}) {
|
|
26
|
-
this.messages = options.messages || [];
|
|
27
|
-
this.newMessages = options.newMessages || [];
|
|
28
|
-
this.error = options.error;
|
|
29
|
-
this.runAgentDelay = options.runAgentDelay || 0;
|
|
30
|
-
this.runAgentCallback = options.runAgentCallback;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async runAgent(input: any): Promise<{ newMessages: Message[] }> {
|
|
34
|
-
this.runAgentCalls.push(input);
|
|
35
|
-
|
|
36
|
-
if (this.runAgentCallback) {
|
|
37
|
-
this.runAgentCallback(input);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (this.runAgentDelay > 0) {
|
|
41
|
-
await new Promise(resolve => setTimeout(resolve, this.runAgentDelay));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (this.error) {
|
|
45
|
-
throw this.error;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return { newMessages: this.newMessages };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
clone(): MockAgent {
|
|
52
|
-
return new MockAgent({
|
|
53
|
-
messages: [...this.messages],
|
|
54
|
-
newMessages: [...this.newMessages],
|
|
55
|
-
error: this.error,
|
|
56
|
-
runAgentDelay: this.runAgentDelay,
|
|
57
|
-
runAgentCallback: this.runAgentCallback,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
setNewMessages(messages: Message[]): void {
|
|
62
|
-
this.newMessages = messages;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function createMessage(overrides: Partial<Message> = {}): Message {
|
|
67
|
-
return {
|
|
68
|
-
id: `msg-${Math.random().toString(36).substr(2, 9)}`,
|
|
69
|
-
role: "user",
|
|
70
|
-
content: "Test message",
|
|
71
|
-
...overrides,
|
|
72
|
-
} as Message;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function createAssistantMessage(
|
|
76
|
-
overrides: Partial<Message> = {}
|
|
77
|
-
): Message {
|
|
78
|
-
return createMessage({
|
|
79
|
-
role: "assistant",
|
|
80
|
-
content: "Assistant message",
|
|
81
|
-
...overrides,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function createToolCallMessage(
|
|
86
|
-
toolCallName: string,
|
|
87
|
-
args: any = {},
|
|
88
|
-
overrides: Partial<Message> = {}
|
|
89
|
-
): Message {
|
|
90
|
-
const toolCallId = `tool-call-${Math.random().toString(36).substr(2, 9)}`;
|
|
91
|
-
return createAssistantMessage({
|
|
92
|
-
content: "",
|
|
93
|
-
toolCalls: [
|
|
94
|
-
{
|
|
95
|
-
id: toolCallId,
|
|
96
|
-
type: "function",
|
|
97
|
-
function: {
|
|
98
|
-
name: toolCallName,
|
|
99
|
-
arguments: JSON.stringify(args),
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
],
|
|
103
|
-
...overrides,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function createToolResultMessage(
|
|
108
|
-
toolCallId: string,
|
|
109
|
-
content: string,
|
|
110
|
-
overrides: Partial<Message> = {}
|
|
111
|
-
): Message {
|
|
112
|
-
return createMessage({
|
|
113
|
-
role: "tool",
|
|
114
|
-
content,
|
|
115
|
-
toolCallId,
|
|
116
|
-
...overrides,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function createTool<T extends Record<string, unknown>>(
|
|
121
|
-
overrides: Partial<FrontendTool<T>> = {}
|
|
122
|
-
): FrontendTool<T> {
|
|
123
|
-
return {
|
|
124
|
-
name: `tool-${Math.random().toString(36).substr(2, 9)}`,
|
|
125
|
-
description: "Test tool",
|
|
126
|
-
handler: vi.fn(async () => "Tool result"),
|
|
127
|
-
followUp: false, // Default to false to avoid unexpected recursion in tests
|
|
128
|
-
...overrides,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function createMultipleToolCallsMessage(
|
|
133
|
-
toolCalls: Array<{ name: string; args?: any }>,
|
|
134
|
-
overrides: Partial<Message> = {}
|
|
135
|
-
): Message {
|
|
136
|
-
return createAssistantMessage({
|
|
137
|
-
content: "",
|
|
138
|
-
toolCalls: toolCalls.map((tc) => ({
|
|
139
|
-
id: `tool-call-${Math.random().toString(36).substr(2, 9)}`,
|
|
140
|
-
type: "function",
|
|
141
|
-
function: {
|
|
142
|
-
name: tc.name,
|
|
143
|
-
arguments: JSON.stringify(tc.args || {}),
|
|
144
|
-
},
|
|
145
|
-
})),
|
|
146
|
-
...overrides,
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export async function waitForCondition(
|
|
151
|
-
condition: () => boolean,
|
|
152
|
-
timeout: number = 1000,
|
|
153
|
-
interval: number = 10
|
|
154
|
-
): Promise<void> {
|
|
155
|
-
const start = Date.now();
|
|
156
|
-
while (!condition()) {
|
|
157
|
-
if (Date.now() - start > timeout) {
|
|
158
|
-
throw new Error("Timeout waiting for condition");
|
|
159
|
-
}
|
|
160
|
-
await new Promise(resolve => setTimeout(resolve, interval));
|
|
161
|
-
}
|
|
162
|
-
}
|
package/src/agent.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BaseEvent,
|
|
3
|
-
HttpAgent,
|
|
4
|
-
HttpAgentConfig,
|
|
5
|
-
RunAgentInput,
|
|
6
|
-
runHttpRequest,
|
|
7
|
-
transformHttpEventStream,
|
|
8
|
-
} from "@ag-ui/client";
|
|
9
|
-
import { Observable } from "rxjs";
|
|
10
|
-
|
|
11
|
-
export interface CopilotKitHttpAgentConfig
|
|
12
|
-
extends Omit<HttpAgentConfig, "url"> {
|
|
13
|
-
runtimeUrl?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class CopilotKitHttpAgent extends HttpAgent {
|
|
17
|
-
isCopilotKitAgent = true;
|
|
18
|
-
runtimeUrl?: string;
|
|
19
|
-
|
|
20
|
-
constructor(config: CopilotKitHttpAgentConfig) {
|
|
21
|
-
super({
|
|
22
|
-
...config,
|
|
23
|
-
url: `${config.runtimeUrl}/agent/${config.agentId}/run`,
|
|
24
|
-
});
|
|
25
|
-
this.runtimeUrl = config.runtimeUrl;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
run(input: RunAgentInput): Observable<BaseEvent> {
|
|
29
|
-
const url = (
|
|
30
|
-
input.forwardedProps.__copilotkitConnect === true
|
|
31
|
-
? `${this.runtimeUrl}/agent/${this.agentId}/connect`
|
|
32
|
-
: this.url
|
|
33
|
-
) as string;
|
|
34
|
-
const httpEvents = runHttpRequest(url, this.requestInit(input));
|
|
35
|
-
return transformHttpEventStream(httpEvents);
|
|
36
|
-
}
|
|
37
|
-
}
|
package/src/core.ts
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AgentDescription,
|
|
3
|
-
randomUUID,
|
|
4
|
-
RuntimeInfo,
|
|
5
|
-
} from "@copilotkitnext/shared";
|
|
6
|
-
import { logger } from "@copilotkitnext/shared";
|
|
7
|
-
import { AbstractAgent, Context, HttpAgent, Message } from "@ag-ui/client";
|
|
8
|
-
import { FrontendTool } from "./types";
|
|
9
|
-
import { CopilotKitHttpAgent } from "./agent";
|
|
10
|
-
|
|
11
|
-
export interface CopilotKitCoreConfig {
|
|
12
|
-
runtimeUrl?: string;
|
|
13
|
-
agents?: Record<string, AbstractAgent>;
|
|
14
|
-
headers?: Record<string, string>;
|
|
15
|
-
properties?: Record<string, unknown>;
|
|
16
|
-
tools?: Record<string, FrontendTool<any>>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface CopilotKitCoreAddAgentParams {
|
|
20
|
-
id: string;
|
|
21
|
-
agent: AbstractAgent;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface RunAgentParams {
|
|
25
|
-
agent: AbstractAgent;
|
|
26
|
-
withMessages?: Message[];
|
|
27
|
-
agentId?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface CopilotKitCoreSubscriber {
|
|
31
|
-
onRuntimeLoaded?: (event: {
|
|
32
|
-
copilotkit: CopilotKitCore;
|
|
33
|
-
}) => void | Promise<void>;
|
|
34
|
-
onRuntimeLoadError?: (event: {
|
|
35
|
-
copilotkit: CopilotKitCore;
|
|
36
|
-
}) => void | Promise<void>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export class CopilotKitCore {
|
|
40
|
-
runtimeUrl?: string;
|
|
41
|
-
didLoadRuntime: boolean = false;
|
|
42
|
-
|
|
43
|
-
context: Record<string, Context> = {};
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
-
tools: Record<string, FrontendTool<any>> = {};
|
|
46
|
-
agents: Record<string, AbstractAgent> = {};
|
|
47
|
-
|
|
48
|
-
headers: Record<string, string>;
|
|
49
|
-
properties: Record<string, unknown>;
|
|
50
|
-
|
|
51
|
-
version?: string;
|
|
52
|
-
|
|
53
|
-
private localAgents: Record<string, AbstractAgent> = {};
|
|
54
|
-
private remoteAgents: Record<string, AbstractAgent> = {};
|
|
55
|
-
private subscribers: Set<CopilotKitCoreSubscriber> = new Set();
|
|
56
|
-
|
|
57
|
-
constructor({
|
|
58
|
-
runtimeUrl,
|
|
59
|
-
headers = {},
|
|
60
|
-
properties = {},
|
|
61
|
-
agents = {},
|
|
62
|
-
tools = {},
|
|
63
|
-
}: CopilotKitCoreConfig) {
|
|
64
|
-
this.headers = headers;
|
|
65
|
-
this.properties = properties;
|
|
66
|
-
this.localAgents = agents;
|
|
67
|
-
this.agents = this.localAgents;
|
|
68
|
-
this.tools = tools;
|
|
69
|
-
|
|
70
|
-
this.setRuntimeUrl(runtimeUrl);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private async getRuntimeInfo() {
|
|
74
|
-
const response = await fetch(`${this.runtimeUrl}/info`, {
|
|
75
|
-
headers: this.headers,
|
|
76
|
-
});
|
|
77
|
-
const {
|
|
78
|
-
version,
|
|
79
|
-
...runtimeInfo
|
|
80
|
-
}: {
|
|
81
|
-
agents: Record<string, AgentDescription>;
|
|
82
|
-
version: string;
|
|
83
|
-
} = (await response.json()) as RuntimeInfo;
|
|
84
|
-
|
|
85
|
-
const agents: Record<string, AbstractAgent> = Object.fromEntries(
|
|
86
|
-
Object.entries(runtimeInfo.agents).map(([id, { description }]) => {
|
|
87
|
-
const agent = new CopilotKitHttpAgent({
|
|
88
|
-
runtimeUrl: this.runtimeUrl,
|
|
89
|
-
agentId: id,
|
|
90
|
-
description: description,
|
|
91
|
-
});
|
|
92
|
-
return [id, agent];
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
return { agents, version };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private async fetchRemoteAgents() {
|
|
100
|
-
if (this.runtimeUrl) {
|
|
101
|
-
this.getRuntimeInfo()
|
|
102
|
-
.then(({ agents, version }) => {
|
|
103
|
-
this.remoteAgents = agents;
|
|
104
|
-
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
105
|
-
this.didLoadRuntime = true;
|
|
106
|
-
this.version = version;
|
|
107
|
-
|
|
108
|
-
this.subscribers.forEach(async (subscriber) => {
|
|
109
|
-
try {
|
|
110
|
-
await subscriber.onRuntimeLoaded?.({ copilotkit: this });
|
|
111
|
-
} catch (error) {
|
|
112
|
-
logger.error(
|
|
113
|
-
"Error in CopilotKitCore subscriber (onRuntimeLoaded):",
|
|
114
|
-
error
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
})
|
|
119
|
-
.catch((error) => {
|
|
120
|
-
this.subscribers.forEach(async (subscriber) => {
|
|
121
|
-
try {
|
|
122
|
-
await subscriber.onRuntimeLoadError?.({ copilotkit: this });
|
|
123
|
-
} catch (error) {
|
|
124
|
-
logger.error(
|
|
125
|
-
"Error in CopilotKitCore subscriber (onRuntimeLoadError):",
|
|
126
|
-
error
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
logger.warn(`Failed to load runtime info: ${error.message}`);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
setAgents(agents: Record<string, AbstractAgent>) {
|
|
136
|
-
this.localAgents = agents;
|
|
137
|
-
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
addAgent({ id, agent }: CopilotKitCoreAddAgentParams) {
|
|
141
|
-
this.localAgents[id] = agent;
|
|
142
|
-
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
removeAgent(id: string) {
|
|
146
|
-
delete this.localAgents[id];
|
|
147
|
-
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
getAgent(id: string): AbstractAgent | undefined {
|
|
151
|
-
if (id in this.agents) {
|
|
152
|
-
return this.agents[id] as AbstractAgent;
|
|
153
|
-
} else {
|
|
154
|
-
if (!this.didLoadRuntime) {
|
|
155
|
-
return undefined;
|
|
156
|
-
} else {
|
|
157
|
-
throw new Error(`Agent ${id} not found`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
addContext({ description, value }: Context): string {
|
|
163
|
-
const id = randomUUID();
|
|
164
|
-
this.context[id] = { description, value };
|
|
165
|
-
return id;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
removeContext(id: string) {
|
|
169
|
-
delete this.context[id];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
setRuntimeUrl(runtimeUrl?: string) {
|
|
173
|
-
this.runtimeUrl = runtimeUrl ? runtimeUrl.replace(/\/$/, "") : undefined;
|
|
174
|
-
this.fetchRemoteAgents();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
addTool<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
178
|
-
tool: FrontendTool<T>
|
|
179
|
-
) {
|
|
180
|
-
if (tool.name in this.tools) {
|
|
181
|
-
logger.warn(`Tool already exists: '${tool.name}', skipping.`);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
this.tools[tool.name] = tool;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
removeTool(id: string) {
|
|
189
|
-
delete this.tools[id];
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
setHeaders(headers: Record<string, string>) {
|
|
193
|
-
this.headers = headers;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
setProperties(properties: Record<string, unknown>) {
|
|
197
|
-
this.properties = properties;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
subscribe(subscriber: CopilotKitCoreSubscriber): () => void {
|
|
201
|
-
this.subscribers.add(subscriber);
|
|
202
|
-
|
|
203
|
-
// Return unsubscribe function
|
|
204
|
-
return () => {
|
|
205
|
-
this.unsubscribe(subscriber);
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
unsubscribe(subscriber: CopilotKitCoreSubscriber) {
|
|
210
|
-
this.subscribers.delete(subscriber);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// TODO: AG-UI needs to expose the runAgent result type
|
|
214
|
-
async runAgent({
|
|
215
|
-
agent,
|
|
216
|
-
withMessages,
|
|
217
|
-
agentId,
|
|
218
|
-
}: RunAgentParams): Promise<Awaited<ReturnType<typeof agent.runAgent>>> {
|
|
219
|
-
if (withMessages) {
|
|
220
|
-
agent.addMessages(withMessages);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const runAgentResult = await agent.runAgent({
|
|
224
|
-
forwardedProps: this.properties,
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
const { newMessages } = runAgentResult;
|
|
228
|
-
|
|
229
|
-
let needsFollowUp = false;
|
|
230
|
-
|
|
231
|
-
for (const message of newMessages) {
|
|
232
|
-
if (message.role === "assistant") {
|
|
233
|
-
for (const toolCall of message.toolCalls || []) {
|
|
234
|
-
if (
|
|
235
|
-
newMessages.findIndex(
|
|
236
|
-
(m) => m.role === "tool" && m.toolCallId === toolCall.id
|
|
237
|
-
) === -1
|
|
238
|
-
) {
|
|
239
|
-
if (toolCall.function.name in this.tools) {
|
|
240
|
-
const tool = this.tools[toolCall.function.name];
|
|
241
|
-
|
|
242
|
-
// Check if tool is constrained to a specific agent
|
|
243
|
-
if (tool?.agentId && tool.agentId !== agentId) {
|
|
244
|
-
// Tool is not available for this agent, skip it
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
let toolCallResult = "";
|
|
249
|
-
if (tool?.handler) {
|
|
250
|
-
const args = JSON.parse(toolCall.function.arguments);
|
|
251
|
-
try {
|
|
252
|
-
const result = await tool.handler(args);
|
|
253
|
-
if (result === undefined || result === null) {
|
|
254
|
-
toolCallResult = "";
|
|
255
|
-
} else if (typeof result === "string") {
|
|
256
|
-
toolCallResult = result;
|
|
257
|
-
} else {
|
|
258
|
-
toolCallResult = JSON.stringify(result);
|
|
259
|
-
}
|
|
260
|
-
} catch (error) {
|
|
261
|
-
toolCallResult = `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const messageIndex = agent.messages.findIndex(
|
|
266
|
-
(m) => m.id === message.id
|
|
267
|
-
);
|
|
268
|
-
const toolMessage = {
|
|
269
|
-
id: randomUUID(),
|
|
270
|
-
role: "tool" as const,
|
|
271
|
-
toolCallId: toolCall.id,
|
|
272
|
-
content: toolCallResult,
|
|
273
|
-
};
|
|
274
|
-
agent.messages.splice(messageIndex + 1, 0, toolMessage);
|
|
275
|
-
|
|
276
|
-
if (tool?.followUp !== false) {
|
|
277
|
-
needsFollowUp = true;
|
|
278
|
-
}
|
|
279
|
-
} else if ("*" in this.tools) {
|
|
280
|
-
// Wildcard fallback for undefined tools
|
|
281
|
-
const wildcardTool = this.tools["*"];
|
|
282
|
-
|
|
283
|
-
// Check if wildcard tool is constrained to a specific agent
|
|
284
|
-
if (wildcardTool?.agentId && wildcardTool.agentId !== agentId) {
|
|
285
|
-
// Wildcard tool is not available for this agent, skip it
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
let toolCallResult = "";
|
|
290
|
-
if (wildcardTool?.handler) {
|
|
291
|
-
// Pass both the tool name and original args to the wildcard handler
|
|
292
|
-
const wildcardArgs = {
|
|
293
|
-
toolName: toolCall.function.name,
|
|
294
|
-
args: JSON.parse(toolCall.function.arguments),
|
|
295
|
-
};
|
|
296
|
-
try {
|
|
297
|
-
const result = await wildcardTool.handler(wildcardArgs);
|
|
298
|
-
if (result === undefined || result === null) {
|
|
299
|
-
toolCallResult = "";
|
|
300
|
-
} else if (typeof result === "string") {
|
|
301
|
-
toolCallResult = result;
|
|
302
|
-
} else {
|
|
303
|
-
toolCallResult = JSON.stringify(result);
|
|
304
|
-
}
|
|
305
|
-
} catch (error) {
|
|
306
|
-
toolCallResult = `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const messageIndex = agent.messages.findIndex(
|
|
311
|
-
(m) => m.id === message.id
|
|
312
|
-
);
|
|
313
|
-
const toolMessage = {
|
|
314
|
-
id: randomUUID(),
|
|
315
|
-
role: "tool" as const,
|
|
316
|
-
toolCallId: toolCall.id,
|
|
317
|
-
content: toolCallResult,
|
|
318
|
-
};
|
|
319
|
-
agent.messages.splice(messageIndex + 1, 0, toolMessage);
|
|
320
|
-
|
|
321
|
-
if (wildcardTool?.followUp !== false) {
|
|
322
|
-
needsFollowUp = true;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (needsFollowUp) {
|
|
331
|
-
return await this.runAgent({ agent, agentId });
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return runAgentResult;
|
|
335
|
-
}
|
|
336
|
-
}
|
package/src/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Status of a tool call execution
|
|
5
|
-
*/
|
|
6
|
-
export enum ToolCallStatus {
|
|
7
|
-
InProgress = "inProgress",
|
|
8
|
-
Executing = "executing",
|
|
9
|
-
Complete = "complete"
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type FrontendTool<T extends Record<string, unknown> = Record<string, unknown>> = {
|
|
13
|
-
name: string;
|
|
14
|
-
description?: string;
|
|
15
|
-
parameters?: z.ZodType<T>;
|
|
16
|
-
handler?: (args: T) => Promise<unknown>;
|
|
17
|
-
followUp?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Optional agent ID to constrain this tool to a specific agent.
|
|
20
|
-
* If specified, this tool will only be available to the specified agent.
|
|
21
|
-
*/
|
|
22
|
-
agentId?: string;
|
|
23
|
-
};
|