@kernel4632/agent-core 0.38.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +243 -1463
- package/dist/agent-core.js +151 -343
- package/dist/types/index.d.ts +2 -0
- package/dist/types/src/agent.d.ts +20 -0
- package/dist/types/src/context/build.d.ts +26 -0
- package/dist/types/src/history.d.ts +10 -0
- package/dist/types/src/loop/compact.d.ts +6 -0
- package/dist/types/src/loop/decide.d.ts +10 -0
- package/dist/types/src/loop/run.d.ts +3 -0
- package/dist/types/src/model/ask.d.ts +2 -0
- package/dist/types/src/model/configure.d.ts +5 -0
- package/dist/types/src/model/text-tools/describe.d.ts +11 -0
- package/dist/types/src/model/text-tools/parse.d.ts +6 -0
- package/dist/types/src/tools/child.d.ts +13 -0
- package/dist/types/src/tools/load.d.ts +6 -0
- package/dist/types/src/tools/mcp.d.ts +5 -0
- package/dist/types/src/tools/run.d.ts +16 -0
- package/dist/types/src/tools/shape.d.ts +16 -0
- package/dist/types/src/tools/spawn.d.ts +19 -0
- package/dist/types/src/types.d.ts +324 -0
- package/dist/types/src/utils/errors.d.ts +9 -0
- package/dist/types/src/utils/retry.d.ts +17 -0
- package/dist/types/src/utils/tokens.d.ts +13 -0
- package/package.json +19 -17
- package/dist/agent-core.d.ts +0 -288
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { Agent } from './src/agent.js';
|
|
2
|
+
export type { Config, CreateOptions, Input, Answer, Reason, Usage, Callbacks, Chunk, Stopped, MCP, MCPToggle, Message, AssistantMessage, CompactMessage, ErrorMessage, RequestRecord, Draft, Role, ToolDefinition, ToolMeta, ToolCall, ToolResult, MCPServer, AskRequest, Reply, RetryOptions, RetryFilter, ErrorKind, Protocol, ModelInput, MediaSwitch, ReasoningMode, ContextKeep, CacheOption, } from './src/types.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Answer, Callbacks, Config, CreateOptions, Input, MCP, Message, Stopped } from './types.js';
|
|
2
|
+
import { ask } from './model/ask.js';
|
|
3
|
+
export declare class Agent {
|
|
4
|
+
#private;
|
|
5
|
+
static readonly LLM: {
|
|
6
|
+
ask: typeof ask;
|
|
7
|
+
};
|
|
8
|
+
id: string;
|
|
9
|
+
config: Config;
|
|
10
|
+
history: Message[];
|
|
11
|
+
tools: string[];
|
|
12
|
+
readonly MCP: MCP;
|
|
13
|
+
contextRatio: number | undefined;
|
|
14
|
+
ratios: Record<string, number>;
|
|
15
|
+
constructor(options: CreateOptions);
|
|
16
|
+
static create(options: CreateOptions): Agent;
|
|
17
|
+
send(input?: Input, callbacks?: Callbacks): Promise<Answer>;
|
|
18
|
+
stop(): Promise<Stopped>;
|
|
19
|
+
compact(callbacks?: Callbacks): Promise<string>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { JSONSchema7, ModelMessage } from 'ai';
|
|
2
|
+
import type { Callbacks, RatioSource, State } from '../types.js';
|
|
3
|
+
export declare const DEFAULT_CONTEXT: {
|
|
4
|
+
head: number;
|
|
5
|
+
beforeSummary: number;
|
|
6
|
+
budget: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const DEFAULT_MAX_CONTEXT_TOKENS = 128000;
|
|
9
|
+
export declare const DEFAULT_COMPACT_THRESHOLD = 0.8;
|
|
10
|
+
export type ToolSchemas = Record<string, {
|
|
11
|
+
description: string;
|
|
12
|
+
parameters: JSONSchema7;
|
|
13
|
+
}>;
|
|
14
|
+
export interface Built {
|
|
15
|
+
system: string | undefined;
|
|
16
|
+
messages: ModelMessage[];
|
|
17
|
+
tokens: number;
|
|
18
|
+
chars: number;
|
|
19
|
+
media: number;
|
|
20
|
+
budget: number;
|
|
21
|
+
threshold: number;
|
|
22
|
+
ratio: number;
|
|
23
|
+
ratioSource: RatioSource;
|
|
24
|
+
over: boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare function build(state: State, tools: ToolSchemas, onWarning?: Callbacks['onWarning']): Built;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Draft, Message } from './types.js';
|
|
2
|
+
export declare const PARTS: ReadonlySet<string>;
|
|
3
|
+
export declare function append(history: Message[], draft: Draft): Message;
|
|
4
|
+
export declare function check(part: unknown): string | null;
|
|
5
|
+
export declare function turns(history: Message[]): Message[][];
|
|
6
|
+
export declare function unanswered(history: Message[]): {
|
|
7
|
+
toolCallId: string;
|
|
8
|
+
toolName: string;
|
|
9
|
+
input: unknown;
|
|
10
|
+
}[];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Callbacks, CompactReason, State, Usage } from '../types.js';
|
|
2
|
+
export declare const SUMMARY_PROMPT = "\u8BF7\u628A\u4EE5\u4E0A\u5BF9\u8BDD\u538B\u7F29\u6210\u4E00\u6BB5\u603B\u7ED3\uFF0C\u4F9B\u4F60\u81EA\u5DF1\u540E\u7EED\u7EE7\u7EED\u5DE5\u4F5C\u65F6\u4F7F\u7528\u3002\n\u4FDD\u7559\uFF1A\u5DF2\u7ECF\u786E\u8BA4\u7684\u4E8B\u5B9E\u4E0E\u6570\u636E\u3001\u5DF2\u5B8C\u6210\u7684\u6B65\u9AA4\u53CA\u5176\u7ED3\u8BBA\u3001\u8FD8\u6CA1\u505A\u5B8C\u7684\u4E8B\u3001\u7528\u6237\u6700\u521D\u7684\u76EE\u6807\u3002\n\u4E22\u5F03\uFF1A\u5BD2\u6684\u3001\u91CD\u590D\u7684\u4E2D\u95F4\u8FC7\u7A0B\u3001\u5DF2\u7ECF\u88AB\u540E\u7EED\u7ED3\u679C\u63A8\u7FFB\u7684\u5185\u5BB9\u3002\n\u53EA\u8F93\u51FA\u603B\u7ED3\u672C\u8EAB\uFF0C\u4E0D\u8981\u8BF4\"\u597D\u7684\"\uFF0C\u4E0D\u8981\u7EE7\u7EED\u5BF9\u8BDD\u3002";
|
|
3
|
+
export declare function compact(state: State, callbacks?: Callbacks, reason?: CompactReason): Promise<{
|
|
4
|
+
summary: string;
|
|
5
|
+
usage: Usage;
|
|
6
|
+
}>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Config, Decision, Reply, ToolResult } from '../types.js';
|
|
2
|
+
export declare const DEFAULT_NO_TOOL_ROUNDS = 3;
|
|
3
|
+
export declare const DEFAULT_NO_TOOL_PROMPT = "[\u9519\u8BEF] \u4F60\u521A\u624D\u7684\u54CD\u5E94\u4E2D\u6CA1\u6709\u4F7F\u7528\u5DE5\u5177\uFF01\u8BF7\u7EE7\u7EED\u4F7F\u7528\u5DE5\u5177\uFF08\u8FD9\u662F\u4E00\u6761\u7CFB\u7EDF\u63D0\u9192\u6D88\u606F\uFF0C\u8BF7\u52FF\u4EE5\u5BF9\u8BDD\u5F62\u5F0F\u56DE\u590D\uFF09";
|
|
4
|
+
export interface Round {
|
|
5
|
+
reply: Reply;
|
|
6
|
+
results: ToolResult[];
|
|
7
|
+
idle: number;
|
|
8
|
+
hasTools: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function decide(round: Round, config: Config): Decision;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { LanguageModel, ModelMessage } from 'ai';
|
|
2
|
+
import type { AskRequest } from '../types.js';
|
|
3
|
+
export declare function configure(request: AskRequest, system: string | undefined, messages: ModelMessage[], hasTools: boolean): Record<string, unknown> & {
|
|
4
|
+
model: LanguageModel;
|
|
5
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JSONSchema7, ModelMessage } from 'ai';
|
|
2
|
+
export interface TextToolSpec {
|
|
3
|
+
names: string[];
|
|
4
|
+
params: Record<string, Record<string, string>>;
|
|
5
|
+
instructions: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function describe(tools: Record<string, {
|
|
8
|
+
description: string;
|
|
9
|
+
parameters: JSONSchema7;
|
|
10
|
+
}> | undefined): TextToolSpec | null;
|
|
11
|
+
export declare function render(messages: ModelMessage[]): ModelMessage[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ToolDefinition } from '../types.js';
|
|
2
|
+
export type ChildRequest = {
|
|
3
|
+
mode: 'meta';
|
|
4
|
+
path: string;
|
|
5
|
+
} | {
|
|
6
|
+
mode: 'run';
|
|
7
|
+
path: string;
|
|
8
|
+
args: unknown;
|
|
9
|
+
limit?: number;
|
|
10
|
+
toolCallId: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function check(value: unknown, path: string): ToolDefinition;
|
|
13
|
+
export declare function main(): Promise<void>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Internal, ToolMeta } from '../types.js';
|
|
2
|
+
export declare const LOAD_TIMEOUT = 3000;
|
|
3
|
+
export declare function load(paths: string[], internal: Pick<Internal, 'toolCache' | 'warned' | 'procs'>, onWarning?: (warning: {
|
|
4
|
+
path?: string;
|
|
5
|
+
message: string;
|
|
6
|
+
}) => void): Promise<ToolMeta[]>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Tool } from 'ai';
|
|
2
|
+
import type { Internal, MCPServer } from '../types.js';
|
|
3
|
+
export declare const MCP_TIMEOUT = 15000;
|
|
4
|
+
export declare function connect(servers: MCPServer[], clients: Internal['MCPClients']): Record<string, Tool>;
|
|
5
|
+
export declare function close(clients: Internal['MCPClients'], name?: string): Promise<boolean>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Tool } from 'ai';
|
|
2
|
+
import type { Subprocess } from 'bun';
|
|
3
|
+
import type { ToolCall, ToolMeta, ToolResult } from '../types.js';
|
|
4
|
+
export interface Runnable {
|
|
5
|
+
metas: ToolMeta[];
|
|
6
|
+
MCPTools: Record<string, Tool>;
|
|
7
|
+
}
|
|
8
|
+
export declare function run(calls: ToolCall[], tools: Runnable, procs: Set<Subprocess>, options?: {
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
limit?: number;
|
|
11
|
+
onOutput?: (output: {
|
|
12
|
+
toolCallId: string;
|
|
13
|
+
stream: 'stdout' | 'stderr';
|
|
14
|
+
data: string;
|
|
15
|
+
}) => unknown;
|
|
16
|
+
}): Promise<ToolResult[]>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ToolDefinition } from '../types.js';
|
|
2
|
+
export interface Shaped {
|
|
3
|
+
output: {
|
|
4
|
+
type: string;
|
|
5
|
+
value?: unknown;
|
|
6
|
+
reason?: string;
|
|
7
|
+
};
|
|
8
|
+
stop?: boolean;
|
|
9
|
+
truncated?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function shape(value: unknown, options?: {
|
|
12
|
+
limit?: number;
|
|
13
|
+
toModelOutput?: ToolDefinition['toModelOutput'];
|
|
14
|
+
input?: unknown;
|
|
15
|
+
toolCallId?: string;
|
|
16
|
+
}): Promise<Shaped>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Subprocess } from 'bun';
|
|
2
|
+
import type { ChildRequest } from './child.js';
|
|
3
|
+
export type ChildResult = {
|
|
4
|
+
ok: true;
|
|
5
|
+
meta?: any;
|
|
6
|
+
output?: any;
|
|
7
|
+
stop?: boolean;
|
|
8
|
+
truncated?: boolean;
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
error: string;
|
|
12
|
+
killed?: boolean;
|
|
13
|
+
timedOut?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export declare function spawn(request: ChildRequest, procs: Set<Subprocess>, options?: {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
onOutput?: (stream: 'stdout' | 'stderr', data: string) => unknown;
|
|
18
|
+
}): Promise<ChildResult>;
|
|
19
|
+
export declare function kill(procs: Set<Subprocess>): Promise<number>;
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import type { AssistantModelMessage, LanguageModel, ModelMessage, UserContent, Tool, JSONSchema7 } from 'ai';
|
|
2
|
+
import type { Subprocess } from 'bun';
|
|
3
|
+
export type Protocol = 'chat' | 'responses' | 'anthropic' | 'gemini';
|
|
4
|
+
export type ModelInput = string | LanguageModel;
|
|
5
|
+
export interface MediaSwitch {
|
|
6
|
+
image?: boolean;
|
|
7
|
+
audio?: boolean;
|
|
8
|
+
video?: boolean;
|
|
9
|
+
file?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export type ReasoningMode = 'send' | 'strip';
|
|
12
|
+
export interface ContextKeep {
|
|
13
|
+
head?: number;
|
|
14
|
+
beforeSummary?: number;
|
|
15
|
+
budget?: number;
|
|
16
|
+
}
|
|
17
|
+
export type CacheOption = boolean | {
|
|
18
|
+
key?: string;
|
|
19
|
+
retention?: string;
|
|
20
|
+
body?: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
export type RetryFilter = ((error: unknown) => boolean) | {
|
|
23
|
+
shouldRetry?: (error: unknown) => boolean;
|
|
24
|
+
skipCodes?: number[];
|
|
25
|
+
skipText?: string | RegExp;
|
|
26
|
+
skipKinds?: ErrorKind[];
|
|
27
|
+
};
|
|
28
|
+
export interface RetryOptions {
|
|
29
|
+
maxTries?: number;
|
|
30
|
+
maxElapsed?: number;
|
|
31
|
+
baseDelay?: number;
|
|
32
|
+
maxDelay?: number;
|
|
33
|
+
timeout?: number;
|
|
34
|
+
filter?: RetryFilter;
|
|
35
|
+
}
|
|
36
|
+
export interface Config {
|
|
37
|
+
model: ModelInput;
|
|
38
|
+
baseURL?: string;
|
|
39
|
+
apiKey?: string;
|
|
40
|
+
protocol?: Protocol;
|
|
41
|
+
system?: string;
|
|
42
|
+
stream?: boolean;
|
|
43
|
+
cache?: CacheOption;
|
|
44
|
+
textTools?: boolean;
|
|
45
|
+
media?: MediaSwitch;
|
|
46
|
+
includeUsage?: boolean;
|
|
47
|
+
reasoning?: ReasoningMode;
|
|
48
|
+
provider?: Record<string, unknown>;
|
|
49
|
+
maxTokens?: number;
|
|
50
|
+
maxToolOutput?: number;
|
|
51
|
+
maxContextTokens?: number;
|
|
52
|
+
compactThreshold?: number;
|
|
53
|
+
compact?: Omit<Partial<Config>, 'maxTokens'>;
|
|
54
|
+
context?: ContextKeep;
|
|
55
|
+
output?: unknown;
|
|
56
|
+
noToolRounds?: number;
|
|
57
|
+
noToolPrompt?: string;
|
|
58
|
+
retryMaxTries?: number;
|
|
59
|
+
retryMaxElapsed?: number;
|
|
60
|
+
retryBaseDelay?: number;
|
|
61
|
+
retryMaxDelay?: number;
|
|
62
|
+
requestTimeout?: number;
|
|
63
|
+
retry?: RetryFilter;
|
|
64
|
+
}
|
|
65
|
+
export interface RequestRecord {
|
|
66
|
+
model: string;
|
|
67
|
+
start: number;
|
|
68
|
+
first?: number;
|
|
69
|
+
duration: number;
|
|
70
|
+
usage?: {
|
|
71
|
+
input?: number;
|
|
72
|
+
output?: number;
|
|
73
|
+
cacheRead?: number;
|
|
74
|
+
cacheWrite?: number;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export interface CompactMessage {
|
|
78
|
+
id: string;
|
|
79
|
+
role: 'compact';
|
|
80
|
+
content: string;
|
|
81
|
+
request?: RequestRecord;
|
|
82
|
+
}
|
|
83
|
+
export interface ErrorMessage {
|
|
84
|
+
id: string;
|
|
85
|
+
role: 'error';
|
|
86
|
+
content: string;
|
|
87
|
+
request?: unknown;
|
|
88
|
+
}
|
|
89
|
+
export type AssistantMessage = AssistantModelMessage & {
|
|
90
|
+
id: string;
|
|
91
|
+
request?: RequestRecord;
|
|
92
|
+
};
|
|
93
|
+
export type Message = (Exclude<ModelMessage, AssistantModelMessage> & {
|
|
94
|
+
id: string;
|
|
95
|
+
}) | AssistantMessage | CompactMessage | ErrorMessage;
|
|
96
|
+
export type Role = Message['role'];
|
|
97
|
+
export type Draft = Message extends infer M ? (M extends Message ? Omit<M, 'id'> & {
|
|
98
|
+
id?: string;
|
|
99
|
+
} : never) : never;
|
|
100
|
+
export interface ToolDefinition {
|
|
101
|
+
name: string;
|
|
102
|
+
description: string;
|
|
103
|
+
parameters: JSONSchema7;
|
|
104
|
+
run: (args: any) => unknown | Promise<unknown>;
|
|
105
|
+
toModelOutput?: (options: {
|
|
106
|
+
output: unknown;
|
|
107
|
+
input: unknown;
|
|
108
|
+
toolCallId: string;
|
|
109
|
+
}) => unknown;
|
|
110
|
+
timeout?: number;
|
|
111
|
+
}
|
|
112
|
+
export interface ToolMeta {
|
|
113
|
+
name: string;
|
|
114
|
+
description: string;
|
|
115
|
+
parameters: JSONSchema7;
|
|
116
|
+
timeout?: number;
|
|
117
|
+
path: string;
|
|
118
|
+
mtime: number;
|
|
119
|
+
}
|
|
120
|
+
export interface MCPServer {
|
|
121
|
+
name: string;
|
|
122
|
+
url: string;
|
|
123
|
+
headers?: Record<string, string>;
|
|
124
|
+
transport?: 'http' | 'sse';
|
|
125
|
+
enabled?: boolean;
|
|
126
|
+
}
|
|
127
|
+
export interface ToolCall {
|
|
128
|
+
toolCallId: string;
|
|
129
|
+
toolName: string;
|
|
130
|
+
input: unknown;
|
|
131
|
+
invalid?: string;
|
|
132
|
+
}
|
|
133
|
+
export interface ToolResult extends ToolCall {
|
|
134
|
+
output: unknown;
|
|
135
|
+
error?: string;
|
|
136
|
+
stop?: boolean;
|
|
137
|
+
cancelled?: boolean;
|
|
138
|
+
path?: string;
|
|
139
|
+
}
|
|
140
|
+
export interface AskRequest {
|
|
141
|
+
model: ModelInput;
|
|
142
|
+
baseURL?: string;
|
|
143
|
+
apiKey?: string;
|
|
144
|
+
protocol?: Protocol;
|
|
145
|
+
system?: string;
|
|
146
|
+
messages: ModelMessage[];
|
|
147
|
+
tools?: Record<string, {
|
|
148
|
+
description: string;
|
|
149
|
+
parameters: JSONSchema7;
|
|
150
|
+
}>;
|
|
151
|
+
textTools?: boolean;
|
|
152
|
+
reasoning?: ReasoningMode;
|
|
153
|
+
includeUsage?: boolean;
|
|
154
|
+
stream?: boolean;
|
|
155
|
+
cache?: CacheOption;
|
|
156
|
+
provider?: Record<string, unknown>;
|
|
157
|
+
maxTokens?: number;
|
|
158
|
+
output?: unknown;
|
|
159
|
+
retry?: RetryOptions;
|
|
160
|
+
signal?: AbortSignal;
|
|
161
|
+
onRetry?: (info: RetryInfo) => unknown;
|
|
162
|
+
}
|
|
163
|
+
export interface RetryInfo {
|
|
164
|
+
attempt: number;
|
|
165
|
+
maxTries: number;
|
|
166
|
+
error: Error;
|
|
167
|
+
delay: number;
|
|
168
|
+
}
|
|
169
|
+
export type CompactReason = 'auto' | 'overflow' | 'manual';
|
|
170
|
+
export type RatioSource = 'usage' | 'host' | 'default';
|
|
171
|
+
export type Chunk = ({
|
|
172
|
+
type: 'text';
|
|
173
|
+
text: string;
|
|
174
|
+
} | {
|
|
175
|
+
type: 'reasoning';
|
|
176
|
+
text: string;
|
|
177
|
+
} | {
|
|
178
|
+
type: 'tool-call';
|
|
179
|
+
call: ToolCall;
|
|
180
|
+
} | {
|
|
181
|
+
type: 'tool-result';
|
|
182
|
+
result: ToolResult;
|
|
183
|
+
} | {
|
|
184
|
+
type: 'finish';
|
|
185
|
+
usage: Usage;
|
|
186
|
+
} | {
|
|
187
|
+
type: 'compact-start';
|
|
188
|
+
reason: CompactReason;
|
|
189
|
+
}) & {
|
|
190
|
+
compact?: true;
|
|
191
|
+
};
|
|
192
|
+
export interface Usage {
|
|
193
|
+
inputTokens: number;
|
|
194
|
+
outputTokens: number;
|
|
195
|
+
totalTokens: number;
|
|
196
|
+
cacheReadTokens: number;
|
|
197
|
+
cacheWriteTokens: number;
|
|
198
|
+
}
|
|
199
|
+
export interface Reply {
|
|
200
|
+
text: string;
|
|
201
|
+
reasoning: string;
|
|
202
|
+
toolCalls: ToolCall[];
|
|
203
|
+
usage: Usage;
|
|
204
|
+
request: RequestRecord;
|
|
205
|
+
output?: unknown;
|
|
206
|
+
messages: ModelMessage[];
|
|
207
|
+
}
|
|
208
|
+
export type ErrorKind = 'aborted' | 'auth' | 'limit' | 'timeout' | 'server' | 'network' | 'request' | 'overflow' | 'unknown';
|
|
209
|
+
export type Reason = 'finished' | 'no-tool' | 'tool-stop';
|
|
210
|
+
export interface Answer {
|
|
211
|
+
reason: Reason;
|
|
212
|
+
text: string;
|
|
213
|
+
steps: number;
|
|
214
|
+
usage: Usage;
|
|
215
|
+
output?: unknown;
|
|
216
|
+
}
|
|
217
|
+
export interface Stopped {
|
|
218
|
+
stopped: true;
|
|
219
|
+
wasRunning: boolean;
|
|
220
|
+
killed: number;
|
|
221
|
+
}
|
|
222
|
+
export interface MCPToggle {
|
|
223
|
+
name: string;
|
|
224
|
+
found: boolean;
|
|
225
|
+
enabled: boolean;
|
|
226
|
+
closed?: boolean;
|
|
227
|
+
connected?: boolean;
|
|
228
|
+
error?: string;
|
|
229
|
+
}
|
|
230
|
+
export interface MCP {
|
|
231
|
+
list: MCPServer[];
|
|
232
|
+
open: (name: string) => Promise<MCPToggle>;
|
|
233
|
+
close: (name: string) => Promise<MCPToggle>;
|
|
234
|
+
}
|
|
235
|
+
export type Decision = {
|
|
236
|
+
action: 'continue';
|
|
237
|
+
} | {
|
|
238
|
+
action: 'remind';
|
|
239
|
+
reminder: string;
|
|
240
|
+
} | {
|
|
241
|
+
action: 'end';
|
|
242
|
+
reason: Reason;
|
|
243
|
+
};
|
|
244
|
+
export interface Callbacks {
|
|
245
|
+
onChunk?: (chunk: Chunk) => unknown;
|
|
246
|
+
onPermission?: (call: ToolCall & {
|
|
247
|
+
path?: string;
|
|
248
|
+
}) => boolean | Promise<boolean>;
|
|
249
|
+
onInject?: (info: {
|
|
250
|
+
tokens: number;
|
|
251
|
+
budget: number;
|
|
252
|
+
compactThreshold: number;
|
|
253
|
+
ratio: number;
|
|
254
|
+
ratioSource: RatioSource;
|
|
255
|
+
}) => string | undefined | Promise<string | undefined>;
|
|
256
|
+
onToolCall?: (call: ToolCall & {
|
|
257
|
+
path?: string;
|
|
258
|
+
}) => unknown;
|
|
259
|
+
onToolOutput?: (output: {
|
|
260
|
+
toolCallId: string;
|
|
261
|
+
stream: 'stdout' | 'stderr';
|
|
262
|
+
data: string;
|
|
263
|
+
}) => unknown;
|
|
264
|
+
onToolResult?: (result: ToolResult) => unknown;
|
|
265
|
+
onStep?: (step: {
|
|
266
|
+
step: number;
|
|
267
|
+
reply: Reply;
|
|
268
|
+
results: ToolResult[];
|
|
269
|
+
}) => unknown;
|
|
270
|
+
onCompact?: (info: CompactInfo) => unknown;
|
|
271
|
+
onRetry?: (info: RetryInfo) => unknown;
|
|
272
|
+
onWarning?: (warning: {
|
|
273
|
+
path?: string;
|
|
274
|
+
message: string;
|
|
275
|
+
}) => unknown;
|
|
276
|
+
}
|
|
277
|
+
export interface CompactInfo {
|
|
278
|
+
reason: CompactReason;
|
|
279
|
+
summary: string;
|
|
280
|
+
before: number;
|
|
281
|
+
after: number;
|
|
282
|
+
budget: number;
|
|
283
|
+
compactThreshold: number;
|
|
284
|
+
ratio: number;
|
|
285
|
+
ratioSource: RatioSource;
|
|
286
|
+
chars: number;
|
|
287
|
+
media: number;
|
|
288
|
+
usage: Usage;
|
|
289
|
+
}
|
|
290
|
+
export type CreateOptions = Config & {
|
|
291
|
+
id?: string;
|
|
292
|
+
history?: Message[];
|
|
293
|
+
tools?: string[];
|
|
294
|
+
MCP?: MCPServer[];
|
|
295
|
+
contextRatio?: number;
|
|
296
|
+
ratios?: Record<string, number>;
|
|
297
|
+
};
|
|
298
|
+
export type Input = string | Exclude<UserContent, string>;
|
|
299
|
+
export interface Internal {
|
|
300
|
+
procs: Set<Subprocess>;
|
|
301
|
+
toolCache: Map<string, ToolMeta>;
|
|
302
|
+
MCPClients: Map<string, MCPClient>;
|
|
303
|
+
controller: AbortController | null;
|
|
304
|
+
running: Promise<unknown> | null;
|
|
305
|
+
warned: Set<string>;
|
|
306
|
+
}
|
|
307
|
+
export interface MCPClient {
|
|
308
|
+
key: string;
|
|
309
|
+
status: 'connecting' | 'ready' | 'failed';
|
|
310
|
+
tools: Record<string, Tool>;
|
|
311
|
+
error?: string;
|
|
312
|
+
pending: Promise<void>;
|
|
313
|
+
close: () => Promise<void>;
|
|
314
|
+
}
|
|
315
|
+
export interface State {
|
|
316
|
+
readonly id: string;
|
|
317
|
+
readonly config: Config;
|
|
318
|
+
readonly history: Message[];
|
|
319
|
+
readonly tools: string[];
|
|
320
|
+
readonly MCP: MCPServer[];
|
|
321
|
+
contextRatio: number | undefined;
|
|
322
|
+
readonly ratios: Record<string, number>;
|
|
323
|
+
readonly internal: Internal;
|
|
324
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ErrorKind } from '../types.js';
|
|
2
|
+
export declare function classify(error: unknown, timeout?: AbortSignal | null): ErrorKind;
|
|
3
|
+
export declare function explain(error: unknown, info?: Record<string, unknown>): {
|
|
4
|
+
role: 'error';
|
|
5
|
+
content: string;
|
|
6
|
+
request: Record<string, unknown>;
|
|
7
|
+
} | null;
|
|
8
|
+
export declare function cancelled(): Error;
|
|
9
|
+
export declare function tell<T>(callback: ((value: T) => unknown) | undefined, value: T): Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Config, RetryOptions } from '../types.js';
|
|
2
|
+
export declare const DEFAULT_BASE_DELAY = 5000;
|
|
3
|
+
export declare const DEFAULT_MAX_DELAY = 600000;
|
|
4
|
+
export interface Attempt {
|
|
5
|
+
attempt: number;
|
|
6
|
+
signal: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export declare function options(config: Config): RetryOptions;
|
|
9
|
+
export declare function retry<T>(fn: (attempt: Attempt) => Promise<T>, settings?: RetryOptions & {
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
onRetry?: (info: {
|
|
12
|
+
attempt: number;
|
|
13
|
+
maxTries: number;
|
|
14
|
+
error: Error;
|
|
15
|
+
delay: number;
|
|
16
|
+
}) => unknown;
|
|
17
|
+
}): Promise<T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModelInput, RatioSource, Usage } from '../types.js';
|
|
2
|
+
export declare const DEFAULT_RATIO = 0.6;
|
|
3
|
+
export declare const MEDIA_TOKENS = 1200;
|
|
4
|
+
export declare function current(ratios: Record<string, number>, model: ModelInput, seed?: number): {
|
|
5
|
+
ratio: number;
|
|
6
|
+
source: RatioSource;
|
|
7
|
+
};
|
|
8
|
+
export declare function estimate(payload: unknown, ratio: number): {
|
|
9
|
+
tokens: number;
|
|
10
|
+
chars: number;
|
|
11
|
+
media: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function calibrate(ratios: Record<string, number>, model: ModelInput, payload: unknown, real: Usage['inputTokens']): number;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kernel4632/agent-core",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "轻量的 AI Agent
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "轻量的 AI Agent 核心:给它模型地址和工具文件路径(也支持 MCP 服务),自动循环「问模型 → 执行工具 → 再问模型」。支持 OpenAI Chat / Responses、Anthropic、Gemini 四种协议,上下文压缩保留、提示词缓存,纯对话模型也能用工具。",
|
|
5
5
|
"keywords": ["agent", "ai", "llm", "tool-calling", "bun", "openai", "anthropic", "gemini", "ai-sdk"],
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Kernyr",
|
|
@@ -15,36 +15,38 @@
|
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
|
17
17
|
"main": "./dist/agent-core.js",
|
|
18
|
-
"types": "./dist/
|
|
18
|
+
"types": "./dist/types/index.d.ts",
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
|
-
"types": "./dist/
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
22
|
"default": "./dist/agent-core.js"
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
|
-
"files": ["dist/agent-core.js", "dist/
|
|
25
|
+
"files": ["dist/agent-core.js", "dist/types", "README.md", "LICENSE"],
|
|
26
26
|
"engines": {
|
|
27
|
-
"bun": ">=1.
|
|
27
|
+
"bun": ">=1.4.4"
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"test": "bun test",
|
|
31
|
-
"build": "bun scripts/build.
|
|
31
|
+
"build": "bun scripts/build.ts",
|
|
32
32
|
"prepack": "bun run build",
|
|
33
33
|
"prepublishOnly": "bun test",
|
|
34
|
-
"bump": "bun scripts/bump.
|
|
35
|
-
"release": "bun scripts/release.
|
|
34
|
+
"bump": "bun scripts/bump.ts",
|
|
35
|
+
"release": "bun scripts/release.ts",
|
|
36
|
+
"check": "tsc --noEmit -p . && bun scripts/check-imports.ts",
|
|
36
37
|
"publish:npm": "npm publish --registry=https://registry.npmjs.org --access public"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
|
-
"@ai-sdk/anthropic": "^4.0.
|
|
40
|
-
"@ai-sdk/google": "^4.0.
|
|
41
|
-
"@ai-sdk/openai": "^4.0.
|
|
42
|
-
"@ai-sdk/openai-compatible": "^3.0.
|
|
43
|
-
"ai": "^7.0.
|
|
40
|
+
"@ai-sdk/anthropic": "^4.0.102",
|
|
41
|
+
"@ai-sdk/google": "^4.0.117",
|
|
42
|
+
"@ai-sdk/openai": "^4.0.114",
|
|
43
|
+
"@ai-sdk/openai-compatible": "^3.0.90",
|
|
44
|
+
"ai": "^7.0.169",
|
|
44
45
|
"jsonrepair": "^3.15.0",
|
|
45
46
|
"nanoid": "^6.0.1",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
47
|
+
"zod": "4.6.7",
|
|
48
|
+
"@ai-sdk/mcp": "^2.0.99",
|
|
49
|
+
"@types/bun": "^1.4.4",
|
|
50
|
+
"typescript": "^7.0.2"
|
|
49
51
|
}
|
|
50
52
|
}
|