agent-world 0.11.1 → 0.12.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 +17 -7
- package/dist/cli/commands.d.ts +109 -0
- package/dist/cli/commands.js +2024 -0
- package/dist/cli/display.d.ts +124 -0
- package/dist/cli/display.js +381 -0
- package/dist/cli/hitl.d.ts +33 -0
- package/dist/cli/hitl.js +81 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/stream.d.ts +41 -0
- package/dist/cli/stream.js +222 -0
- package/dist/core/activity-tracker.d.ts +16 -0
- package/dist/core/activity-tracker.d.ts.map +1 -0
- package/dist/core/activity-tracker.js +91 -0
- package/dist/core/activity-tracker.js.map +1 -0
- package/dist/core/ai-commands.d.ts +16 -0
- package/dist/core/ai-commands.d.ts.map +1 -0
- package/dist/core/ai-commands.js +24 -0
- package/dist/core/ai-commands.js.map +1 -0
- package/dist/core/ai-sdk-patch.d.ts +24 -0
- package/dist/core/ai-sdk-patch.d.ts.map +1 -0
- package/dist/core/ai-sdk-patch.js +169 -0
- package/dist/core/ai-sdk-patch.js.map +1 -0
- package/dist/core/anthropic-direct.d.ts +52 -0
- package/dist/core/anthropic-direct.d.ts.map +1 -0
- package/dist/core/anthropic-direct.js +301 -0
- package/dist/core/anthropic-direct.js.map +1 -0
- package/dist/core/approval-cache.d.ts +104 -0
- package/dist/core/approval-cache.d.ts.map +1 -0
- package/dist/core/approval-cache.js +150 -0
- package/dist/core/approval-cache.js.map +1 -0
- package/dist/core/chat-constants.d.ts +20 -0
- package/dist/core/chat-constants.d.ts.map +1 -0
- package/dist/core/chat-constants.js +22 -0
- package/dist/core/chat-constants.js.map +1 -0
- package/dist/core/create-agent-tool.d.ts +66 -0
- package/dist/core/create-agent-tool.d.ts.map +1 -0
- package/dist/core/create-agent-tool.js +212 -0
- package/dist/core/create-agent-tool.js.map +1 -0
- package/dist/core/events/approval-checker.d.ts +61 -0
- package/dist/core/events/approval-checker.d.ts.map +1 -0
- package/dist/core/events/approval-checker.js +226 -0
- package/dist/core/events/approval-checker.js.map +1 -0
- package/dist/core/events/index.d.ts +25 -0
- package/dist/core/events/index.d.ts.map +1 -0
- package/dist/core/events/index.js +30 -0
- package/dist/core/events/index.js.map +1 -0
- package/dist/core/events/memory-manager.d.ts +73 -0
- package/dist/core/events/memory-manager.d.ts.map +1 -0
- package/dist/core/events/memory-manager.js +1218 -0
- package/dist/core/events/memory-manager.js.map +1 -0
- package/dist/core/events/mention-logic.d.ts +39 -0
- package/dist/core/events/mention-logic.d.ts.map +1 -0
- package/dist/core/events/mention-logic.js +163 -0
- package/dist/core/events/mention-logic.js.map +1 -0
- package/dist/core/events/orchestrator.d.ts +69 -0
- package/dist/core/events/orchestrator.d.ts.map +1 -0
- package/dist/core/events/orchestrator.js +883 -0
- package/dist/core/events/orchestrator.js.map +1 -0
- package/dist/core/events/persistence.d.ts +41 -0
- package/dist/core/events/persistence.d.ts.map +1 -0
- package/dist/core/events/persistence.js +296 -0
- package/dist/core/events/persistence.js.map +1 -0
- package/dist/core/events/publishers.d.ts +81 -0
- package/dist/core/events/publishers.d.ts.map +1 -0
- package/dist/core/events/publishers.js +272 -0
- package/dist/core/events/publishers.js.map +1 -0
- package/dist/core/events/subscribers.d.ts +45 -0
- package/dist/core/events/subscribers.d.ts.map +1 -0
- package/dist/core/events/subscribers.js +288 -0
- package/dist/core/events/subscribers.js.map +1 -0
- package/dist/core/events/tool-bridge-logging.d.ts +28 -0
- package/dist/core/events/tool-bridge-logging.d.ts.map +1 -0
- package/dist/core/events/tool-bridge-logging.js +94 -0
- package/dist/core/events/tool-bridge-logging.js.map +1 -0
- package/dist/core/events-metadata.d.ts +72 -0
- package/dist/core/events-metadata.d.ts.map +1 -0
- package/dist/core/events-metadata.js +167 -0
- package/dist/core/events-metadata.js.map +1 -0
- package/dist/core/events.d.ts +186 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/events.js +1248 -0
- package/dist/core/events.js.map +1 -0
- package/dist/core/export.d.ts +106 -0
- package/dist/core/export.d.ts.map +1 -0
- package/dist/core/export.js +705 -0
- package/dist/core/export.js.map +1 -0
- package/dist/core/file-tools.d.ts +114 -0
- package/dist/core/file-tools.d.ts.map +1 -0
- package/dist/core/file-tools.js +370 -0
- package/dist/core/file-tools.js.map +1 -0
- package/dist/core/google-direct.d.ts +58 -0
- package/dist/core/google-direct.d.ts.map +1 -0
- package/dist/core/google-direct.js +298 -0
- package/dist/core/google-direct.js.map +1 -0
- package/dist/core/hitl.d.ts +54 -0
- package/dist/core/hitl.d.ts.map +1 -0
- package/dist/core/hitl.js +153 -0
- package/dist/core/hitl.js.map +1 -0
- package/dist/core/index.d.ts +59 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +70 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/llm-config.d.ts +128 -0
- package/dist/core/llm-config.d.ts.map +1 -0
- package/dist/core/llm-config.js +164 -0
- package/dist/core/llm-config.js.map +1 -0
- package/dist/core/llm-manager.d.ts +163 -0
- package/dist/core/llm-manager.d.ts.map +1 -0
- package/dist/core/llm-manager.js +669 -0
- package/dist/core/llm-manager.js.map +1 -0
- package/dist/core/load-skill-tool.d.ts +55 -0
- package/dist/core/load-skill-tool.d.ts.map +1 -0
- package/dist/core/load-skill-tool.js +468 -0
- package/dist/core/load-skill-tool.js.map +1 -0
- package/dist/core/logger.d.ts +88 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +358 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/managers.d.ts +131 -0
- package/dist/core/managers.d.ts.map +1 -0
- package/dist/core/managers.js +1223 -0
- package/dist/core/managers.js.map +1 -0
- package/dist/core/mcp-server-registry.d.ts +304 -0
- package/dist/core/mcp-server-registry.d.ts.map +1 -0
- package/dist/core/mcp-server-registry.js +1769 -0
- package/dist/core/mcp-server-registry.js.map +1 -0
- package/dist/core/mcp-tools.d.ts +56 -0
- package/dist/core/mcp-tools.d.ts.map +1 -0
- package/dist/core/mcp-tools.js +186 -0
- package/dist/core/mcp-tools.js.map +1 -0
- package/dist/core/message-prep.d.ts +81 -0
- package/dist/core/message-prep.d.ts.map +1 -0
- package/dist/core/message-prep.js +223 -0
- package/dist/core/message-prep.js.map +1 -0
- package/dist/core/message-processing-control.d.ts +54 -0
- package/dist/core/message-processing-control.d.ts.map +1 -0
- package/dist/core/message-processing-control.js +139 -0
- package/dist/core/message-processing-control.js.map +1 -0
- package/dist/core/openai-direct.d.ts +80 -0
- package/dist/core/openai-direct.d.ts.map +1 -0
- package/dist/core/openai-direct.js +374 -0
- package/dist/core/openai-direct.js.map +1 -0
- package/dist/core/shell-cmd-tool.d.ts +235 -0
- package/dist/core/shell-cmd-tool.d.ts.map +1 -0
- package/dist/core/shell-cmd-tool.js +1157 -0
- package/dist/core/shell-cmd-tool.js.map +1 -0
- package/dist/core/shell-process-registry.d.ts +88 -0
- package/dist/core/shell-process-registry.d.ts.map +1 -0
- package/dist/core/shell-process-registry.js +309 -0
- package/dist/core/shell-process-registry.js.map +1 -0
- package/dist/core/skill-registry.d.ts +75 -0
- package/dist/core/skill-registry.d.ts.map +1 -0
- package/dist/core/skill-registry.js +369 -0
- package/dist/core/skill-registry.js.map +1 -0
- package/dist/core/skill-script-runner.d.ts +89 -0
- package/dist/core/skill-script-runner.d.ts.map +1 -0
- package/dist/core/skill-script-runner.js +274 -0
- package/dist/core/skill-script-runner.js.map +1 -0
- package/dist/core/skill-selector.d.ts +65 -0
- package/dist/core/skill-selector.d.ts.map +1 -0
- package/dist/core/skill-selector.js +190 -0
- package/dist/core/skill-selector.js.map +1 -0
- package/dist/core/skill-settings.d.ts +20 -0
- package/dist/core/skill-settings.d.ts.map +1 -0
- package/dist/core/skill-settings.js +40 -0
- package/dist/core/skill-settings.js.map +1 -0
- package/dist/core/storage/agent-storage.d.ts +134 -0
- package/dist/core/storage/agent-storage.d.ts.map +1 -0
- package/dist/core/storage/agent-storage.js +498 -0
- package/dist/core/storage/agent-storage.js.map +1 -0
- package/dist/core/storage/eventStorage/fileEventStorage.d.ts +100 -0
- package/dist/core/storage/eventStorage/fileEventStorage.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/fileEventStorage.js +494 -0
- package/dist/core/storage/eventStorage/fileEventStorage.js.map +1 -0
- package/dist/core/storage/eventStorage/index.d.ts +31 -0
- package/dist/core/storage/eventStorage/index.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/index.js +31 -0
- package/dist/core/storage/eventStorage/index.js.map +1 -0
- package/dist/core/storage/eventStorage/memoryEventStorage.d.ts +87 -0
- package/dist/core/storage/eventStorage/memoryEventStorage.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/memoryEventStorage.js +244 -0
- package/dist/core/storage/eventStorage/memoryEventStorage.js.map +1 -0
- package/dist/core/storage/eventStorage/sqliteEventStorage.d.ts +45 -0
- package/dist/core/storage/eventStorage/sqliteEventStorage.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/sqliteEventStorage.js +301 -0
- package/dist/core/storage/eventStorage/sqliteEventStorage.js.map +1 -0
- package/dist/core/storage/eventStorage/types.d.ts +142 -0
- package/dist/core/storage/eventStorage/types.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/types.js +43 -0
- package/dist/core/storage/eventStorage/types.js.map +1 -0
- package/dist/core/storage/eventStorage/validation.d.ts +30 -0
- package/dist/core/storage/eventStorage/validation.d.ts.map +1 -0
- package/dist/core/storage/eventStorage/validation.js +68 -0
- package/dist/core/storage/eventStorage/validation.js.map +1 -0
- package/dist/core/storage/legacy-migrations.d.ts +45 -0
- package/dist/core/storage/legacy-migrations.d.ts.map +1 -0
- package/dist/core/storage/legacy-migrations.js +295 -0
- package/dist/core/storage/legacy-migrations.js.map +1 -0
- package/dist/core/storage/memory-storage.d.ts +105 -0
- package/dist/core/storage/memory-storage.d.ts.map +1 -0
- package/dist/core/storage/memory-storage.js +415 -0
- package/dist/core/storage/memory-storage.js.map +1 -0
- package/dist/core/storage/migration-runner.d.ts +96 -0
- package/dist/core/storage/migration-runner.d.ts.map +1 -0
- package/dist/core/storage/migration-runner.js +306 -0
- package/dist/core/storage/migration-runner.js.map +1 -0
- package/dist/core/storage/queue-storage.d.ts +147 -0
- package/dist/core/storage/queue-storage.d.ts.map +1 -0
- package/dist/core/storage/queue-storage.js +290 -0
- package/dist/core/storage/queue-storage.js.map +1 -0
- package/dist/core/storage/skill-storage.d.ts +136 -0
- package/dist/core/storage/skill-storage.d.ts.map +1 -0
- package/dist/core/storage/skill-storage.js +474 -0
- package/dist/core/storage/skill-storage.js.map +1 -0
- package/dist/core/storage/sqlite-schema.d.ts +95 -0
- package/dist/core/storage/sqlite-schema.d.ts.map +1 -0
- package/dist/core/storage/sqlite-schema.js +156 -0
- package/dist/core/storage/sqlite-schema.js.map +1 -0
- package/dist/core/storage/sqlite-storage.d.ts +146 -0
- package/dist/core/storage/sqlite-storage.d.ts.map +1 -0
- package/dist/core/storage/sqlite-storage.js +709 -0
- package/dist/core/storage/sqlite-storage.js.map +1 -0
- package/dist/core/storage/storage-factory.d.ts +61 -0
- package/dist/core/storage/storage-factory.d.ts.map +1 -0
- package/dist/core/storage/storage-factory.js +794 -0
- package/dist/core/storage/storage-factory.js.map +1 -0
- package/dist/core/storage/validation.d.ts +36 -0
- package/dist/core/storage/validation.d.ts.map +1 -0
- package/dist/core/storage/validation.js +79 -0
- package/dist/core/storage/validation.js.map +1 -0
- package/dist/core/storage/world-storage.d.ts +114 -0
- package/dist/core/storage/world-storage.d.ts.map +1 -0
- package/dist/core/storage/world-storage.js +378 -0
- package/dist/core/storage/world-storage.js.map +1 -0
- package/dist/core/subscription.d.ts +43 -0
- package/dist/core/subscription.d.ts.map +1 -0
- package/dist/core/subscription.js +227 -0
- package/dist/core/subscription.js.map +1 -0
- package/dist/core/tool-utils.d.ts +80 -0
- package/dist/core/tool-utils.d.ts.map +1 -0
- package/dist/core/tool-utils.js +273 -0
- package/dist/core/tool-utils.js.map +1 -0
- package/dist/core/types.d.ts +595 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +158 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/utils.d.ts +138 -0
- package/dist/core/utils.d.ts.map +1 -0
- package/dist/core/utils.js +478 -0
- package/dist/core/utils.js.map +1 -0
- package/dist/core/world-class.d.ts +43 -0
- package/dist/core/world-class.d.ts.map +1 -0
- package/dist/core/world-class.js +90 -0
- package/dist/core/world-class.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/public/assets/agent-sprites-DJFgj-zP.png +0 -0
- package/dist/public/assets/border-KHK37r8y.svg +83 -0
- package/dist/public/assets/index-C9kPXL6G.css +1 -0
- package/dist/public/assets/index-DOQEHGWt.js +96 -0
- package/dist/public/index.html +21 -0
- package/dist/server/api.d.ts +2 -0
- package/dist/server/api.js +1124 -0
- package/dist/server/index.d.ts +29 -0
- package/dist/server/sse-handler.d.ts +62 -0
- package/dist/server/sse-handler.js +234 -0
- package/package.json +15 -3
- package/scripts/launch-electron.js +0 -58
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message Preparation Utilities for LLM Processing
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Filters client.* tool calls from messages prepared for LLM input
|
|
6
|
+
* - Two-layer architecture: storage (agent.memory) vs processing (LLM input)
|
|
7
|
+
* - Maintains complete message history while providing clean LLM context
|
|
8
|
+
* - Enhanced string protocol: Parses JSON strings with __type markers into OpenAI format
|
|
9
|
+
*
|
|
10
|
+
* Implementation:
|
|
11
|
+
* - Removes client.* tool calls from LLM context
|
|
12
|
+
* - No side-effect state updates while preparing messages
|
|
13
|
+
* - parseMessageContent() converts enhanced string format to OpenAI ChatMessage
|
|
14
|
+
* - Used by utils.ts prepareMessagesForLLM() - high-level function applies this filter
|
|
15
|
+
* - NOT used directly by llm-manager.ts (receives pre-filtered messages from utils.ts)
|
|
16
|
+
*
|
|
17
|
+
* Changes:
|
|
18
|
+
* - 2026-02-11: Added unresolved-tool-call cleanup.
|
|
19
|
+
* - Assistant `tool_calls` are pruned to only IDs that have matching tool-result messages.
|
|
20
|
+
* - Prevents OpenAI 400 errors from legacy/incomplete tool-call history.
|
|
21
|
+
* - 2026-02-08: Removed stale manual tool-intervention terminology from message prep docs
|
|
22
|
+
* - 2026-02-08: Fixed OpenAI API validation error - now filters orphaned tool messages
|
|
23
|
+
* - Tool messages referencing removed client.* tool_call_ids are now properly filtered
|
|
24
|
+
* - Tracks removed tool_call_ids to prevent "tool must follow tool_calls" errors
|
|
25
|
+
* - 2025-11-04: Simplified from message-filter.ts, removed legacy tool decision cache logic
|
|
26
|
+
* - 2025-11-06: Added parseMessageContent() for enhanced string protocol support
|
|
27
|
+
* - 2025-11-06: Consolidated with utils.ts - renamed to filterClientSideMessages, added alias
|
|
28
|
+
*/
|
|
29
|
+
import { createCategoryLogger } from './logger.js';
|
|
30
|
+
const logger = createCategoryLogger('llm.message-prep');
|
|
31
|
+
/**
|
|
32
|
+
* Parse message content to detect enhanced string format and convert to OpenAI ChatMessage.
|
|
33
|
+
*
|
|
34
|
+
* Enhanced String Protocol:
|
|
35
|
+
* - Transport Layer: JSON strings with __type markers (e.g., {"__type": "tool_result", ...})
|
|
36
|
+
* - Storage Layer: OpenAI ChatMessage format (e.g., {role: "tool", tool_call_id: "...", ...})
|
|
37
|
+
*
|
|
38
|
+
* Supported __type values:
|
|
39
|
+
* - "tool_result": Converts to OpenAI tool message with role: "tool"
|
|
40
|
+
*
|
|
41
|
+
* AgentId Handling:
|
|
42
|
+
* - If agentId is present in JSON, returns { message, targetAgentId }
|
|
43
|
+
* - Caller should prepend @mention before publishing
|
|
44
|
+
*
|
|
45
|
+
* Backward Compatibility:
|
|
46
|
+
* - Regular text strings pass through unchanged
|
|
47
|
+
* - Invalid JSON strings pass through unchanged
|
|
48
|
+
* - Missing __type passes through unchanged
|
|
49
|
+
*
|
|
50
|
+
* @param content - String content (may be plain text or JSON with __type marker)
|
|
51
|
+
* @param defaultRole - Role to use if content is not enhanced format (default: "user")
|
|
52
|
+
* @returns Object with ChatMessage and optional targetAgentId
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* // Tool result with agentId (enhanced format)
|
|
56
|
+
* parseMessageContent('{"__type":"tool_result","tool_call_id":"call_123","agentId":"a1","content":"..."}')
|
|
57
|
+
* // → {message: {role: "tool", ...}, targetAgentId: "a1"}
|
|
58
|
+
*
|
|
59
|
+
* // Regular text (backward compatible)
|
|
60
|
+
* parseMessageContent("Hello world")
|
|
61
|
+
* // → {message: {role: "user", content: "Hello world", ...}, targetAgentId: undefined}
|
|
62
|
+
*/
|
|
63
|
+
export function parseMessageContent(content, defaultRole = 'user') {
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(content);
|
|
66
|
+
// Enhanced format: tool_result
|
|
67
|
+
if (parsed.__type === 'tool_result') {
|
|
68
|
+
if (!parsed.tool_call_id) {
|
|
69
|
+
logger.warn('Enhanced format missing tool_call_id, falling back to default role', {
|
|
70
|
+
parsed
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
message: {
|
|
74
|
+
role: defaultRole,
|
|
75
|
+
content: content,
|
|
76
|
+
createdAt: new Date()
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
logger.debug('Parsed enhanced tool_result format', {
|
|
81
|
+
toolCallId: parsed.tool_call_id,
|
|
82
|
+
agentId: parsed.agentId,
|
|
83
|
+
contentLength: parsed.content?.length || 0
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
message: {
|
|
87
|
+
role: 'tool',
|
|
88
|
+
tool_call_id: parsed.tool_call_id,
|
|
89
|
+
content: parsed.content || '',
|
|
90
|
+
createdAt: new Date()
|
|
91
|
+
},
|
|
92
|
+
targetAgentId: parsed.agentId // Extract agentId for @mention routing
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// JSON without __type marker - treat as regular text
|
|
96
|
+
logger.debug('JSON without __type marker, treating as regular content');
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// Not JSON - regular text
|
|
100
|
+
logger.debug('Non-JSON content, using default role', { role: defaultRole });
|
|
101
|
+
}
|
|
102
|
+
// Default: regular text message
|
|
103
|
+
return {
|
|
104
|
+
message: {
|
|
105
|
+
role: defaultRole,
|
|
106
|
+
content: content,
|
|
107
|
+
createdAt: new Date()
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Filter client-side messages and tool calls from message array.
|
|
113
|
+
*
|
|
114
|
+
* This function creates a clean copy of messages suitable for LLM processing:
|
|
115
|
+
* - Removes messages marked as clientOnly
|
|
116
|
+
* - Removes client.* tool calls from assistant messages
|
|
117
|
+
* - Removes orphaned tool messages (those referencing removed client.* tool calls)
|
|
118
|
+
* - Removes orphaned tool messages with missing tool_call_ids (invalid data)
|
|
119
|
+
* - Removes tool messages that don't have a preceding assistant message with matching tool_call
|
|
120
|
+
* - Drops messages that become empty after filtering
|
|
121
|
+
*
|
|
122
|
+
* @param messages - All messages from agent memory
|
|
123
|
+
* @returns Filtered messages ready for LLM
|
|
124
|
+
*/
|
|
125
|
+
export function filterClientSideMessages(messages) {
|
|
126
|
+
const prepared = [];
|
|
127
|
+
const removedToolCallIds = new Set();
|
|
128
|
+
const validToolCallIds = new Set();
|
|
129
|
+
// First pass: Filter assistant messages and track tool_call_ids
|
|
130
|
+
for (const message of messages) {
|
|
131
|
+
// Deep clone to avoid mutating original
|
|
132
|
+
const clonedMessage = {
|
|
133
|
+
...message,
|
|
134
|
+
tool_calls: message.tool_calls
|
|
135
|
+
? message.tool_calls.map(toolCall => ({
|
|
136
|
+
...toolCall,
|
|
137
|
+
function: { ...toolCall.function }
|
|
138
|
+
}))
|
|
139
|
+
: undefined
|
|
140
|
+
};
|
|
141
|
+
// Filter assistant messages with tool calls
|
|
142
|
+
if (clonedMessage.role === 'assistant' && clonedMessage.tool_calls?.length) {
|
|
143
|
+
// Track which tool_call_ids we're removing
|
|
144
|
+
const removedCalls = clonedMessage.tool_calls.filter(toolCall => toolCall.function.name.startsWith('client.'));
|
|
145
|
+
removedCalls.forEach(tc => removedToolCallIds.add(tc.id));
|
|
146
|
+
const filteredToolCalls = clonedMessage.tool_calls.filter(toolCall => !toolCall.function.name.startsWith('client.'));
|
|
147
|
+
// Track valid tool_call_ids (non-client.*)
|
|
148
|
+
filteredToolCalls.forEach(tc => validToolCallIds.add(tc.id));
|
|
149
|
+
// If all tool calls were client.* and no content, skip this message
|
|
150
|
+
if (filteredToolCalls.length === 0 && !clonedMessage.content) {
|
|
151
|
+
logger.debug('Dropping assistant message with only client.* tool calls', {
|
|
152
|
+
droppedToolCalls: clonedMessage.tool_calls.map(tc => tc.function.name),
|
|
153
|
+
droppedToolCallIds: Array.from(removedToolCallIds)
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
clonedMessage.tool_calls = filteredToolCalls;
|
|
158
|
+
}
|
|
159
|
+
// Filter tool messages
|
|
160
|
+
if (clonedMessage.role === 'tool') {
|
|
161
|
+
// Drop tool messages without tool_call_id (invalid data)
|
|
162
|
+
if (!clonedMessage.tool_call_id) {
|
|
163
|
+
logger.debug('Dropping tool message without tool_call_id (invalid data)');
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
// Drop tool messages referencing removed client.* tool calls
|
|
167
|
+
if (removedToolCallIds.has(clonedMessage.tool_call_id)) {
|
|
168
|
+
logger.debug('Dropping orphaned tool message for removed client.* tool call', {
|
|
169
|
+
toolCallId: clonedMessage.tool_call_id
|
|
170
|
+
});
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
// Drop tool messages that don't have a valid preceding tool_call
|
|
174
|
+
// This handles legacy data where tool_calls weren't properly saved
|
|
175
|
+
if (!validToolCallIds.has(clonedMessage.tool_call_id)) {
|
|
176
|
+
logger.debug('Dropping tool message with no matching tool_call (legacy data)', {
|
|
177
|
+
toolCallId: clonedMessage.tool_call_id
|
|
178
|
+
});
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
prepared.push(clonedMessage);
|
|
183
|
+
}
|
|
184
|
+
const answeredToolCallIds = new Set();
|
|
185
|
+
for (const message of prepared) {
|
|
186
|
+
if (message.role === 'tool' && message.tool_call_id) {
|
|
187
|
+
answeredToolCallIds.add(message.tool_call_id);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const finalized = [];
|
|
191
|
+
for (const message of prepared) {
|
|
192
|
+
if (message.role !== 'assistant' || !message.tool_calls?.length) {
|
|
193
|
+
finalized.push(message);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const resolvedToolCalls = message.tool_calls.filter((toolCall) => answeredToolCallIds.has(toolCall.id));
|
|
197
|
+
if (resolvedToolCalls.length === message.tool_calls.length) {
|
|
198
|
+
finalized.push(message);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
logger.debug('Pruning unresolved assistant tool_calls from message history', {
|
|
202
|
+
removedCount: message.tool_calls.length - resolvedToolCalls.length,
|
|
203
|
+
removedToolCallIds: message.tool_calls
|
|
204
|
+
.filter((toolCall) => !answeredToolCallIds.has(toolCall.id))
|
|
205
|
+
.map((toolCall) => toolCall.id)
|
|
206
|
+
});
|
|
207
|
+
if (resolvedToolCalls.length === 0 && !message.content) {
|
|
208
|
+
logger.debug('Dropping assistant message with only unresolved tool_calls');
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
finalized.push({
|
|
212
|
+
...message,
|
|
213
|
+
tool_calls: resolvedToolCalls
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
logger.debug(`Prepared ${finalized.length}/${messages.length} messages for LLM consumption`, {
|
|
217
|
+
removedToolCallIds: Array.from(removedToolCallIds),
|
|
218
|
+
validToolCallIds: Array.from(validToolCallIds),
|
|
219
|
+
answeredToolCallIds: Array.from(answeredToolCallIds)
|
|
220
|
+
});
|
|
221
|
+
return finalized;
|
|
222
|
+
}
|
|
223
|
+
//# sourceMappingURL=message-prep.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-prep.js","sourceRoot":"","sources":["../../core/message-prep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGnD,MAAM,MAAM,GAAG,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,mBAAmB,CACjC,OAAe,EACf,cAAoC,MAAM;IAE1C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEnC,+BAA+B;QAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,aAAa,EAAE,CAAC;YACpC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC;gBACzB,MAAM,CAAC,IAAI,CAAC,oEAAoE,EAAE;oBAChF,MAAM;iBACP,CAAC,CAAC;gBACH,OAAO;oBACL,OAAO,EAAE;wBACP,IAAI,EAAE,WAAW;wBACjB,OAAO,EAAE,OAAO;wBAChB,SAAS,EAAE,IAAI,IAAI,EAAE;qBACtB;iBACF,CAAC;YACJ,CAAC;YAED,MAAM,CAAC,KAAK,CAAC,oCAAoC,EAAE;gBACjD,UAAU,EAAE,MAAM,CAAC,YAAY;gBAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,aAAa,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC;aAC3C,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO,EAAE;oBACP,IAAI,EAAE,MAAM;oBACZ,YAAY,EAAE,MAAM,CAAC,YAAY;oBACjC,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,EAAE;oBAC7B,SAAS,EAAE,IAAI,IAAI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,CAAC;YACxB,SAAS;QACX,CAAC;QAED,MAAM,CAAC,KAAK,CAAC,8DAA8D,EAAE;YAC3E,YAAY,EAAE,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,iBAAiB,CAAC,MAAM;YAClE,kBAAkB,EAAE,OAAO,CAAC,UAAU;iBACnC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;iBAC3D,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC;SAClC,CAAC,CAAC;QAEH,IAAI,iBAAiB,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACvD,MAAM,CAAC,KAAK,CAAC,4DAA4D,CAAC,CAAC;YAC3E,SAAS;QACX,CAAC;QAED,SAAS,CAAC,IAAI,CAAC;YACb,GAAG,OAAO;YACV,UAAU,EAAE,iBAAiB;SAC9B,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,YAAY,SAAS,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM,+BAA+B,EAAE;QAC3F,kBAAkB,EAAE,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC;QAClD,gBAAgB,EAAE,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC;QAC9C,mBAAmB,EAAE,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC;KACrD,CAAC,CAAC;IACH,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message Processing Control
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* - Provide chat-scoped runtime controls for stopping active message processing.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Registers per-chat processing handles with abort signals.
|
|
9
|
+
* - Cancels queued and active LLM calls by `worldId` and `chatId`.
|
|
10
|
+
* - Stops active shell command executions for the same session scope.
|
|
11
|
+
* - Returns deterministic stop results for IPC/UI feedback.
|
|
12
|
+
*
|
|
13
|
+
* Implementation Notes:
|
|
14
|
+
* - Stop requests are idempotent for repeated calls on the same active operation.
|
|
15
|
+
* - Abort signals are scoped to active processing handles and released on completion.
|
|
16
|
+
* - Existing persisted output is preserved; only in-flight work is interrupted.
|
|
17
|
+
*
|
|
18
|
+
* Recent Changes:
|
|
19
|
+
* - 2026-02-13: Added chat-scoped processing handle registry with abort signals so stop requests also prevent follow-up continuation work.
|
|
20
|
+
* - 2026-02-14: Added `hasActiveChatMessageProcessing()` for chat-scoped lock checks (e.g., message edit guards).
|
|
21
|
+
*/
|
|
22
|
+
export interface ChatMessageProcessingHandle {
|
|
23
|
+
worldId: string;
|
|
24
|
+
chatId: string;
|
|
25
|
+
operationId: string;
|
|
26
|
+
signal: AbortSignal;
|
|
27
|
+
isStopped: () => boolean;
|
|
28
|
+
throwIfStopped: () => void;
|
|
29
|
+
complete: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function beginChatMessageProcessing(worldId: string, chatId: string): ChatMessageProcessingHandle;
|
|
32
|
+
export declare function hasActiveChatMessageProcessing(worldId: string, chatId: string): boolean;
|
|
33
|
+
export declare function throwIfMessageProcessingStopped(signal?: AbortSignal): void;
|
|
34
|
+
export declare function isMessageProcessingCanceledError(error: unknown): boolean;
|
|
35
|
+
export interface StopMessageProcessingResult {
|
|
36
|
+
success: true;
|
|
37
|
+
worldId: string;
|
|
38
|
+
chatId: string;
|
|
39
|
+
stopped: boolean;
|
|
40
|
+
reason: 'stopped' | 'no-active-process';
|
|
41
|
+
stoppedOperations: number;
|
|
42
|
+
llm: {
|
|
43
|
+
canceledPending: number;
|
|
44
|
+
abortedActive: number;
|
|
45
|
+
};
|
|
46
|
+
shell: {
|
|
47
|
+
killed: number;
|
|
48
|
+
};
|
|
49
|
+
processing: {
|
|
50
|
+
abortedActive: number;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export declare function stopMessageProcessing(worldId: string, chatId: string): StopMessageProcessingResult;
|
|
54
|
+
//# sourceMappingURL=message-processing-control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-processing-control.d.ts","sourceRoot":"","sources":["../../core/message-processing-control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAmCH,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC;IACpB,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB,cAAc,EAAE,MAAM,IAAI,CAAC;IAC3B,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,2BAA2B,CAwB7B;AAED,wBAAgB,8BAA8B,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAmBvF;AAED,wBAAgB,+BAA+B,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,CAI1E;AAED,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOxE;AAED,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,IAAI,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,SAAS,GAAG,mBAAmB,CAAC;IACxC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,GAAG,EAAE;QACH,eAAe,EAAE,MAAM,CAAC;QACxB,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;IACF,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,UAAU,EAAE;QACV,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;CACH;AAED,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,2BAA2B,CA8B7B"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message Processing Control
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* - Provide chat-scoped runtime controls for stopping active message processing.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Registers per-chat processing handles with abort signals.
|
|
9
|
+
* - Cancels queued and active LLM calls by `worldId` and `chatId`.
|
|
10
|
+
* - Stops active shell command executions for the same session scope.
|
|
11
|
+
* - Returns deterministic stop results for IPC/UI feedback.
|
|
12
|
+
*
|
|
13
|
+
* Implementation Notes:
|
|
14
|
+
* - Stop requests are idempotent for repeated calls on the same active operation.
|
|
15
|
+
* - Abort signals are scoped to active processing handles and released on completion.
|
|
16
|
+
* - Existing persisted output is preserved; only in-flight work is interrupted.
|
|
17
|
+
*
|
|
18
|
+
* Recent Changes:
|
|
19
|
+
* - 2026-02-13: Added chat-scoped processing handle registry with abort signals so stop requests also prevent follow-up continuation work.
|
|
20
|
+
* - 2026-02-14: Added `hasActiveChatMessageProcessing()` for chat-scoped lock checks (e.g., message edit guards).
|
|
21
|
+
*/
|
|
22
|
+
import { cancelLLMCallsForChat } from './llm-manager.js';
|
|
23
|
+
import { stopShellCommandsForChat } from './shell-cmd-tool.js';
|
|
24
|
+
import { generateId } from './utils.js';
|
|
25
|
+
const activeProcessingByChat = new Map();
|
|
26
|
+
function toChatKey(worldId, chatId) {
|
|
27
|
+
return `${worldId}::${chatId}`;
|
|
28
|
+
}
|
|
29
|
+
function toAbortError(message = 'Message processing canceled by user') {
|
|
30
|
+
return new DOMException(message, 'AbortError');
|
|
31
|
+
}
|
|
32
|
+
function getOrCreateChatProcessingMap(worldId, chatId) {
|
|
33
|
+
const key = toChatKey(worldId, chatId);
|
|
34
|
+
const existing = activeProcessingByChat.get(key);
|
|
35
|
+
if (existing)
|
|
36
|
+
return existing;
|
|
37
|
+
const created = new Map();
|
|
38
|
+
activeProcessingByChat.set(key, created);
|
|
39
|
+
return created;
|
|
40
|
+
}
|
|
41
|
+
function unregisterProcessingHandle(worldId, chatId, operationId) {
|
|
42
|
+
const key = toChatKey(worldId, chatId);
|
|
43
|
+
const operations = activeProcessingByChat.get(key);
|
|
44
|
+
if (!operations)
|
|
45
|
+
return;
|
|
46
|
+
operations.delete(operationId);
|
|
47
|
+
if (operations.size === 0) {
|
|
48
|
+
activeProcessingByChat.delete(key);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function beginChatMessageProcessing(worldId, chatId) {
|
|
52
|
+
const operationId = generateId();
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
const operations = getOrCreateChatProcessingMap(worldId, chatId);
|
|
55
|
+
operations.set(operationId, controller);
|
|
56
|
+
let completed = false;
|
|
57
|
+
return {
|
|
58
|
+
worldId,
|
|
59
|
+
chatId,
|
|
60
|
+
operationId,
|
|
61
|
+
signal: controller.signal,
|
|
62
|
+
isStopped: () => controller.signal.aborted,
|
|
63
|
+
throwIfStopped: () => {
|
|
64
|
+
if (controller.signal.aborted) {
|
|
65
|
+
throw toAbortError();
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
complete: () => {
|
|
69
|
+
if (completed)
|
|
70
|
+
return;
|
|
71
|
+
completed = true;
|
|
72
|
+
unregisterProcessingHandle(worldId, chatId, operationId);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function hasActiveChatMessageProcessing(worldId, chatId) {
|
|
77
|
+
const chatKey = toChatKey(worldId, chatId);
|
|
78
|
+
const operations = activeProcessingByChat.get(chatKey);
|
|
79
|
+
if (!operations || operations.size === 0) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
for (const [operationId, controller] of operations.entries()) {
|
|
83
|
+
if (controller.signal.aborted) {
|
|
84
|
+
operations.delete(operationId);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (operations.size === 0) {
|
|
88
|
+
activeProcessingByChat.delete(chatKey);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
export function throwIfMessageProcessingStopped(signal) {
|
|
94
|
+
if (signal?.aborted) {
|
|
95
|
+
throw toAbortError();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function isMessageProcessingCanceledError(error) {
|
|
99
|
+
if (!error)
|
|
100
|
+
return false;
|
|
101
|
+
if (error instanceof DOMException && error.name === 'AbortError')
|
|
102
|
+
return true;
|
|
103
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
104
|
+
return true;
|
|
105
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
106
|
+
const normalized = message.toLowerCase();
|
|
107
|
+
return normalized.includes('abort') || normalized.includes('canceled');
|
|
108
|
+
}
|
|
109
|
+
export function stopMessageProcessing(worldId, chatId) {
|
|
110
|
+
const chatKey = toChatKey(worldId, chatId);
|
|
111
|
+
const processingOperations = activeProcessingByChat.get(chatKey);
|
|
112
|
+
let abortedProcessing = 0;
|
|
113
|
+
if (processingOperations && processingOperations.size > 0) {
|
|
114
|
+
for (const controller of processingOperations.values()) {
|
|
115
|
+
if (controller.signal.aborted)
|
|
116
|
+
continue;
|
|
117
|
+
controller.abort();
|
|
118
|
+
abortedProcessing += 1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const llm = cancelLLMCallsForChat(worldId, chatId);
|
|
122
|
+
const shell = stopShellCommandsForChat(worldId, chatId);
|
|
123
|
+
const stoppedOperations = llm.canceledPending + llm.abortedActive + shell.killed + abortedProcessing;
|
|
124
|
+
const stopped = stoppedOperations > 0;
|
|
125
|
+
return {
|
|
126
|
+
success: true,
|
|
127
|
+
worldId,
|
|
128
|
+
chatId,
|
|
129
|
+
stopped,
|
|
130
|
+
reason: stopped ? 'stopped' : 'no-active-process',
|
|
131
|
+
stoppedOperations,
|
|
132
|
+
llm,
|
|
133
|
+
shell,
|
|
134
|
+
processing: {
|
|
135
|
+
abortedActive: abortedProcessing
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=message-processing-control.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-processing-control.js","sourceRoot":"","sources":["../../core/message-processing-control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,sBAAsB,GAAG,IAAI,GAAG,EAAwC,CAAC;AAE/E,SAAS,SAAS,CAAC,OAAe,EAAE,MAAc;IAChD,OAAO,GAAG,OAAO,KAAK,MAAM,EAAE,CAAC;AACjC,CAAC;AAED,SAAS,YAAY,CAAC,OAAO,GAAG,qCAAqC;IACnE,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,4BAA4B,CAAC,OAAe,EAAE,MAAc;IACnE,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACvC,MAAM,QAAQ,GAAG,sBAAsB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACjD,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC9B,MAAM,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IACnD,sBAAsB,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACzC,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,0BAA0B,CAAC,OAAe,EAAE,MAAc,EAAE,WAAmB;IACtF,MAAM,GAAG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5
|
+
* - Direct OpenAI API integration bypassing AI SDK schema corruption bug
|
|
6
|
+
* - Support for OpenAI, Azure OpenAI, and OpenAI-compatible providers
|
|
7
|
+
* - Streaming and non-streaming responses returning LLMResponse
|
|
8
|
+
* - Function/tool calling detection (NO execution)
|
|
9
|
+
* - Proper error handling and retry logic
|
|
10
|
+
* - Browser-safe configuration injection
|
|
11
|
+
* - Pure client: only calls APIs and returns structured data
|
|
12
|
+
*
|
|
13
|
+
* Provider Support:
|
|
14
|
+
* - OpenAI: Direct integration with OpenAI API
|
|
15
|
+
* - Azure OpenAI: Direct integration with Azure OpenAI endpoints
|
|
16
|
+
* - OpenAI-Compatible: Direct integration with custom OpenAI-compatible APIs
|
|
17
|
+
* - XAI: Direct integration using OpenAI package with custom base URL
|
|
18
|
+
* - Ollama: Direct integration using OpenAI package with Ollama's OpenAI-compatible endpoint
|
|
19
|
+
*
|
|
20
|
+
* Implementation Details:
|
|
21
|
+
* - Uses official OpenAI package for reliable API access
|
|
22
|
+
* - Converts AI SDK message format to OpenAI format
|
|
23
|
+
* - Returns LLMResponse with type='text' or type='tool_calls'
|
|
24
|
+
* - Streaming support with chunk-by-chunk processing via onChunk callback
|
|
25
|
+
* - Error handling with descriptive messages
|
|
26
|
+
* - Configuration injection from llm-config module
|
|
27
|
+
* - NO event emission, NO storage, NO tool execution
|
|
28
|
+
*
|
|
29
|
+
* Recent Changes:
|
|
30
|
+
* - 2026-02-16: Fixed streaming tool-call chunk merge to preserve delayed tool `id`/`name` fields across deltas.
|
|
31
|
+
* - 2026-02-13: Added abort-signal support for streaming and non-streaming calls to enable chat stop cancellation.
|
|
32
|
+
* - 2026-02-10: Added env-flagged Ollama tool attachment support via ENABLE_OLLAMA_TOOLS
|
|
33
|
+
* - 2026-02-07: Disabled tool definitions for Ollama provider requests
|
|
34
|
+
* - 2025-11-09: Phase 2 - Removed ALL tool execution logic (~200 lines)
|
|
35
|
+
* - Provider is now a pure client - only API calls and data transformation
|
|
36
|
+
* - Returns LLMResponse interface with type discriminator
|
|
37
|
+
* - Filters invalid tool calls (empty/missing names) and logs warnings
|
|
38
|
+
* - Includes usage tracking (prompt_tokens, completion_tokens) for non-streaming
|
|
39
|
+
* - 2025-11-08: Removed ALL event emission from provider (publishToolEvent, publishSSE)
|
|
40
|
+
* - Streaming uses onChunk callback instead of publishSSE
|
|
41
|
+
* - Initial implementation with full OpenAI package integration
|
|
42
|
+
*/
|
|
43
|
+
import OpenAI from 'openai';
|
|
44
|
+
import { World, Agent, ChatMessage, LLMResponse } from './types.js';
|
|
45
|
+
import { OpenAIConfig, AzureConfig, OpenAICompatibleConfig, XAIConfig, OllamaConfig } from './llm-config.js';
|
|
46
|
+
/**
|
|
47
|
+
* OpenAI client factory for standard OpenAI API
|
|
48
|
+
*/
|
|
49
|
+
export declare function createOpenAIClient(config: OpenAIConfig): OpenAI;
|
|
50
|
+
/**
|
|
51
|
+
* OpenAI client factory for Azure OpenAI
|
|
52
|
+
*/
|
|
53
|
+
export declare function createAzureOpenAIClient(config: AzureConfig): OpenAI;
|
|
54
|
+
/**
|
|
55
|
+
* OpenAI client factory for OpenAI-compatible APIs
|
|
56
|
+
*/
|
|
57
|
+
export declare function createOpenAICompatibleClient(config: OpenAICompatibleConfig): OpenAI;
|
|
58
|
+
/**
|
|
59
|
+
* OpenAI client factory for XAI
|
|
60
|
+
*/
|
|
61
|
+
export declare function createXAIClient(config: XAIConfig): OpenAI;
|
|
62
|
+
/**
|
|
63
|
+
* OpenAI client factory for Ollama (OpenAI-compatible endpoint)
|
|
64
|
+
*/
|
|
65
|
+
export declare function createOllamaClient(config: OllamaConfig): OpenAI;
|
|
66
|
+
/**
|
|
67
|
+
* Streaming OpenAI response handler - Pure client (no tool execution)
|
|
68
|
+
* Returns LLMResponse with type='text' or type='tool_calls'
|
|
69
|
+
*/
|
|
70
|
+
export declare function streamOpenAIResponse(client: OpenAI, model: string, messages: ChatMessage[], agent: Agent, mcpTools: Record<string, any>, world: World, onChunk: (content: string) => void, messageId: string, abortSignal?: AbortSignal): Promise<LLMResponse>;
|
|
71
|
+
/**
|
|
72
|
+
* Non-streaming OpenAI response handler - Pure client (no tool execution)
|
|
73
|
+
* Returns LLMResponse with type='text' or type='tool_calls'
|
|
74
|
+
*/
|
|
75
|
+
export declare function generateOpenAIResponse(client: OpenAI, model: string, messages: ChatMessage[], agent: Agent, mcpTools: Record<string, any>, world: World, abortSignal?: AbortSignal): Promise<LLMResponse>;
|
|
76
|
+
/**
|
|
77
|
+
* Factory function to create appropriate OpenAI client based on provider type
|
|
78
|
+
*/
|
|
79
|
+
export declare function createClientForProvider(providerType: string, config: any): OpenAI;
|
|
80
|
+
//# sourceMappingURL=openai-direct.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-direct.d.ts","sourceRoot":"","sources":["../../core/openai-direct.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAwB,YAAY,EAAE,WAAW,EAAE,sBAAsB,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAOnI;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAI/D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAYnE;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,sBAAsB,GAAG,MAAM,CAKnF;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAKzD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAK/D;AA8DD;;;GAGG;A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