@ixo/oracle-runtime-workers 0.19.0 → 0.20.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 +3 -3
- package/src/core/env.ts +32 -0
- package/src/core/main-agent.ts +20 -0
- package/src/core/middlewares/tool-marks.ts +2 -1
- package/src/core/prompt-composer.ts +3 -1
- package/src/do/contracts.ts +27 -0
- package/src/do/user-oracle-do.ts +330 -9
- package/src/memory/session-history-indexer.ts +3 -0
- package/src/memory/user-context-fetcher.ts +999 -0
- package/src/memory/user-context-invalidation.ts +66 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drops a room's prefetched user context (`UserContextFetcher.invalidate`)
|
|
3
|
+
* after a tool call that changed the user's memory succeeded, so the next
|
|
4
|
+
* session in the room gathers again instead of reading the pre-write answer
|
|
5
|
+
* for the rest of the cache TTL. It never changes the current thread: a
|
|
6
|
+
* thread keeps the context it got on first contact.
|
|
7
|
+
*
|
|
8
|
+
* Applied as a tool middleware to the main agent and every sub-agent; a
|
|
9
|
+
* failed call (thrown, or an error `ToolMessage`) leaves the cache alone.
|
|
10
|
+
*/
|
|
11
|
+
import { type AgentMiddleware, createMiddleware } from 'langchain';
|
|
12
|
+
import { isErrorResult } from '../core/middlewares/tool-marks';
|
|
13
|
+
import { NOOP_LOGGER } from '../core/utils';
|
|
14
|
+
import type { Logger } from '../plugin-api/types';
|
|
15
|
+
import {
|
|
16
|
+
MEMORY_ADD_MCP_NAME,
|
|
17
|
+
MEMORY_ADD_ORACLE_KNOWLEDGE_MCP_NAME,
|
|
18
|
+
MEMORY_CLEAR_MCP_NAME,
|
|
19
|
+
MEMORY_DELETE_EDGE_MCP_NAME,
|
|
20
|
+
MEMORY_DELETE_EPISODE_MCP_NAME,
|
|
21
|
+
} from '../plugins/memory/memory-tools';
|
|
22
|
+
|
|
23
|
+
/** The memory engine tools that write or delete what the prefetch reads. */
|
|
24
|
+
export const MEMORY_WRITE_TOOL_NAMES: readonly string[] = [
|
|
25
|
+
MEMORY_ADD_MCP_NAME,
|
|
26
|
+
MEMORY_ADD_ORACLE_KNOWLEDGE_MCP_NAME,
|
|
27
|
+
MEMORY_DELETE_EPISODE_MCP_NAME,
|
|
28
|
+
MEMORY_DELETE_EDGE_MCP_NAME,
|
|
29
|
+
MEMORY_CLEAR_MCP_NAME,
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
export interface UserContextInvalidationOptions {
|
|
33
|
+
/** The turn's oracle room: the prefetch cache key. */
|
|
34
|
+
roomId: string;
|
|
35
|
+
invalidate: (roomId: string) => void;
|
|
36
|
+
/** Tool names that invalidate (default `MEMORY_WRITE_TOOL_NAMES`). */
|
|
37
|
+
toolNames?: Iterable<string>;
|
|
38
|
+
logger?: Logger;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createUserContextInvalidationMiddleware(
|
|
42
|
+
options: UserContextInvalidationOptions,
|
|
43
|
+
): AgentMiddleware {
|
|
44
|
+
const names = new Set(options.toolNames ?? MEMORY_WRITE_TOOL_NAMES);
|
|
45
|
+
const logger = options.logger ?? NOOP_LOGGER;
|
|
46
|
+
return createMiddleware({
|
|
47
|
+
name: 'UserContextInvalidation',
|
|
48
|
+
wrapToolCall: async (request, handler) => {
|
|
49
|
+
const output = await handler(request);
|
|
50
|
+
const toolName = request.toolCall.name;
|
|
51
|
+
if (names.has(toolName) && !isErrorResult(output)) {
|
|
52
|
+
try {
|
|
53
|
+
options.invalidate(options.roomId);
|
|
54
|
+
logger.log(
|
|
55
|
+
`[user-context] invalidated room=${options.roomId} after ${toolName}`,
|
|
56
|
+
);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
logger.warn(
|
|
59
|
+
`[user-context] could not invalidate room=${options.roomId}: ${err instanceof Error ? err.message : String(err)}`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return output;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|