Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/chat-sdk/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class MemoryStateAdapter implements StateAdapter {
if (existingTimeout)
clearTimeout(existingTimeout)

// eslint-disable-next-line @masknet/prefer-timer-id
const timeout = setTimeout(() => {
this.lists.delete(key)
this.listTimeouts.delete(key)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface ResponseOptions<T = unknown> extends PluginHookBase<T> {
export interface SessionInitOptions<T = unknown> {
agentName: string
context: AgentContext<T>
send?: (input: ItemParam) => string
sessionId: string
}

Expand Down
33 changes: 18 additions & 15 deletions packages/core/src/utils/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,26 +185,20 @@ export const createAgent = <T = unknown>(options: CreateAgentOptions<T>): Agent<
const resolveContext = (runContext?: Partial<AgentContext<T>>): AgentContext<T> =>
merge(merge(context, currentSessionContext), runContext)

const createSessionOptions = (): SessionInitOptions<T> => ({
let sessionReady: Promise<void> | undefined

const sessionCallbacks = {
ensureSessionReady: async (): Promise<void> => {},
}

const createSessionOptions = (send: (input: { context?: Partial<AgentContext<T>>, input: ItemParam, signal?: AbortSignal }) => string): SessionInitOptions<T> => ({
agentName: options.name,
context: resolveContext(),
send: input => send({ input }),
sessionId: id,
})

let sessionReady: Promise<void> | undefined

const ensureSessionReady = async () => {
sessionReady ??= ready.then(async () => {
for (const plugin of plugins)
await plugin.onSessionInit?.(createSessionOptions())
})

return sessionReady
}

const loadSession = async (): Promise<SessionState<T> | undefined> => {
await ensureSessionReady()

for (const plugin of plugins) {
if (plugin.storage == null)
continue
Expand Down Expand Up @@ -242,13 +236,22 @@ export const createAgent = <T = unknown>(options: CreateAgentOptions<T>): Agent<
await plugin.onTurnDone?.(turnContext)
},
plugins,
ready: async () => ensureSessionReady(),
ready: async () => sessionCallbacks.ensureSessionReady(),
responseOptions: options.options,
saveSession,
sessionContext: initialSessionContext,
sessionId: id,
})

sessionCallbacks.ensureSessionReady = async () => {
sessionReady ??= ready.then(async () => {
for (const plugin of plugins)
await plugin.onSessionInit?.(createSessionOptions(runtime.send))
})

return sessionReady
}

const subscribeSession = (channel: string, listener: PluginChannelListener) => {
const register = () => {
if (channel === 'apeira') {
Expand Down
54 changes: 54 additions & 0 deletions packages/plugin-proactive/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@apeira/plugin-proactive",
"type": "module",
"version": "0.0.4",
"description": "Proactive agent behavior with DMN, scheduler, and autonomous ticks.",
"author": "Moeru AI",
"license": "MIT",
"homepage": "https://github.com/moeru-ai/apeira",
"repository": {
"type": "git",
"url": "git+https://github.com/moeru-ai/apeira.git",
"directory": "packages/plugin-proactive"
},
"bugs": "https://github.com/moeru-ai/apeira/issues",
"keywords": [
"agent",
"ai",
"xsai",
"proactive"
],
"sideEffects": false,
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
},
"files": [
"dist"
],
"publishConfig": {
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"scripts": {
"build": "pkgroll",
"test": "vitest run"
},
"peerDependencies": {
"@apeira/core": "workspace:"
},
"dependencies": {
"@xsai/tool": "catalog:xsai",
"zod": "catalog:"
},
"devDependencies": {
"@apeira/core": "workspace:"
}
}
52 changes: 52 additions & 0 deletions packages/plugin-proactive/src/dmn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export type DmnState = 'foraging' | 'paused' | 'resting' | 'working'

export const DMN_TICK_INTERVALS: Record<DmnState, null | number> = {
foraging: 30000,
paused: null,
resting: 300000,
working: 3000,
}

export interface DmnContext {
lastTickAt: number
lastUserInputAt: number
state: DmnState
}

export const createDmnContext = (): DmnContext => ({
lastTickAt: 0,
lastUserInputAt: 0,
state: 'resting',
})

/**
* Compute next DMN state based on turn result
* - tool calls made → working
* - no tool calls → decays from working → foraging → resting
* - paused stays unchanged
*/
export const nextDmnState = (
current: DmnContext,
hasToolCalls: boolean,
modelSlept: boolean,
): DmnState => {
if (current.state === 'paused')
return 'paused'

if (modelSlept)
return 'resting'

if (hasToolCalls)
return 'working'

if (current.state === 'working')
return 'foraging'

if (current.state === 'foraging')
return 'resting'

return 'resting'
}

export const shouldSkipTick = (ctx: DmnContext): boolean =>
Date.now() - ctx.lastUserInputAt < 5000
Loading
Loading