@copilotkitnext/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +22 -0
- package/.turbo/turbo-check-types.log +4 -0
- package/.turbo/turbo-lint.log +12 -0
- package/.turbo/turbo-test.log +96 -0
- package/LICENSE +11 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/agent.ts.html +193 -0
- package/coverage/lcov-report/src/core.ts.html +919 -0
- package/coverage/lcov-report/src/index.html +146 -0
- package/coverage/lcov-report/src/types.ts.html +112 -0
- package/coverage/lcov-report/src/utils/index.html +116 -0
- package/coverage/lcov-report/src/utils/markdown.ts.html +895 -0
- package/coverage/lcov.info +556 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/agent.ts.html +193 -0
- package/coverage/src/core.ts.html +919 -0
- package/coverage/src/index.html +146 -0
- package/coverage/src/types.ts.html +112 -0
- package/coverage/src/utils/index.html +116 -0
- package/coverage/src/utils/markdown.ts.html +895 -0
- package/dist/index.d.mts +93 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +526 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +502 -0
- package/dist/index.mjs.map +1 -0
- package/eslint.config.mjs +3 -0
- package/package.json +46 -0
- package/src/__tests__/core-agent-constraints.test.ts +86 -0
- package/src/__tests__/core-basic-functionality.test.ts +158 -0
- package/src/__tests__/core-basic.test.ts +27 -0
- package/src/__tests__/core-edge-cases.test.ts +166 -0
- package/src/__tests__/core-follow-up.test.ts +216 -0
- package/src/__tests__/core-full.test.ts +320 -0
- package/src/__tests__/core-simple.test.ts +24 -0
- package/src/__tests__/core-tool-minimal.test.ts +41 -0
- package/src/__tests__/core-tool-simple.test.ts +40 -0
- package/src/__tests__/core-wildcard.test.ts +45 -0
- package/src/__tests__/import.test.ts +13 -0
- package/src/__tests__/simple.test.ts +7 -0
- package/src/__tests__/test-utils.ts +162 -0
- package/src/agent.ts +37 -0
- package/src/core.ts +336 -0
- package/src/index.ts +4 -0
- package/src/types.ts +23 -0
- package/src/utils/markdown.ts +271 -0
- package/tsconfig.json +12 -0
- package/tsup.config.ts +11 -0
- package/vitest.config.mjs +15 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AbstractAgent, Message, Context, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Status of a tool call execution
|
|
7
|
+
*/
|
|
8
|
+
declare enum ToolCallStatus {
|
|
9
|
+
InProgress = "inProgress",
|
|
10
|
+
Executing = "executing",
|
|
11
|
+
Complete = "complete"
|
|
12
|
+
}
|
|
13
|
+
type FrontendTool<T extends Record<string, unknown> = Record<string, unknown>> = {
|
|
14
|
+
name: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
parameters?: z.ZodType<T>;
|
|
17
|
+
handler?: (args: T) => Promise<unknown>;
|
|
18
|
+
followUp?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Optional agent ID to constrain this tool to a specific agent.
|
|
21
|
+
* If specified, this tool will only be available to the specified agent.
|
|
22
|
+
*/
|
|
23
|
+
agentId?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
interface CopilotKitCoreConfig {
|
|
27
|
+
runtimeUrl?: string;
|
|
28
|
+
agents?: Record<string, AbstractAgent>;
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
properties?: Record<string, unknown>;
|
|
31
|
+
tools?: Record<string, FrontendTool<any>>;
|
|
32
|
+
}
|
|
33
|
+
interface CopilotKitCoreAddAgentParams {
|
|
34
|
+
id: string;
|
|
35
|
+
agent: AbstractAgent;
|
|
36
|
+
}
|
|
37
|
+
interface RunAgentParams {
|
|
38
|
+
agent: AbstractAgent;
|
|
39
|
+
withMessages?: Message[];
|
|
40
|
+
agentId?: string;
|
|
41
|
+
}
|
|
42
|
+
interface CopilotKitCoreSubscriber {
|
|
43
|
+
onRuntimeLoaded?: (event: {
|
|
44
|
+
copilotkit: CopilotKitCore;
|
|
45
|
+
}) => void | Promise<void>;
|
|
46
|
+
onRuntimeLoadError?: (event: {
|
|
47
|
+
copilotkit: CopilotKitCore;
|
|
48
|
+
}) => void | Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
declare class CopilotKitCore {
|
|
51
|
+
runtimeUrl?: string;
|
|
52
|
+
didLoadRuntime: boolean;
|
|
53
|
+
context: Record<string, Context>;
|
|
54
|
+
tools: Record<string, FrontendTool<any>>;
|
|
55
|
+
agents: Record<string, AbstractAgent>;
|
|
56
|
+
headers: Record<string, string>;
|
|
57
|
+
properties: Record<string, unknown>;
|
|
58
|
+
version?: string;
|
|
59
|
+
private localAgents;
|
|
60
|
+
private remoteAgents;
|
|
61
|
+
private subscribers;
|
|
62
|
+
constructor({ runtimeUrl, headers, properties, agents, tools, }: CopilotKitCoreConfig);
|
|
63
|
+
private getRuntimeInfo;
|
|
64
|
+
private fetchRemoteAgents;
|
|
65
|
+
setAgents(agents: Record<string, AbstractAgent>): void;
|
|
66
|
+
addAgent({ id, agent }: CopilotKitCoreAddAgentParams): void;
|
|
67
|
+
removeAgent(id: string): void;
|
|
68
|
+
getAgent(id: string): AbstractAgent | undefined;
|
|
69
|
+
addContext({ description, value }: Context): string;
|
|
70
|
+
removeContext(id: string): void;
|
|
71
|
+
setRuntimeUrl(runtimeUrl?: string): void;
|
|
72
|
+
addTool<T extends Record<string, unknown> = Record<string, unknown>>(tool: FrontendTool<T>): void;
|
|
73
|
+
removeTool(id: string): void;
|
|
74
|
+
setHeaders(headers: Record<string, string>): void;
|
|
75
|
+
setProperties(properties: Record<string, unknown>): void;
|
|
76
|
+
subscribe(subscriber: CopilotKitCoreSubscriber): () => void;
|
|
77
|
+
unsubscribe(subscriber: CopilotKitCoreSubscriber): void;
|
|
78
|
+
runAgent({ agent, withMessages, agentId, }: RunAgentParams): Promise<Awaited<ReturnType<typeof agent.runAgent>>>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface CopilotKitHttpAgentConfig extends Omit<HttpAgentConfig, "url"> {
|
|
82
|
+
runtimeUrl?: string;
|
|
83
|
+
}
|
|
84
|
+
declare class CopilotKitHttpAgent extends HttpAgent {
|
|
85
|
+
isCopilotKitAgent: boolean;
|
|
86
|
+
runtimeUrl?: string;
|
|
87
|
+
constructor(config: CopilotKitHttpAgentConfig);
|
|
88
|
+
run(input: RunAgentInput): Observable<BaseEvent>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declare function completePartialMarkdown(input: string): string;
|
|
92
|
+
|
|
93
|
+
export { CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreSubscriber, CopilotKitHttpAgent, type CopilotKitHttpAgentConfig, type FrontendTool, type RunAgentParams, ToolCallStatus, completePartialMarkdown };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AbstractAgent, Message, Context, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Status of a tool call execution
|
|
7
|
+
*/
|
|
8
|
+
declare enum ToolCallStatus {
|
|
9
|
+
InProgress = "inProgress",
|
|
10
|
+
Executing = "executing",
|
|
11
|
+
Complete = "complete"
|
|
12
|
+
}
|
|
13
|
+
type FrontendTool<T extends Record<string, unknown> = Record<string, unknown>> = {
|
|
14
|
+
name: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
parameters?: z.ZodType<T>;
|
|
17
|
+
handler?: (args: T) => Promise<unknown>;
|
|
18
|
+
followUp?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Optional agent ID to constrain this tool to a specific agent.
|
|
21
|
+
* If specified, this tool will only be available to the specified agent.
|
|
22
|
+
*/
|
|
23
|
+
agentId?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
interface CopilotKitCoreConfig {
|
|
27
|
+
runtimeUrl?: string;
|
|
28
|
+
agents?: Record<string, AbstractAgent>;
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
properties?: Record<string, unknown>;
|
|
31
|
+
tools?: Record<string, FrontendTool<any>>;
|
|
32
|
+
}
|
|
33
|
+
interface CopilotKitCoreAddAgentParams {
|
|
34
|
+
id: string;
|
|
35
|
+
agent: AbstractAgent;
|
|
36
|
+
}
|
|
37
|
+
interface RunAgentParams {
|
|
38
|
+
agent: AbstractAgent;
|
|
39
|
+
withMessages?: Message[];
|
|
40
|
+
agentId?: string;
|
|
41
|
+
}
|
|
42
|
+
interface CopilotKitCoreSubscriber {
|
|
43
|
+
onRuntimeLoaded?: (event: {
|
|
44
|
+
copilotkit: CopilotKitCore;
|
|
45
|
+
}) => void | Promise<void>;
|
|
46
|
+
onRuntimeLoadError?: (event: {
|
|
47
|
+
copilotkit: CopilotKitCore;
|
|
48
|
+
}) => void | Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
declare class CopilotKitCore {
|
|
51
|
+
runtimeUrl?: string;
|
|
52
|
+
didLoadRuntime: boolean;
|
|
53
|
+
context: Record<string, Context>;
|
|
54
|
+
tools: Record<string, FrontendTool<any>>;
|
|
55
|
+
agents: Record<string, AbstractAgent>;
|
|
56
|
+
headers: Record<string, string>;
|
|
57
|
+
properties: Record<string, unknown>;
|
|
58
|
+
version?: string;
|
|
59
|
+
private localAgents;
|
|
60
|
+
private remoteAgents;
|
|
61
|
+
private subscribers;
|
|
62
|
+
constructor({ runtimeUrl, headers, properties, agents, tools, }: CopilotKitCoreConfig);
|
|
63
|
+
private getRuntimeInfo;
|
|
64
|
+
private fetchRemoteAgents;
|
|
65
|
+
setAgents(agents: Record<string, AbstractAgent>): void;
|
|
66
|
+
addAgent({ id, agent }: CopilotKitCoreAddAgentParams): void;
|
|
67
|
+
removeAgent(id: string): void;
|
|
68
|
+
getAgent(id: string): AbstractAgent | undefined;
|
|
69
|
+
addContext({ description, value }: Context): string;
|
|
70
|
+
removeContext(id: string): void;
|
|
71
|
+
setRuntimeUrl(runtimeUrl?: string): void;
|
|
72
|
+
addTool<T extends Record<string, unknown> = Record<string, unknown>>(tool: FrontendTool<T>): void;
|
|
73
|
+
removeTool(id: string): void;
|
|
74
|
+
setHeaders(headers: Record<string, string>): void;
|
|
75
|
+
setProperties(properties: Record<string, unknown>): void;
|
|
76
|
+
subscribe(subscriber: CopilotKitCoreSubscriber): () => void;
|
|
77
|
+
unsubscribe(subscriber: CopilotKitCoreSubscriber): void;
|
|
78
|
+
runAgent({ agent, withMessages, agentId, }: RunAgentParams): Promise<Awaited<ReturnType<typeof agent.runAgent>>>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface CopilotKitHttpAgentConfig extends Omit<HttpAgentConfig, "url"> {
|
|
82
|
+
runtimeUrl?: string;
|
|
83
|
+
}
|
|
84
|
+
declare class CopilotKitHttpAgent extends HttpAgent {
|
|
85
|
+
isCopilotKitAgent: boolean;
|
|
86
|
+
runtimeUrl?: string;
|
|
87
|
+
constructor(config: CopilotKitHttpAgentConfig);
|
|
88
|
+
run(input: RunAgentInput): Observable<BaseEvent>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declare function completePartialMarkdown(input: string): string;
|
|
92
|
+
|
|
93
|
+
export { CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreSubscriber, CopilotKitHttpAgent, type CopilotKitHttpAgentConfig, type FrontendTool, type RunAgentParams, ToolCallStatus, completePartialMarkdown };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CopilotKitCore: () => CopilotKitCore,
|
|
24
|
+
CopilotKitHttpAgent: () => CopilotKitHttpAgent,
|
|
25
|
+
ToolCallStatus: () => ToolCallStatus,
|
|
26
|
+
completePartialMarkdown: () => completePartialMarkdown
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/core.ts
|
|
31
|
+
var import_shared = require("@copilotkitnext/shared");
|
|
32
|
+
var import_shared2 = require("@copilotkitnext/shared");
|
|
33
|
+
|
|
34
|
+
// src/agent.ts
|
|
35
|
+
var import_client = require("@ag-ui/client");
|
|
36
|
+
var CopilotKitHttpAgent = class extends import_client.HttpAgent {
|
|
37
|
+
isCopilotKitAgent = true;
|
|
38
|
+
runtimeUrl;
|
|
39
|
+
constructor(config) {
|
|
40
|
+
super({
|
|
41
|
+
...config,
|
|
42
|
+
url: `${config.runtimeUrl}/agent/${config.agentId}/run`
|
|
43
|
+
});
|
|
44
|
+
this.runtimeUrl = config.runtimeUrl;
|
|
45
|
+
}
|
|
46
|
+
run(input) {
|
|
47
|
+
const url = input.forwardedProps.__copilotkitConnect === true ? `${this.runtimeUrl}/agent/${this.agentId}/connect` : this.url;
|
|
48
|
+
const httpEvents = (0, import_client.runHttpRequest)(url, this.requestInit(input));
|
|
49
|
+
return (0, import_client.transformHttpEventStream)(httpEvents);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/core.ts
|
|
54
|
+
var CopilotKitCore = class {
|
|
55
|
+
runtimeUrl;
|
|
56
|
+
didLoadRuntime = false;
|
|
57
|
+
context = {};
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
tools = {};
|
|
60
|
+
agents = {};
|
|
61
|
+
headers;
|
|
62
|
+
properties;
|
|
63
|
+
version;
|
|
64
|
+
localAgents = {};
|
|
65
|
+
remoteAgents = {};
|
|
66
|
+
subscribers = /* @__PURE__ */ new Set();
|
|
67
|
+
constructor({
|
|
68
|
+
runtimeUrl,
|
|
69
|
+
headers = {},
|
|
70
|
+
properties = {},
|
|
71
|
+
agents = {},
|
|
72
|
+
tools = {}
|
|
73
|
+
}) {
|
|
74
|
+
this.headers = headers;
|
|
75
|
+
this.properties = properties;
|
|
76
|
+
this.localAgents = agents;
|
|
77
|
+
this.agents = this.localAgents;
|
|
78
|
+
this.tools = tools;
|
|
79
|
+
this.setRuntimeUrl(runtimeUrl);
|
|
80
|
+
}
|
|
81
|
+
async getRuntimeInfo() {
|
|
82
|
+
const response = await fetch(`${this.runtimeUrl}/info`, {
|
|
83
|
+
headers: this.headers
|
|
84
|
+
});
|
|
85
|
+
const {
|
|
86
|
+
version,
|
|
87
|
+
...runtimeInfo
|
|
88
|
+
} = await response.json();
|
|
89
|
+
const agents = Object.fromEntries(
|
|
90
|
+
Object.entries(runtimeInfo.agents).map(([id, { description }]) => {
|
|
91
|
+
const agent = new CopilotKitHttpAgent({
|
|
92
|
+
runtimeUrl: this.runtimeUrl,
|
|
93
|
+
agentId: id,
|
|
94
|
+
description
|
|
95
|
+
});
|
|
96
|
+
return [id, agent];
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
return { agents, version };
|
|
100
|
+
}
|
|
101
|
+
async fetchRemoteAgents() {
|
|
102
|
+
if (this.runtimeUrl) {
|
|
103
|
+
this.getRuntimeInfo().then(({ agents, version }) => {
|
|
104
|
+
this.remoteAgents = agents;
|
|
105
|
+
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
106
|
+
this.didLoadRuntime = true;
|
|
107
|
+
this.version = version;
|
|
108
|
+
this.subscribers.forEach(async (subscriber) => {
|
|
109
|
+
try {
|
|
110
|
+
await subscriber.onRuntimeLoaded?.({ copilotkit: this });
|
|
111
|
+
} catch (error) {
|
|
112
|
+
import_shared2.logger.error(
|
|
113
|
+
"Error in CopilotKitCore subscriber (onRuntimeLoaded):",
|
|
114
|
+
error
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}).catch((error) => {
|
|
119
|
+
this.subscribers.forEach(async (subscriber) => {
|
|
120
|
+
try {
|
|
121
|
+
await subscriber.onRuntimeLoadError?.({ copilotkit: this });
|
|
122
|
+
} catch (error2) {
|
|
123
|
+
import_shared2.logger.error(
|
|
124
|
+
"Error in CopilotKitCore subscriber (onRuntimeLoadError):",
|
|
125
|
+
error2
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
import_shared2.logger.warn(`Failed to load runtime info: ${error.message}`);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
setAgents(agents) {
|
|
134
|
+
this.localAgents = agents;
|
|
135
|
+
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
136
|
+
}
|
|
137
|
+
addAgent({ id, agent }) {
|
|
138
|
+
this.localAgents[id] = agent;
|
|
139
|
+
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
140
|
+
}
|
|
141
|
+
removeAgent(id) {
|
|
142
|
+
delete this.localAgents[id];
|
|
143
|
+
this.agents = { ...this.localAgents, ...this.remoteAgents };
|
|
144
|
+
}
|
|
145
|
+
getAgent(id) {
|
|
146
|
+
if (id in this.agents) {
|
|
147
|
+
return this.agents[id];
|
|
148
|
+
} else {
|
|
149
|
+
if (!this.didLoadRuntime) {
|
|
150
|
+
return void 0;
|
|
151
|
+
} else {
|
|
152
|
+
throw new Error(`Agent ${id} not found`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
addContext({ description, value }) {
|
|
157
|
+
const id = (0, import_shared.randomUUID)();
|
|
158
|
+
this.context[id] = { description, value };
|
|
159
|
+
return id;
|
|
160
|
+
}
|
|
161
|
+
removeContext(id) {
|
|
162
|
+
delete this.context[id];
|
|
163
|
+
}
|
|
164
|
+
setRuntimeUrl(runtimeUrl) {
|
|
165
|
+
this.runtimeUrl = runtimeUrl ? runtimeUrl.replace(/\/$/, "") : void 0;
|
|
166
|
+
this.fetchRemoteAgents();
|
|
167
|
+
}
|
|
168
|
+
addTool(tool) {
|
|
169
|
+
if (tool.name in this.tools) {
|
|
170
|
+
import_shared2.logger.warn(`Tool already exists: '${tool.name}', skipping.`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.tools[tool.name] = tool;
|
|
174
|
+
}
|
|
175
|
+
removeTool(id) {
|
|
176
|
+
delete this.tools[id];
|
|
177
|
+
}
|
|
178
|
+
setHeaders(headers) {
|
|
179
|
+
this.headers = headers;
|
|
180
|
+
}
|
|
181
|
+
setProperties(properties) {
|
|
182
|
+
this.properties = properties;
|
|
183
|
+
}
|
|
184
|
+
subscribe(subscriber) {
|
|
185
|
+
this.subscribers.add(subscriber);
|
|
186
|
+
return () => {
|
|
187
|
+
this.unsubscribe(subscriber);
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
unsubscribe(subscriber) {
|
|
191
|
+
this.subscribers.delete(subscriber);
|
|
192
|
+
}
|
|
193
|
+
// TODO: AG-UI needs to expose the runAgent result type
|
|
194
|
+
async runAgent({
|
|
195
|
+
agent,
|
|
196
|
+
withMessages,
|
|
197
|
+
agentId
|
|
198
|
+
}) {
|
|
199
|
+
if (withMessages) {
|
|
200
|
+
agent.addMessages(withMessages);
|
|
201
|
+
}
|
|
202
|
+
const runAgentResult = await agent.runAgent({
|
|
203
|
+
forwardedProps: this.properties
|
|
204
|
+
});
|
|
205
|
+
const { newMessages } = runAgentResult;
|
|
206
|
+
let needsFollowUp = false;
|
|
207
|
+
for (const message of newMessages) {
|
|
208
|
+
if (message.role === "assistant") {
|
|
209
|
+
for (const toolCall of message.toolCalls || []) {
|
|
210
|
+
if (newMessages.findIndex(
|
|
211
|
+
(m) => m.role === "tool" && m.toolCallId === toolCall.id
|
|
212
|
+
) === -1) {
|
|
213
|
+
if (toolCall.function.name in this.tools) {
|
|
214
|
+
const tool = this.tools[toolCall.function.name];
|
|
215
|
+
if (tool?.agentId && tool.agentId !== agentId) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
let toolCallResult = "";
|
|
219
|
+
if (tool?.handler) {
|
|
220
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
221
|
+
try {
|
|
222
|
+
const result = await tool.handler(args);
|
|
223
|
+
if (result === void 0 || result === null) {
|
|
224
|
+
toolCallResult = "";
|
|
225
|
+
} else if (typeof result === "string") {
|
|
226
|
+
toolCallResult = result;
|
|
227
|
+
} else {
|
|
228
|
+
toolCallResult = JSON.stringify(result);
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
toolCallResult = `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const messageIndex = agent.messages.findIndex(
|
|
235
|
+
(m) => m.id === message.id
|
|
236
|
+
);
|
|
237
|
+
const toolMessage = {
|
|
238
|
+
id: (0, import_shared.randomUUID)(),
|
|
239
|
+
role: "tool",
|
|
240
|
+
toolCallId: toolCall.id,
|
|
241
|
+
content: toolCallResult
|
|
242
|
+
};
|
|
243
|
+
agent.messages.splice(messageIndex + 1, 0, toolMessage);
|
|
244
|
+
if (tool?.followUp !== false) {
|
|
245
|
+
needsFollowUp = true;
|
|
246
|
+
}
|
|
247
|
+
} else if ("*" in this.tools) {
|
|
248
|
+
const wildcardTool = this.tools["*"];
|
|
249
|
+
if (wildcardTool?.agentId && wildcardTool.agentId !== agentId) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
let toolCallResult = "";
|
|
253
|
+
if (wildcardTool?.handler) {
|
|
254
|
+
const wildcardArgs = {
|
|
255
|
+
toolName: toolCall.function.name,
|
|
256
|
+
args: JSON.parse(toolCall.function.arguments)
|
|
257
|
+
};
|
|
258
|
+
try {
|
|
259
|
+
const result = await wildcardTool.handler(wildcardArgs);
|
|
260
|
+
if (result === void 0 || result === null) {
|
|
261
|
+
toolCallResult = "";
|
|
262
|
+
} else if (typeof result === "string") {
|
|
263
|
+
toolCallResult = result;
|
|
264
|
+
} else {
|
|
265
|
+
toolCallResult = JSON.stringify(result);
|
|
266
|
+
}
|
|
267
|
+
} catch (error) {
|
|
268
|
+
toolCallResult = `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const messageIndex = agent.messages.findIndex(
|
|
272
|
+
(m) => m.id === message.id
|
|
273
|
+
);
|
|
274
|
+
const toolMessage = {
|
|
275
|
+
id: (0, import_shared.randomUUID)(),
|
|
276
|
+
role: "tool",
|
|
277
|
+
toolCallId: toolCall.id,
|
|
278
|
+
content: toolCallResult
|
|
279
|
+
};
|
|
280
|
+
agent.messages.splice(messageIndex + 1, 0, toolMessage);
|
|
281
|
+
if (wildcardTool?.followUp !== false) {
|
|
282
|
+
needsFollowUp = true;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (needsFollowUp) {
|
|
290
|
+
return await this.runAgent({ agent, agentId });
|
|
291
|
+
}
|
|
292
|
+
return runAgentResult;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/types.ts
|
|
297
|
+
var ToolCallStatus = /* @__PURE__ */ ((ToolCallStatus2) => {
|
|
298
|
+
ToolCallStatus2["InProgress"] = "inProgress";
|
|
299
|
+
ToolCallStatus2["Executing"] = "executing";
|
|
300
|
+
ToolCallStatus2["Complete"] = "complete";
|
|
301
|
+
return ToolCallStatus2;
|
|
302
|
+
})(ToolCallStatus || {});
|
|
303
|
+
|
|
304
|
+
// src/utils/markdown.ts
|
|
305
|
+
function completePartialMarkdown(input) {
|
|
306
|
+
let s = input;
|
|
307
|
+
const fenceMatches = Array.from(s.matchAll(/^(\s*)(`{3,}|~{3,})/gm));
|
|
308
|
+
if (fenceMatches.length % 2 === 1) {
|
|
309
|
+
const [, indent, fence] = fenceMatches[0];
|
|
310
|
+
s += `
|
|
311
|
+
${indent}${fence}`;
|
|
312
|
+
}
|
|
313
|
+
const incompleteLinkMatch = s.match(/\[([^\]]*)\]\(([^)]*)$/);
|
|
314
|
+
if (incompleteLinkMatch) {
|
|
315
|
+
s += ")";
|
|
316
|
+
}
|
|
317
|
+
const openElements = [];
|
|
318
|
+
const chars = Array.from(s);
|
|
319
|
+
const codeBlockRanges = [];
|
|
320
|
+
const inlineCodeRanges = [];
|
|
321
|
+
let tempCodeFenceCount = 0;
|
|
322
|
+
let currentCodeBlockStart = -1;
|
|
323
|
+
for (let i = 0; i < chars.length; i++) {
|
|
324
|
+
if (i === 0 || chars[i - 1] === "\n") {
|
|
325
|
+
const lineMatch = s.substring(i).match(/^(\s*)(`{3,}|~{3,})/);
|
|
326
|
+
if (lineMatch) {
|
|
327
|
+
tempCodeFenceCount++;
|
|
328
|
+
if (tempCodeFenceCount % 2 === 1) {
|
|
329
|
+
currentCodeBlockStart = i;
|
|
330
|
+
} else if (currentCodeBlockStart !== -1) {
|
|
331
|
+
codeBlockRanges.push({
|
|
332
|
+
start: currentCodeBlockStart,
|
|
333
|
+
end: i + lineMatch[0].length
|
|
334
|
+
});
|
|
335
|
+
currentCodeBlockStart = -1;
|
|
336
|
+
}
|
|
337
|
+
i += lineMatch[0].length - 1;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
for (let i = 0; i < chars.length; i++) {
|
|
342
|
+
if (chars[i] === "`") {
|
|
343
|
+
let backslashCount = 0;
|
|
344
|
+
for (let j = i - 1; j >= 0 && chars[j] === "\\"; j--) {
|
|
345
|
+
backslashCount++;
|
|
346
|
+
}
|
|
347
|
+
if (backslashCount % 2 === 0) {
|
|
348
|
+
for (let j = i + 1; j < chars.length; j++) {
|
|
349
|
+
if (chars[j] === "`") {
|
|
350
|
+
let closingBackslashCount = 0;
|
|
351
|
+
for (let k = j - 1; k >= 0 && chars[k] === "\\"; k--) {
|
|
352
|
+
closingBackslashCount++;
|
|
353
|
+
}
|
|
354
|
+
if (closingBackslashCount % 2 === 0) {
|
|
355
|
+
inlineCodeRanges.push({ start: i, end: j + 1 });
|
|
356
|
+
i = j;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const isInCode = (pos) => {
|
|
365
|
+
return codeBlockRanges.some((range) => pos >= range.start && pos < range.end) || inlineCodeRanges.some((range) => pos >= range.start && pos < range.end);
|
|
366
|
+
};
|
|
367
|
+
for (let i = 0; i < chars.length; i++) {
|
|
368
|
+
const char = chars[i];
|
|
369
|
+
const nextChar = chars[i + 1];
|
|
370
|
+
const prevChar = chars[i - 1];
|
|
371
|
+
if (isInCode(i)) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (char === "[") {
|
|
375
|
+
let isCompleteLink = false;
|
|
376
|
+
let bracketDepth = 1;
|
|
377
|
+
let j = i + 1;
|
|
378
|
+
while (j < chars.length && bracketDepth > 0) {
|
|
379
|
+
if (chars[j] === "[" && !isInCode(j)) bracketDepth++;
|
|
380
|
+
if (chars[j] === "]" && !isInCode(j)) bracketDepth--;
|
|
381
|
+
j++;
|
|
382
|
+
}
|
|
383
|
+
if (bracketDepth === 0 && chars[j] === "(") {
|
|
384
|
+
let parenDepth = 1;
|
|
385
|
+
j++;
|
|
386
|
+
while (j < chars.length && parenDepth > 0) {
|
|
387
|
+
if (chars[j] === "(" && !isInCode(j)) parenDepth++;
|
|
388
|
+
if (chars[j] === ")" && !isInCode(j)) parenDepth--;
|
|
389
|
+
j++;
|
|
390
|
+
}
|
|
391
|
+
if (parenDepth === 0) {
|
|
392
|
+
isCompleteLink = true;
|
|
393
|
+
i = j - 1;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (!isCompleteLink) {
|
|
398
|
+
const existingIndex = openElements.findIndex(
|
|
399
|
+
(el) => el.type === "bracket"
|
|
400
|
+
);
|
|
401
|
+
if (existingIndex !== -1) {
|
|
402
|
+
openElements.splice(existingIndex, 1);
|
|
403
|
+
} else {
|
|
404
|
+
openElements.push({ type: "bracket", marker: "[", position: i });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
} else if (char === "*" && nextChar === "*") {
|
|
408
|
+
const existingIndex = openElements.findIndex(
|
|
409
|
+
(el) => el.type === "bold_star"
|
|
410
|
+
);
|
|
411
|
+
if (existingIndex !== -1) {
|
|
412
|
+
openElements.splice(existingIndex, 1);
|
|
413
|
+
} else {
|
|
414
|
+
openElements.push({ type: "bold_star", marker: "**", position: i });
|
|
415
|
+
}
|
|
416
|
+
i++;
|
|
417
|
+
} else if (char === "_" && nextChar === "_") {
|
|
418
|
+
const existingIndex = openElements.findIndex(
|
|
419
|
+
(el) => el.type === "bold_underscore"
|
|
420
|
+
);
|
|
421
|
+
if (existingIndex !== -1) {
|
|
422
|
+
openElements.splice(existingIndex, 1);
|
|
423
|
+
} else {
|
|
424
|
+
openElements.push({
|
|
425
|
+
type: "bold_underscore",
|
|
426
|
+
marker: "__",
|
|
427
|
+
position: i
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
i++;
|
|
431
|
+
} else if (char === "~" && nextChar === "~") {
|
|
432
|
+
const existingIndex = openElements.findIndex(
|
|
433
|
+
(el) => el.type === "strike"
|
|
434
|
+
);
|
|
435
|
+
if (existingIndex !== -1) {
|
|
436
|
+
openElements.splice(existingIndex, 1);
|
|
437
|
+
} else {
|
|
438
|
+
openElements.push({ type: "strike", marker: "~~", position: i });
|
|
439
|
+
}
|
|
440
|
+
i++;
|
|
441
|
+
} else if (char === "*" && prevChar !== "*" && nextChar !== "*") {
|
|
442
|
+
const existingIndex = openElements.findIndex(
|
|
443
|
+
(el) => el.type === "italic_star"
|
|
444
|
+
);
|
|
445
|
+
if (existingIndex !== -1) {
|
|
446
|
+
openElements.splice(existingIndex, 1);
|
|
447
|
+
} else {
|
|
448
|
+
openElements.push({ type: "italic_star", marker: "*", position: i });
|
|
449
|
+
}
|
|
450
|
+
} else if (char === "_" && prevChar !== "_" && nextChar !== "_") {
|
|
451
|
+
const existingIndex = openElements.findIndex(
|
|
452
|
+
(el) => el.type === "italic_underscore"
|
|
453
|
+
);
|
|
454
|
+
if (existingIndex !== -1) {
|
|
455
|
+
openElements.splice(existingIndex, 1);
|
|
456
|
+
} else {
|
|
457
|
+
openElements.push({
|
|
458
|
+
type: "italic_underscore",
|
|
459
|
+
marker: "_",
|
|
460
|
+
position: i
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
let backtickCount = 0;
|
|
466
|
+
for (let i = 0; i < chars.length; i++) {
|
|
467
|
+
if (chars[i] === "`" && !isInCode(i)) {
|
|
468
|
+
backtickCount++;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (backtickCount % 2 === 1) {
|
|
472
|
+
s += "`";
|
|
473
|
+
}
|
|
474
|
+
openElements.sort((a, b) => b.position - a.position);
|
|
475
|
+
const closers = openElements.map((el) => {
|
|
476
|
+
switch (el.type) {
|
|
477
|
+
case "bracket":
|
|
478
|
+
return "]";
|
|
479
|
+
case "bold_star":
|
|
480
|
+
return "**";
|
|
481
|
+
case "bold_underscore":
|
|
482
|
+
return "__";
|
|
483
|
+
case "strike":
|
|
484
|
+
return "~~";
|
|
485
|
+
case "italic_star":
|
|
486
|
+
return "*";
|
|
487
|
+
case "italic_underscore":
|
|
488
|
+
return "_";
|
|
489
|
+
default:
|
|
490
|
+
return "";
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
let result = s + closers.join("");
|
|
494
|
+
const finalFenceMatches = Array.from(
|
|
495
|
+
result.matchAll(/^(\s*)(`{3,}|~{3,})/gm)
|
|
496
|
+
);
|
|
497
|
+
const hasUnclosedBacktick = (result.match(/`/g) || []).length % 2 === 1;
|
|
498
|
+
const hasUnclosedCodeFence = finalFenceMatches.length % 2 === 1;
|
|
499
|
+
let shouldCloseParens = !hasUnclosedBacktick && !hasUnclosedCodeFence;
|
|
500
|
+
if (shouldCloseParens) {
|
|
501
|
+
const lastOpenParen = result.lastIndexOf("(");
|
|
502
|
+
if (lastOpenParen !== -1) {
|
|
503
|
+
const beforeParen = result.substring(0, lastOpenParen);
|
|
504
|
+
const backticksBeforeParen = (beforeParen.match(/`/g) || []).length;
|
|
505
|
+
if (backticksBeforeParen % 2 === 1) {
|
|
506
|
+
shouldCloseParens = false;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (shouldCloseParens) {
|
|
511
|
+
const openParens = (result.match(/\(/g) || []).length;
|
|
512
|
+
const closeParens = (result.match(/\)/g) || []).length;
|
|
513
|
+
if (openParens > closeParens) {
|
|
514
|
+
result += ")".repeat(openParens - closeParens);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
520
|
+
0 && (module.exports = {
|
|
521
|
+
CopilotKitCore,
|
|
522
|
+
CopilotKitHttpAgent,
|
|
523
|
+
ToolCallStatus,
|
|
524
|
+
completePartialMarkdown
|
|
525
|
+
});
|
|
526
|
+
//# sourceMappingURL=index.js.map
|