@centlu/agent-core 0.1.0 → 0.1.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/package.json +5 -1
- package/src/agent/store.ts +75 -75
- package/src/buffer.ts +77 -77
- package/src/index.ts +4 -4
- package/src/manage.ts +3 -3
- package/src/model.ts +77 -78
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@centlu/agent-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Core TypeScript types for Bun-based agents.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ilovejavac/agent-core.git"
|
|
8
|
+
},
|
|
5
9
|
"type": "module",
|
|
6
10
|
"types": "./src/index.ts",
|
|
7
11
|
"exports": {
|
package/src/agent/store.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import type {AgentEvent, Submission} from "../model.ts";
|
|
2
|
-
|
|
3
|
-
export interface HistoryStore {
|
|
4
|
-
load_history_from_store(id: string): Promise<Array<AgentEvent>>
|
|
5
|
-
append_history_to_store(event: AgentEvent): Promise<void>
|
|
6
|
-
|
|
7
|
-
/** Take the currently queued batch without waiting for new messages. */
|
|
8
|
-
recall_pending_messages(id: string): Promise<Array<Submission>>
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Wait until messages are available and take a batch. On cancellation, return
|
|
12
|
-
* an empty batch if nothing was taken, or return the batch already taken.
|
|
13
|
-
*/
|
|
14
|
-
wait_and_recall_pending_messages(id: string, signal: AbortSignal): Promise<Array<Submission>>
|
|
15
|
-
|
|
16
|
-
submit_pending_messages(submission: Submission): Promise<void>
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function createMemoryPendingStore(): Pick<
|
|
20
|
-
HistoryStore,
|
|
21
|
-
| "submit_pending_messages"
|
|
22
|
-
| "recall_pending_messages"
|
|
23
|
-
| "wait_and_recall_pending_messages"
|
|
24
|
-
> {
|
|
25
|
-
const queues = new Map<string, Submission[]>()
|
|
26
|
-
const waiters = new Map<string, (batch: Submission[]) => void>()
|
|
27
|
-
|
|
28
|
-
function take(id: string): Submission[] {
|
|
29
|
-
const batch = queues.get(id) ?? []
|
|
30
|
-
queues.delete(id)
|
|
31
|
-
return batch
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
async submit_pending_messages(submission) {
|
|
36
|
-
const id = submission.session
|
|
37
|
-
const waiter = waiters.get(id)
|
|
38
|
-
if (waiter) {
|
|
39
|
-
waiter([submission])
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const queue = queues.get(id) ?? []
|
|
44
|
-
queue.push(submission)
|
|
45
|
-
queues.set(id, queue)
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
async recall_pending_messages(id) {
|
|
49
|
-
return take(id)
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
async wait_and_recall_pending_messages(id, signal) {
|
|
53
|
-
if (signal.aborted) return []
|
|
54
|
-
|
|
55
|
-
const batch = take(id)
|
|
56
|
-
if (batch.length > 0) return batch
|
|
57
|
-
|
|
58
|
-
if (waiters.has(id)) {
|
|
59
|
-
throw new Error(`Session ${id} already has a waiting consumer`)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return new Promise<Submission[]>(resolve => {
|
|
63
|
-
const cancel = () => finish([])
|
|
64
|
-
|
|
65
|
-
function finish(messages: Submission[]) {
|
|
66
|
-
signal.removeEventListener("abort", cancel)
|
|
67
|
-
waiters.delete(id)
|
|
68
|
-
resolve(messages)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
waiters.set(id, finish)
|
|
72
|
-
signal.addEventListener("abort", cancel, { once: true })
|
|
73
|
-
})
|
|
74
|
-
},
|
|
75
|
-
}
|
|
1
|
+
import type {AgentEvent, Submission} from "../model.ts";
|
|
2
|
+
|
|
3
|
+
export interface HistoryStore {
|
|
4
|
+
load_history_from_store(id: string): Promise<Array<AgentEvent>>
|
|
5
|
+
append_history_to_store(event: AgentEvent): Promise<void>
|
|
6
|
+
|
|
7
|
+
/** Take the currently queued batch without waiting for new messages. */
|
|
8
|
+
recall_pending_messages(id: string): Promise<Array<Submission>>
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wait until messages are available and take a batch. On cancellation, return
|
|
12
|
+
* an empty batch if nothing was taken, or return the batch already taken.
|
|
13
|
+
*/
|
|
14
|
+
wait_and_recall_pending_messages(id: string, signal: AbortSignal): Promise<Array<Submission>>
|
|
15
|
+
|
|
16
|
+
submit_pending_messages(submission: Submission): Promise<void>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createMemoryPendingStore(): Pick<
|
|
20
|
+
HistoryStore,
|
|
21
|
+
| "submit_pending_messages"
|
|
22
|
+
| "recall_pending_messages"
|
|
23
|
+
| "wait_and_recall_pending_messages"
|
|
24
|
+
> {
|
|
25
|
+
const queues = new Map<string, Submission[]>()
|
|
26
|
+
const waiters = new Map<string, (batch: Submission[]) => void>()
|
|
27
|
+
|
|
28
|
+
function take(id: string): Submission[] {
|
|
29
|
+
const batch = queues.get(id) ?? []
|
|
30
|
+
queues.delete(id)
|
|
31
|
+
return batch
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
async submit_pending_messages(submission) {
|
|
36
|
+
const id = submission.session
|
|
37
|
+
const waiter = waiters.get(id)
|
|
38
|
+
if (waiter) {
|
|
39
|
+
waiter([submission])
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const queue = queues.get(id) ?? []
|
|
44
|
+
queue.push(submission)
|
|
45
|
+
queues.set(id, queue)
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async recall_pending_messages(id) {
|
|
49
|
+
return take(id)
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
async wait_and_recall_pending_messages(id, signal) {
|
|
53
|
+
if (signal.aborted) return []
|
|
54
|
+
|
|
55
|
+
const batch = take(id)
|
|
56
|
+
if (batch.length > 0) return batch
|
|
57
|
+
|
|
58
|
+
if (waiters.has(id)) {
|
|
59
|
+
throw new Error(`Session ${id} already has a waiting consumer`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return new Promise<Submission[]>(resolve => {
|
|
63
|
+
const cancel = () => finish([])
|
|
64
|
+
|
|
65
|
+
function finish(messages: Submission[]) {
|
|
66
|
+
signal.removeEventListener("abort", cancel)
|
|
67
|
+
waiters.delete(id)
|
|
68
|
+
resolve(messages)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
waiters.set(id, finish)
|
|
72
|
+
signal.addEventListener("abort", cancel, { once: true })
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
76
|
}
|
package/src/buffer.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
export function createEventSmoother(
|
|
2
|
-
write: (chunk: string) => void | Promise<void>,
|
|
3
|
-
{ intervalMs = 30, charsPerTick = 4 } = {},
|
|
4
|
-
) {
|
|
5
|
-
if (!Number.isInteger(charsPerTick) || charsPerTick < 1) {
|
|
6
|
-
throw new RangeError("charsPerTick must be a positive integer");
|
|
7
|
-
}
|
|
8
|
-
if (!Number.isInteger(intervalMs) || intervalMs < 1 || intervalMs > 2_147_483_647) {
|
|
9
|
-
throw new RangeError("intervalMs is outside the supported timer range");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
let buffer = "";
|
|
13
|
-
let ended = false;
|
|
14
|
-
let running = false;
|
|
15
|
-
let resolveDone!: () => void;
|
|
16
|
-
let rejectDone!: (error: unknown) => void;
|
|
17
|
-
|
|
18
|
-
const done = new Promise<void>((resolve, reject) => {
|
|
19
|
-
resolveDone = resolve;
|
|
20
|
-
rejectDone = reject;
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
async function drain(): Promise<void> {
|
|
24
|
-
if (running) return;
|
|
25
|
-
running = true;
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
while (buffer) {
|
|
29
|
-
await new Promise<void>((resolve) => setTimeout(resolve, intervalMs));
|
|
30
|
-
|
|
31
|
-
let end = 0;
|
|
32
|
-
let count = 0;
|
|
33
|
-
for (const char of buffer) {
|
|
34
|
-
// 保留末尾单独的高代理项,等待下一段补齐 emoji 等字符。
|
|
35
|
-
if (!ended && end + 1 === buffer.length && /[\uD800-\uDBFF]/.test(char)) {
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
end += char.length;
|
|
40
|
-
count += 1;
|
|
41
|
-
if (count === charsPerTick) break;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (end === 0) break;
|
|
45
|
-
|
|
46
|
-
const chunk = buffer.slice(0, end);
|
|
47
|
-
buffer = buffer.slice(end);
|
|
48
|
-
await write(chunk);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (ended) resolveDone();
|
|
52
|
-
} catch (error) {
|
|
53
|
-
ended = true;
|
|
54
|
-
buffer = "";
|
|
55
|
-
rejectDone(error);
|
|
56
|
-
} finally {
|
|
57
|
-
running = false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
push(chunk: string) {
|
|
63
|
-
if (ended) throw new Error("The text input is closed");
|
|
64
|
-
if (!chunk) return;
|
|
65
|
-
|
|
66
|
-
buffer += chunk;
|
|
67
|
-
void drain();
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
end() {
|
|
71
|
-
ended = true;
|
|
72
|
-
void drain();
|
|
73
|
-
return done;
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
done,
|
|
77
|
-
};
|
|
1
|
+
export function createEventSmoother(
|
|
2
|
+
write: (chunk: string) => void | Promise<void>,
|
|
3
|
+
{ intervalMs = 30, charsPerTick = 4 } = {},
|
|
4
|
+
) {
|
|
5
|
+
if (!Number.isInteger(charsPerTick) || charsPerTick < 1) {
|
|
6
|
+
throw new RangeError("charsPerTick must be a positive integer");
|
|
7
|
+
}
|
|
8
|
+
if (!Number.isInteger(intervalMs) || intervalMs < 1 || intervalMs > 2_147_483_647) {
|
|
9
|
+
throw new RangeError("intervalMs is outside the supported timer range");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let buffer = "";
|
|
13
|
+
let ended = false;
|
|
14
|
+
let running = false;
|
|
15
|
+
let resolveDone!: () => void;
|
|
16
|
+
let rejectDone!: (error: unknown) => void;
|
|
17
|
+
|
|
18
|
+
const done = new Promise<void>((resolve, reject) => {
|
|
19
|
+
resolveDone = resolve;
|
|
20
|
+
rejectDone = reject;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
async function drain(): Promise<void> {
|
|
24
|
+
if (running) return;
|
|
25
|
+
running = true;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
while (buffer) {
|
|
29
|
+
await new Promise<void>((resolve) => setTimeout(resolve, intervalMs));
|
|
30
|
+
|
|
31
|
+
let end = 0;
|
|
32
|
+
let count = 0;
|
|
33
|
+
for (const char of buffer) {
|
|
34
|
+
// 保留末尾单独的高代理项,等待下一段补齐 emoji 等字符。
|
|
35
|
+
if (!ended && end + 1 === buffer.length && /[\uD800-\uDBFF]/.test(char)) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
end += char.length;
|
|
40
|
+
count += 1;
|
|
41
|
+
if (count === charsPerTick) break;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (end === 0) break;
|
|
45
|
+
|
|
46
|
+
const chunk = buffer.slice(0, end);
|
|
47
|
+
buffer = buffer.slice(end);
|
|
48
|
+
await write(chunk);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (ended) resolveDone();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
ended = true;
|
|
54
|
+
buffer = "";
|
|
55
|
+
rejectDone(error);
|
|
56
|
+
} finally {
|
|
57
|
+
running = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
push(chunk: string) {
|
|
63
|
+
if (ended) throw new Error("The text input is closed");
|
|
64
|
+
if (!chunk) return;
|
|
65
|
+
|
|
66
|
+
buffer += chunk;
|
|
67
|
+
void drain();
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
end() {
|
|
71
|
+
ended = true;
|
|
72
|
+
void drain();
|
|
73
|
+
return done;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
done,
|
|
77
|
+
};
|
|
78
78
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './model'
|
|
2
|
-
export * from './manage'
|
|
3
|
-
export type {HistoryStore} from './agent'
|
|
4
|
-
export {AbstractAgent, type AgentCore} from './agent/core.ts'
|
|
1
|
+
export * from './model'
|
|
2
|
+
export * from './manage'
|
|
3
|
+
export type {HistoryStore} from './agent'
|
|
4
|
+
export {AbstractAgent, type AgentCore} from './agent/core.ts'
|
package/src/manage.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {AgentEventListener, AgentLife, AgentOptions, Submission} from "./model.ts"
|
|
2
|
-
import type {HistoryStore} from "./agent
|
|
2
|
+
import type {HistoryStore} from "./agent"
|
|
3
3
|
import type {AgentCore} from "./agent/core.ts"
|
|
4
|
+
import {CreateOhMyPiAgent} from "./agent/omp.ts";
|
|
4
5
|
|
|
5
6
|
type SessionState = {
|
|
6
7
|
closing: boolean
|
|
@@ -137,7 +138,6 @@ export function createManager(
|
|
|
137
138
|
listener?: AgentEventListener,
|
|
138
139
|
): SessionManager {
|
|
139
140
|
return new SessionManager(store, life, async (id, options) => {
|
|
140
|
-
const {CreateOhMyPiAgent} = await import("./agent/omp.ts")
|
|
141
141
|
return CreateOhMyPiAgent(id, options)
|
|
142
142
|
}, listener)
|
|
143
|
-
}
|
|
143
|
+
}
|
package/src/model.ts
CHANGED
|
@@ -1,78 +1,77 @@
|
|
|
1
|
-
export type UserInput = {
|
|
2
|
-
kind: "user-input"
|
|
3
|
-
|
|
4
|
-
mode: 'steer' | 'followup'
|
|
5
|
-
prompt: string
|
|
6
|
-
|
|
7
|
-
skills: {
|
|
8
|
-
name: string
|
|
9
|
-
path: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
attachments: {
|
|
13
|
-
index: number,
|
|
14
|
-
path: string,
|
|
15
|
-
detail: string
|
|
16
|
-
}[]
|
|
17
|
-
|
|
18
|
-
mentions: {
|
|
19
|
-
name: string,
|
|
20
|
-
path: string
|
|
21
|
-
}[]
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type UserInterrupt = {
|
|
25
|
-
kind: "user-interrupt"
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type UserResume = {
|
|
29
|
-
kind: "user-resume"
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type UserCompact = {
|
|
33
|
-
kind: "user-compact"
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type UserSettingOverride = {
|
|
37
|
-
kind: "user-setting-override"
|
|
38
|
-
setting: TurnConfig
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type AgentOptions = {
|
|
42
|
-
cwd: string,
|
|
43
|
-
setting: TurnConfig
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type TurnConfig = {
|
|
47
|
-
revision: number
|
|
48
|
-
provider: 'openai' | 'anthropic' | 'google' | 'grok' | 'kimi' | 'deepseek'
|
|
49
|
-
baseurl?: string
|
|
50
|
-
model: string
|
|
51
|
-
apiKey: string
|
|
52
|
-
contextWindow: number
|
|
53
|
-
thinkingLevel: 'none' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra',
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type UserOperation = UserInput | UserInterrupt | UserCompact | UserResume
|
|
57
|
-
|
|
58
|
-
export type Submission = {
|
|
59
|
-
session: string,
|
|
60
|
-
options: AgentOptions,
|
|
61
|
-
operation: UserOperation
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
1
|
+
export type UserInput = {
|
|
2
|
+
kind: "user-input"
|
|
3
|
+
|
|
4
|
+
mode: 'steer' | 'followup'
|
|
5
|
+
prompt: string
|
|
6
|
+
|
|
7
|
+
skills: {
|
|
8
|
+
name: string
|
|
9
|
+
path: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
attachments: {
|
|
13
|
+
index: number,
|
|
14
|
+
path: string,
|
|
15
|
+
detail: string
|
|
16
|
+
}[]
|
|
17
|
+
|
|
18
|
+
mentions: {
|
|
19
|
+
name: string,
|
|
20
|
+
path: string
|
|
21
|
+
}[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type UserInterrupt = {
|
|
25
|
+
kind: "user-interrupt"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type UserResume = {
|
|
29
|
+
kind: "user-resume"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type UserCompact = {
|
|
33
|
+
kind: "user-compact"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type UserSettingOverride = {
|
|
37
|
+
kind: "user-setting-override"
|
|
38
|
+
setting: TurnConfig
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type AgentOptions = {
|
|
42
|
+
cwd: string,
|
|
43
|
+
setting: TurnConfig
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type TurnConfig = {
|
|
47
|
+
revision: number
|
|
48
|
+
provider: 'openai' | 'anthropic' | 'google' | 'grok' | 'kimi' | 'deepseek'
|
|
49
|
+
baseurl?: string
|
|
50
|
+
model: string
|
|
51
|
+
apiKey: string
|
|
52
|
+
contextWindow: number
|
|
53
|
+
thinkingLevel: 'none' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type UserOperation = UserInput | UserInterrupt | UserCompact | UserResume
|
|
57
|
+
|
|
58
|
+
export type Submission = {
|
|
59
|
+
session: string,
|
|
60
|
+
options: AgentOptions,
|
|
61
|
+
operation: UserOperation
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type AgentEvent = {
|
|
65
|
+
type: string
|
|
66
|
+
usage?: any
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface AgentEventListener {
|
|
70
|
+
onEvent(evt: AgentEvent): Promise<void>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface AgentLife {
|
|
74
|
+
alive(id: string): Promise<boolean>
|
|
75
|
+
|
|
76
|
+
report(id: string): Promise<void>
|
|
77
|
+
}
|