@lota-sdk/core 0.4.8 → 0.4.10
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 +11 -12
- package/src/ai/embedding-cache.ts +96 -22
- package/src/ai-gateway/ai-gateway.ts +766 -223
- package/src/config/agent-defaults.ts +189 -75
- package/src/config/agent-types.ts +54 -4
- package/src/config/background-processing.ts +1 -1
- package/src/config/constants.ts +8 -2
- package/src/config/index.ts +0 -1
- package/src/config/logger.ts +299 -19
- package/src/config/thread-defaults.ts +40 -20
- package/src/create-runtime.ts +200 -449
- package/src/db/base.service.ts +52 -28
- package/src/db/cursor-pagination.ts +71 -30
- package/src/db/memory-query-builder.ts +2 -1
- package/src/db/memory-store.helpers.ts +4 -7
- package/src/db/memory-store.ts +868 -601
- package/src/db/memory.ts +396 -280
- package/src/db/record-id.ts +32 -10
- package/src/db/schema-fingerprint.ts +30 -12
- package/src/db/service-normalization.ts +288 -0
- package/src/db/service.ts +912 -779
- package/src/db/startup.ts +153 -68
- package/src/db/transaction-conflict.ts +15 -0
- package/src/effect/awaitable-effect.ts +96 -0
- package/src/effect/errors.ts +121 -0
- package/src/effect/helpers.ts +123 -0
- package/src/effect/index.ts +24 -0
- package/src/effect/layers.ts +238 -0
- package/src/effect/runtime-ref.ts +25 -0
- package/src/effect/runtime.ts +46 -0
- package/src/effect/services.ts +61 -0
- package/src/effect/zod.ts +43 -0
- package/src/embeddings/provider.ts +128 -83
- package/src/index.ts +48 -1
- package/src/openrouter/direct-provider.ts +11 -35
- package/src/queues/autonomous-job.queue.ts +117 -73
- package/src/queues/context-compaction.queue.ts +50 -17
- package/src/queues/delayed-node-promotion.queue.ts +46 -17
- package/src/queues/document-processor.queue.ts +52 -77
- package/src/queues/memory-consolidation.queue.ts +47 -32
- package/src/queues/organization-learning.queue.ts +26 -4
- package/src/queues/plan-agent-heartbeat.queue.ts +71 -24
- package/src/queues/plan-scheduler.queue.ts +97 -33
- package/src/queues/post-chat-memory.queue.ts +56 -26
- package/src/queues/queue-factory.ts +227 -59
- package/src/queues/standalone-worker.ts +39 -0
- package/src/queues/title-generation.queue.ts +45 -11
- package/src/redis/connection.ts +182 -113
- package/src/redis/index.ts +6 -8
- package/src/redis/org-memory-lock.ts +60 -27
- package/src/redis/redis-lease-lock.ts +200 -121
- package/src/redis/runtime-connection.ts +20 -0
- package/src/redis/stream-context.ts +92 -46
- package/src/runtime/agent-identity-overrides.ts +2 -2
- package/src/runtime/agent-runtime-policy.ts +5 -2
- package/src/runtime/agent-stream-helpers.ts +24 -9
- package/src/runtime/chat-run-orchestration.ts +102 -19
- package/src/runtime/chat-run-registry.ts +36 -2
- package/src/runtime/context-compaction/context-compaction-runtime.ts +107 -0
- package/src/runtime/{context-compaction.ts → context-compaction/context-compaction.ts} +161 -94
- package/src/runtime/domain-layer.ts +192 -0
- package/src/runtime/execution-plan-visibility.ts +2 -2
- package/src/runtime/execution-plan.ts +42 -15
- package/src/runtime/graph-designer.ts +16 -4
- package/src/runtime/helper-model.ts +139 -48
- package/src/runtime/index.ts +7 -8
- package/src/runtime/indexed-repositories-policy.ts +3 -3
- package/src/runtime/{memory-block.ts → memory/memory-block.ts} +50 -36
- package/src/runtime/{memory-digest-policy.ts → memory/memory-digest-policy.ts} +1 -1
- package/src/runtime/{memory-pipeline.ts → memory/memory-pipeline.ts} +54 -67
- package/src/runtime/{memory-prompts-fact.ts → memory/memory-prompts-fact.ts} +2 -2
- package/src/runtime/memory/memory-scope.ts +53 -0
- package/src/runtime/plugin-resolution.ts +124 -25
- package/src/runtime/plugin-types.ts +9 -1
- package/src/runtime/post-turn-side-effects.ts +177 -130
- package/src/runtime/retrieval-adapters.ts +40 -6
- package/src/runtime/runtime-accessors.ts +92 -0
- package/src/runtime/runtime-config.ts +150 -61
- package/src/runtime/runtime-extensions.ts +23 -25
- package/src/runtime/runtime-lifecycle.ts +124 -0
- package/src/runtime/runtime-services.ts +386 -0
- package/src/runtime/runtime-token.ts +47 -0
- package/src/runtime/social-chat/social-chat-agent-runner.ts +159 -0
- package/src/runtime/{social-chat-history.ts → social-chat/social-chat-history.ts} +51 -20
- package/src/runtime/social-chat/social-chat.ts +630 -0
- package/src/runtime/specialist-runner.ts +36 -10
- package/src/runtime/team-consultation/team-consultation-orchestrator.ts +433 -0
- package/src/runtime/{team-consultation-prompts.ts → team-consultation/team-consultation-prompts.ts} +6 -2
- package/src/runtime/thread-chat-helpers.ts +2 -2
- package/src/runtime/thread-plan-turn.ts +2 -1
- package/src/runtime/thread-turn-context.ts +183 -111
- package/src/runtime/turn-lifecycle.ts +93 -27
- package/src/services/agent-activity.service.ts +287 -203
- package/src/services/agent-executor.service.ts +253 -149
- package/src/services/artifact.service.ts +231 -149
- package/src/services/attachment.service.ts +171 -115
- package/src/services/autonomous-job.service.ts +890 -491
- package/src/services/background-work.service.ts +54 -0
- package/src/services/chat-run-registry.service.ts +13 -1
- package/src/services/context-compaction.service.ts +136 -86
- package/src/services/document-chunk.service.ts +151 -88
- package/src/services/execution-plan/execution-plan-approval.ts +26 -0
- package/src/services/execution-plan/execution-plan-context.ts +29 -0
- package/src/services/execution-plan/execution-plan-graph.ts +278 -0
- package/src/services/execution-plan/execution-plan-schedule.ts +84 -0
- package/src/services/execution-plan/execution-plan-spec.ts +75 -0
- package/src/services/execution-plan/execution-plan.service.ts +1041 -0
- package/src/services/feedback-loop.service.ts +132 -76
- package/src/services/global-orchestrator.service.ts +101 -168
- package/src/services/graph-full-routing.ts +193 -0
- package/src/services/index.ts +19 -21
- package/src/services/institutional-memory.service.ts +213 -125
- package/src/services/learned-skill.service.ts +368 -260
- package/src/services/memory/memory-conversation.ts +95 -0
- package/src/services/memory/memory-errors.ts +27 -0
- package/src/services/memory/memory-org-memory.ts +50 -0
- package/src/services/memory/memory-preseeded.ts +86 -0
- package/src/services/memory/memory-rerank.ts +297 -0
- package/src/services/{memory-utils.ts → memory/memory-utils.ts} +6 -5
- package/src/services/memory/memory.service.ts +674 -0
- package/src/services/memory/rerank.service.ts +201 -0
- package/src/services/monitoring-window.service.ts +92 -70
- package/src/services/mutating-approval.service.ts +62 -53
- package/src/services/node-workspace.service.ts +141 -98
- package/src/services/notification.service.ts +29 -16
- package/src/services/organization-member.service.ts +120 -66
- package/src/services/organization.service.ts +153 -77
- package/src/services/ownership-dispatcher.service.ts +456 -263
- package/src/services/plan/plan-agent-heartbeat.service.ts +234 -0
- package/src/services/plan/plan-agent-query.service.ts +322 -0
- package/src/services/{plan-approval.service.ts → plan/plan-approval.service.ts} +45 -22
- package/src/services/plan/plan-artifact.service.ts +60 -0
- package/src/services/plan/plan-builder.service.ts +76 -0
- package/src/services/plan/plan-checkpoint.service.ts +103 -0
- package/src/services/{plan-compiler.service.ts → plan/plan-compiler.service.ts} +26 -9
- package/src/services/plan/plan-completion-side-effects.ts +169 -0
- package/src/services/plan/plan-coordination.service.ts +181 -0
- package/src/services/plan/plan-cycle.service.ts +405 -0
- package/src/services/plan/plan-deadline.service.ts +533 -0
- package/src/services/plan/plan-event-delivery.service.ts +266 -0
- package/src/services/plan/plan-executor-context.ts +35 -0
- package/src/services/plan/plan-executor-graph.ts +522 -0
- package/src/services/plan/plan-executor-helpers.ts +307 -0
- package/src/services/plan/plan-executor-persistence.ts +209 -0
- package/src/services/plan/plan-executor.service.ts +1737 -0
- package/src/services/{plan-helpers.ts → plan/plan-helpers.ts} +1 -1
- package/src/services/{plan-run-data.ts → plan/plan-run-data.ts} +4 -4
- package/src/services/plan/plan-run-serialization.ts +15 -0
- package/src/services/plan/plan-run.service.ts +637 -0
- package/src/services/plan/plan-scheduler.service.ts +379 -0
- package/src/services/plan/plan-template.service.ts +224 -0
- package/src/services/plan/plan-transaction-events.ts +36 -0
- package/src/services/plan/plan-validator.service.ts +907 -0
- package/src/services/plan/plan-workspace.service.ts +131 -0
- package/src/services/plugin-executor.service.ts +102 -68
- package/src/services/quality-metrics.service.ts +112 -94
- package/src/services/queue-job.service.ts +288 -231
- package/src/services/recent-activity-title.service.ts +73 -36
- package/src/services/recent-activity.service.ts +274 -259
- package/src/services/skill-resolver.service.ts +38 -12
- package/src/services/social-chat-history.service.ts +190 -122
- package/src/services/system-executor.service.ts +96 -61
- package/src/services/thread/thread-active-run.ts +203 -0
- package/src/services/thread/thread-bootstrap.ts +385 -0
- package/src/services/thread/thread-listing.ts +199 -0
- package/src/services/thread/thread-memory-block.ts +130 -0
- package/src/services/thread/thread-message.service.ts +379 -0
- package/src/services/thread/thread-record-store.ts +155 -0
- package/src/services/thread/thread-title.service.ts +74 -0
- package/src/services/thread/thread-turn-execution.ts +280 -0
- package/src/services/thread/thread-turn-message-context.ts +73 -0
- package/src/services/thread/thread-turn-preparation.service.ts +1148 -0
- package/src/services/thread/thread-turn-streaming.ts +403 -0
- package/src/services/thread/thread-turn-tracing.ts +35 -0
- package/src/services/thread/thread-turn.ts +376 -0
- package/src/services/thread/thread.service.ts +344 -0
- package/src/services/user.service.ts +82 -32
- package/src/services/write-intent-validator.service.ts +63 -51
- package/src/storage/attachment-parser.ts +69 -27
- package/src/storage/attachment-storage.service.ts +334 -275
- package/src/storage/generated-document-storage.service.ts +66 -34
- package/src/system-agents/agent-result.ts +3 -1
- package/src/system-agents/context-compaction.agent.ts +3 -3
- package/src/system-agents/delegated-agent-factory.ts +159 -90
- package/src/system-agents/helper-agent-options.ts +1 -1
- package/src/system-agents/memory-reranker.agent.ts +3 -3
- package/src/system-agents/memory.agent.ts +3 -3
- package/src/system-agents/recent-activity-title-refiner.agent.ts +3 -3
- package/src/system-agents/regular-chat-memory-digest.agent.ts +3 -3
- package/src/system-agents/skill-extractor.agent.ts +3 -3
- package/src/system-agents/skill-manager.agent.ts +3 -3
- package/src/system-agents/thread-router.agent.ts +157 -113
- package/src/system-agents/title-generator.agent.ts +3 -3
- package/src/tools/execution-plan.tool.ts +241 -171
- package/src/tools/fetch-webpage.tool.ts +29 -18
- package/src/tools/firecrawl-client.ts +26 -6
- package/src/tools/index.ts +1 -0
- package/src/tools/memory-block.tool.ts +14 -6
- package/src/tools/plan-approval.tool.ts +57 -47
- package/src/tools/read-file-parts.tool.ts +44 -33
- package/src/tools/remember-memory.tool.ts +65 -45
- package/src/tools/search-web.tool.ts +33 -22
- package/src/tools/search.tool.ts +41 -29
- package/src/tools/team-think.tool.ts +125 -84
- package/src/tools/user-questions.tool.ts +4 -3
- package/src/tools/web-tool-shared.ts +6 -0
- package/src/utils/async.ts +25 -22
- package/src/utils/crypto.ts +21 -0
- package/src/utils/date-time.ts +40 -1
- package/src/utils/errors.ts +111 -20
- package/src/utils/hono-error-handler.ts +24 -39
- package/src/utils/index.ts +2 -1
- package/src/utils/null-proto-record.ts +41 -0
- package/src/utils/sse-keepalive.ts +124 -21
- package/src/workers/bootstrap.ts +164 -52
- package/src/workers/memory-consolidation.worker.ts +325 -237
- package/src/workers/organization-learning.worker.ts +50 -16
- package/src/workers/regular-chat-memory-digest.helpers.ts +28 -27
- package/src/workers/regular-chat-memory-digest.runner.ts +185 -114
- package/src/workers/skill-extraction.runner.ts +176 -93
- package/src/workers/utils/file-section-chunker.ts +8 -10
- package/src/workers/utils/repo-structure-extractor.ts +349 -260
- package/src/workers/utils/repomix-file-sections.ts +2 -2
- package/src/workers/utils/thread-message-query.ts +97 -38
- package/src/workers/worker-utils.ts +74 -31
- package/src/config/debug-logger.ts +0 -47
- package/src/config/search.ts +0 -3
- package/src/redis/connection-accessor.ts +0 -26
- package/src/runtime/agent-types.ts +0 -1
- package/src/runtime/context-compaction-runtime.ts +0 -87
- package/src/runtime/memory-scope.ts +0 -43
- package/src/runtime/social-chat-agent-runner.ts +0 -118
- package/src/runtime/social-chat.ts +0 -516
- package/src/runtime/team-consultation-orchestrator.ts +0 -272
- package/src/services/adaptive-playbook.service.ts +0 -152
- package/src/services/artifact-provenance.service.ts +0 -172
- package/src/services/chat-attachments.service.ts +0 -17
- package/src/services/context-compaction-runtime.singleton.ts +0 -13
- package/src/services/execution-plan.service.ts +0 -1118
- package/src/services/memory.service.ts +0 -914
- package/src/services/plan-agent-heartbeat.service.ts +0 -136
- package/src/services/plan-agent-query.service.ts +0 -267
- package/src/services/plan-artifact.service.ts +0 -50
- package/src/services/plan-builder.service.ts +0 -67
- package/src/services/plan-checkpoint.service.ts +0 -81
- package/src/services/plan-completion-side-effects.ts +0 -80
- package/src/services/plan-coordination.service.ts +0 -157
- package/src/services/plan-cycle.service.ts +0 -284
- package/src/services/plan-deadline.service.ts +0 -430
- package/src/services/plan-event-delivery.service.ts +0 -166
- package/src/services/plan-executor.service.ts +0 -1950
- package/src/services/plan-run.service.ts +0 -515
- package/src/services/plan-scheduler.service.ts +0 -240
- package/src/services/plan-template.service.ts +0 -177
- package/src/services/plan-validator.service.ts +0 -818
- package/src/services/plan-workspace.service.ts +0 -83
- package/src/services/rerank.service.ts +0 -156
- package/src/services/thread-message.service.ts +0 -275
- package/src/services/thread-plan-registry.service.ts +0 -22
- package/src/services/thread-title.service.ts +0 -39
- package/src/services/thread-turn-preparation.service.ts +0 -1147
- package/src/services/thread-turn.ts +0 -172
- package/src/services/thread.service.ts +0 -869
- package/src/utils/env.ts +0 -8
- /package/src/runtime/{context-compaction-constants.ts → context-compaction/context-compaction-constants.ts} +0 -0
- /package/src/runtime/{memory-format.ts → memory/memory-format.ts} +0 -0
- /package/src/runtime/{memory-prompts-parse.ts → memory/memory-prompts-parse.ts} +0 -0
- /package/src/runtime/{memory-prompts-update.ts → memory/memory-prompts-update.ts} +0 -0
- /package/src/runtime/{social-chat-prompts.ts → social-chat/social-chat-prompts.ts} +0 -0
- /package/src/services/{plan-node-spec.ts → plan/plan-node-spec.ts} +0 -0
- /package/src/services/{thread-constants.ts → thread/thread-constants.ts} +0 -0
- /package/src/services/{thread.types.ts → thread/thread.types.ts} +0 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Eagerly resolves the full SDK service surface against a live ManagedRuntime.
|
|
3
|
+
*
|
|
4
|
+
* The previous implementation used per-property `get` Proxies that re-resolved
|
|
5
|
+
* each tag on every access. This module resolves every tag exactly once when
|
|
6
|
+
* the runtime starts and returns a plain object whose shape matches the
|
|
7
|
+
* `LotaRuntime['services']` + `LotaRuntime['lota']` contracts byte-for-byte.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ChatMessage } from '@lota-sdk/shared'
|
|
11
|
+
import type { Context, ManagedRuntime } from 'effect'
|
|
12
|
+
import { Effect } from 'effect'
|
|
13
|
+
|
|
14
|
+
import { ensureRecordId } from '../db/record-id'
|
|
15
|
+
import type { SurrealDBService } from '../db/service'
|
|
16
|
+
import { TABLES } from '../db/tables'
|
|
17
|
+
import type { AwaitableService, AwaitableValue } from '../effect/awaitable-effect'
|
|
18
|
+
import { toAwaitableService } from '../effect/awaitable-effect'
|
|
19
|
+
import { BadRequestError, NotFoundError } from '../effect/errors'
|
|
20
|
+
import { effectTryPromise } from '../effect/helpers'
|
|
21
|
+
import type { RedisConnectionManager } from '../redis/connection'
|
|
22
|
+
import type { SharedThreadStreamSubscriberTag } from '../redis/stream-context'
|
|
23
|
+
import { AgentActivityServiceTag } from '../services/agent-activity.service'
|
|
24
|
+
import { ArtifactServiceTag } from '../services/artifact.service'
|
|
25
|
+
import { AttachmentServiceTag } from '../services/attachment.service'
|
|
26
|
+
import { AutonomousJobServiceTag } from '../services/autonomous-job.service'
|
|
27
|
+
import { ContextCompactionServiceTag } from '../services/context-compaction.service'
|
|
28
|
+
import { DocumentChunkServiceTag } from '../services/document-chunk.service'
|
|
29
|
+
import { ExecutionPlanServiceTag } from '../services/execution-plan/execution-plan.service'
|
|
30
|
+
import { MemoryServiceTag } from '../services/memory/memory.service'
|
|
31
|
+
import { RerankServiceTag } from '../services/memory/rerank.service'
|
|
32
|
+
import { MutatingApprovalServiceTag } from '../services/mutating-approval.service'
|
|
33
|
+
import { OrganizationMemberServiceTag } from '../services/organization-member.service'
|
|
34
|
+
import { OrganizationServiceTag } from '../services/organization.service'
|
|
35
|
+
import { PlanAgentHeartbeatServiceTag } from '../services/plan/plan-agent-heartbeat.service'
|
|
36
|
+
import { PlanAgentQueryServiceTag } from '../services/plan/plan-agent-query.service'
|
|
37
|
+
import { PlanCoordinationServiceTag } from '../services/plan/plan-coordination.service'
|
|
38
|
+
import { PlanCycleServiceTag } from '../services/plan/plan-cycle.service'
|
|
39
|
+
import { PlanDeadlineServiceTag } from '../services/plan/plan-deadline.service'
|
|
40
|
+
import { PlanExecutorServiceTag } from '../services/plan/plan-executor.service'
|
|
41
|
+
import { PlanRunServiceTag } from '../services/plan/plan-run.service'
|
|
42
|
+
import { PlanSchedulerServiceTag } from '../services/plan/plan-scheduler.service'
|
|
43
|
+
import { PlanTemplateServiceTag } from '../services/plan/plan-template.service'
|
|
44
|
+
import { RecentActivityTitleServiceTag } from '../services/recent-activity-title.service'
|
|
45
|
+
import { RecentActivityServiceTag } from '../services/recent-activity.service'
|
|
46
|
+
import type { makeSocialChatHistoryService } from '../services/social-chat-history.service'
|
|
47
|
+
import { ThreadMessageServiceTag } from '../services/thread/thread-message.service'
|
|
48
|
+
import { ThreadTitleServiceTag } from '../services/thread/thread-title.service'
|
|
49
|
+
import { isApprovalContinuationRequest as isApprovalContinuationRequestFn } from '../services/thread/thread-turn'
|
|
50
|
+
import type {
|
|
51
|
+
createThreadApprovalContinuationStream,
|
|
52
|
+
createThreadNativeToolApprovalStream,
|
|
53
|
+
createThreadTurnStream,
|
|
54
|
+
runThreadTurnInBackground,
|
|
55
|
+
ThreadTurnServiceTag,
|
|
56
|
+
triggerPlanNodeTurn,
|
|
57
|
+
} from '../services/thread/thread-turn'
|
|
58
|
+
import { ThreadServiceTag } from '../services/thread/thread.service'
|
|
59
|
+
import { UserServiceTag } from '../services/user.service'
|
|
60
|
+
import { GeneratedDocumentStorageServiceTag } from '../storage/generated-document-storage.service'
|
|
61
|
+
import { routeThreadChatMessages } from './chat-request-routing'
|
|
62
|
+
|
|
63
|
+
// eslint-disable-next-line typescript-eslint/no-explicit-any -- ManagedRuntime is contravariant in R; `any` is the only valid wildcard
|
|
64
|
+
type SdkManagedRuntime = ManagedRuntime.ManagedRuntime<any, any>
|
|
65
|
+
|
|
66
|
+
// ── Type helpers (shared with create-runtime.ts's public interface) ──
|
|
67
|
+
type Svc<T extends { readonly Service: unknown }> = T['Service']
|
|
68
|
+
type HostSvc<T extends { readonly Service: object }> = AwaitableService<T['Service']>
|
|
69
|
+
|
|
70
|
+
export type AwaitableThreadService = HostSvc<typeof ThreadServiceTag> &
|
|
71
|
+
Pick<Svc<typeof ThreadServiceTag>, 'withActiveRunLease'>
|
|
72
|
+
export type AwaitableDocumentChunkService = HostSvc<typeof DocumentChunkServiceTag> &
|
|
73
|
+
Pick<Svc<typeof DocumentChunkServiceTag>, 'syncVersionedChunks'>
|
|
74
|
+
|
|
75
|
+
export type ArchiveSdkThread = (
|
|
76
|
+
threadId: Parameters<Svc<typeof ThreadServiceTag>['updateStatus']>[0],
|
|
77
|
+
status?: 'archived',
|
|
78
|
+
) => ReturnType<Svc<typeof ThreadServiceTag>['updateStatus']>
|
|
79
|
+
|
|
80
|
+
export type UnarchiveSdkThread = (
|
|
81
|
+
threadId: Parameters<Svc<typeof ThreadServiceTag>['updateStatus']>[0],
|
|
82
|
+
status?: 'active',
|
|
83
|
+
) => ReturnType<Svc<typeof ThreadServiceTag>['updateStatus']>
|
|
84
|
+
|
|
85
|
+
export interface LotaRuntimeServices {
|
|
86
|
+
database: SurrealDBService
|
|
87
|
+
redis: RedisConnectionManager
|
|
88
|
+
closeRedisConnection: () => Promise<void>
|
|
89
|
+
agentActivityService: HostSvc<typeof AgentActivityServiceTag>
|
|
90
|
+
artifactService: HostSvc<typeof ArtifactServiceTag>
|
|
91
|
+
attachmentService: HostSvc<typeof AttachmentServiceTag>
|
|
92
|
+
autonomousJobService: HostSvc<typeof AutonomousJobServiceTag>
|
|
93
|
+
contextCompactionService: HostSvc<typeof ContextCompactionServiceTag>
|
|
94
|
+
documentChunkService: AwaitableDocumentChunkService
|
|
95
|
+
generatedDocumentStorageService: HostSvc<typeof GeneratedDocumentStorageServiceTag>
|
|
96
|
+
memoryService: HostSvc<typeof MemoryServiceTag>
|
|
97
|
+
rerankService: HostSvc<typeof RerankServiceTag>
|
|
98
|
+
verifyMutatingApproval: HostSvc<typeof MutatingApprovalServiceTag>
|
|
99
|
+
organizationService: HostSvc<typeof OrganizationServiceTag>
|
|
100
|
+
organizationMemberService: HostSvc<typeof OrganizationMemberServiceTag>
|
|
101
|
+
userService: HostSvc<typeof UserServiceTag>
|
|
102
|
+
recentActivityService: HostSvc<typeof RecentActivityServiceTag>
|
|
103
|
+
recentActivityTitleService: HostSvc<typeof RecentActivityTitleServiceTag>
|
|
104
|
+
socialChatHistoryService: ReturnType<typeof makeSocialChatHistoryService>
|
|
105
|
+
executionPlanService: HostSvc<typeof ExecutionPlanServiceTag>
|
|
106
|
+
planDeadlineService: HostSvc<typeof PlanDeadlineServiceTag>
|
|
107
|
+
planExecutorService: HostSvc<typeof PlanExecutorServiceTag>
|
|
108
|
+
planRunService: HostSvc<typeof PlanRunServiceTag>
|
|
109
|
+
planTemplateService: HostSvc<typeof PlanTemplateServiceTag>
|
|
110
|
+
planCoordinationService: HostSvc<typeof PlanCoordinationServiceTag>
|
|
111
|
+
planSchedulerService: HostSvc<typeof PlanSchedulerServiceTag>
|
|
112
|
+
planAgentHeartbeatService: HostSvc<typeof PlanAgentHeartbeatServiceTag>
|
|
113
|
+
planAgentQueryService: HostSvc<typeof PlanAgentQueryServiceTag>
|
|
114
|
+
planCycleService: HostSvc<typeof PlanCycleServiceTag>
|
|
115
|
+
threadMessageService: HostSvc<typeof ThreadMessageServiceTag>
|
|
116
|
+
threadService: AwaitableThreadService
|
|
117
|
+
threadTitleService: HostSvc<typeof ThreadTitleServiceTag>
|
|
118
|
+
createThreadApprovalContinuationStream: typeof createThreadApprovalContinuationStream
|
|
119
|
+
createThreadNativeToolApprovalStream: typeof createThreadNativeToolApprovalStream
|
|
120
|
+
createThreadTurnStream: typeof createThreadTurnStream
|
|
121
|
+
isApprovalContinuationRequest: typeof isApprovalContinuationRequestFn
|
|
122
|
+
runThreadTurnInBackground: typeof runThreadTurnInBackground
|
|
123
|
+
triggerPlanNodeTurn: typeof triggerPlanNodeTurn
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface LotaRuntimeLota {
|
|
127
|
+
organizations: {
|
|
128
|
+
create: AwaitableValue<Svc<typeof OrganizationServiceTag>['createOrganization']>
|
|
129
|
+
upsert: AwaitableValue<Svc<typeof OrganizationServiceTag>['upsertOrganization']>
|
|
130
|
+
get: AwaitableValue<Svc<typeof OrganizationServiceTag>['getOrganization']>
|
|
131
|
+
list: AwaitableValue<Svc<typeof OrganizationServiceTag>['listOrganizations']>
|
|
132
|
+
update: AwaitableValue<Svc<typeof OrganizationServiceTag>['updateOrganization']>
|
|
133
|
+
delete: AwaitableValue<Svc<typeof OrganizationServiceTag>['deleteOrganization']>
|
|
134
|
+
}
|
|
135
|
+
users: {
|
|
136
|
+
upsert: AwaitableValue<Svc<typeof UserServiceTag>['upsertUser']>
|
|
137
|
+
get: AwaitableValue<Svc<typeof UserServiceTag>['getUser']>
|
|
138
|
+
list: AwaitableValue<Svc<typeof UserServiceTag>['listUsers']>
|
|
139
|
+
update: AwaitableValue<Svc<typeof UserServiceTag>['updateUser']>
|
|
140
|
+
delete: AwaitableValue<Svc<typeof UserServiceTag>['deleteUser']>
|
|
141
|
+
}
|
|
142
|
+
memberships: {
|
|
143
|
+
add: AwaitableValue<Svc<typeof OrganizationMemberServiceTag>['addMembership']>
|
|
144
|
+
listForOrganization: AwaitableValue<Svc<typeof OrganizationMemberServiceTag>['listMembershipsForOrganization']>
|
|
145
|
+
listForUser: AwaitableValue<Svc<typeof OrganizationMemberServiceTag>['listMembershipsForUser']>
|
|
146
|
+
remove: AwaitableValue<Svc<typeof OrganizationMemberServiceTag>['removeMembership']>
|
|
147
|
+
isMember: AwaitableValue<Svc<typeof OrganizationMemberServiceTag>['isMember']>
|
|
148
|
+
}
|
|
149
|
+
threads: {
|
|
150
|
+
create: AwaitableValue<Svc<typeof ThreadServiceTag>['createThread']>
|
|
151
|
+
list: AwaitableValue<Svc<typeof ThreadServiceTag>['listThreads']>
|
|
152
|
+
get: AwaitableValue<Svc<typeof ThreadServiceTag>['getThread']>
|
|
153
|
+
update: AwaitableValue<Svc<typeof ThreadServiceTag>['updateTitle']>
|
|
154
|
+
archive: AwaitableValue<ArchiveSdkThread>
|
|
155
|
+
unarchive: AwaitableValue<UnarchiveSdkThread>
|
|
156
|
+
delete: AwaitableValue<Svc<typeof ThreadServiceTag>['deleteThread']>
|
|
157
|
+
stop: AwaitableValue<Svc<typeof ThreadServiceTag>['stopActiveRun']>
|
|
158
|
+
listMessages: AwaitableValue<Svc<typeof ThreadMessageServiceTag>['listMessageHistoryPage']>
|
|
159
|
+
getMessage: (params: { threadId: string; messageId: string }) => Promise<ChatMessage>
|
|
160
|
+
sendMessage: (params: {
|
|
161
|
+
threadId: string
|
|
162
|
+
organizationId: string
|
|
163
|
+
userId: string
|
|
164
|
+
userName: string
|
|
165
|
+
messages: Parameters<typeof routeThreadChatMessages>[0]
|
|
166
|
+
}) => Promise<Awaited<ReturnType<typeof createThreadTurnStream>>>
|
|
167
|
+
continueApproval: (params: {
|
|
168
|
+
threadId: string
|
|
169
|
+
organizationId: string
|
|
170
|
+
userId: string
|
|
171
|
+
userName: string
|
|
172
|
+
messages: Parameters<typeof routeThreadChatMessages>[0]
|
|
173
|
+
}) => Promise<Awaited<ReturnType<typeof createThreadApprovalContinuationStream>>>
|
|
174
|
+
uploadAttachment: AwaitableValue<Svc<typeof AttachmentServiceTag>['uploadThreadAttachment']>
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
interface BuildRuntimeServiceSurfaceInput {
|
|
179
|
+
managedRuntime: SdkManagedRuntime
|
|
180
|
+
db: SurrealDBService
|
|
181
|
+
redisManager: RedisConnectionManager
|
|
182
|
+
sharedSubscriber: Svc<typeof SharedThreadStreamSubscriberTag>
|
|
183
|
+
threadTurnService: Svc<typeof ThreadTurnServiceTag>
|
|
184
|
+
socialChatHistoryService: ReturnType<typeof makeSocialChatHistoryService>
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface RuntimeServiceSurface {
|
|
188
|
+
services: LotaRuntimeServices
|
|
189
|
+
lota: LotaRuntimeLota
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Eagerly resolve the full service surface exactly once.
|
|
194
|
+
*
|
|
195
|
+
* All Effect-returning service methods are wrapped with `toAwaitableService`,
|
|
196
|
+
* which means callers can `await` them directly while they stay discoverable
|
|
197
|
+
* as Effects for composition.
|
|
198
|
+
*/
|
|
199
|
+
export function buildRuntimeServiceSurface(input: BuildRuntimeServiceSurfaceInput): RuntimeServiceSurface {
|
|
200
|
+
const { managedRuntime, db, redisManager, threadTurnService, socialChatHistoryService } = input
|
|
201
|
+
const runPromise = <A, E>(effect: Effect.Effect<A, E>): Promise<A> => managedRuntime.runPromise(effect)
|
|
202
|
+
|
|
203
|
+
const resolveRaw = <I, T>(tag: Context.Key<I, T>): T => managedRuntime.runSync(Effect.service(tag))
|
|
204
|
+
const resolveOnce = <I, T extends object>(tag: Context.Key<I, T>): AwaitableService<T> =>
|
|
205
|
+
toAwaitableService(resolveRaw(tag), { runPromise: (effect) => managedRuntime.runPromise(effect) })
|
|
206
|
+
|
|
207
|
+
// ── Resolve every tag eagerly. Each binding is a plain value from here. ──
|
|
208
|
+
const agentActivityService = resolveOnce(AgentActivityServiceTag)
|
|
209
|
+
const artifactService = resolveOnce(ArtifactServiceTag)
|
|
210
|
+
const attachmentService = resolveOnce(AttachmentServiceTag)
|
|
211
|
+
const autonomousJobService = resolveOnce(AutonomousJobServiceTag)
|
|
212
|
+
const contextCompactionService = resolveOnce(ContextCompactionServiceTag)
|
|
213
|
+
const documentChunkService = resolveOnce(DocumentChunkServiceTag) as AwaitableDocumentChunkService
|
|
214
|
+
const generatedDocumentStorageService = resolveOnce(GeneratedDocumentStorageServiceTag)
|
|
215
|
+
const memoryService = resolveOnce(MemoryServiceTag)
|
|
216
|
+
const rerankService = resolveOnce(RerankServiceTag)
|
|
217
|
+
const verifyMutatingApproval = resolveOnce(MutatingApprovalServiceTag)
|
|
218
|
+
const organizationService = resolveOnce(OrganizationServiceTag)
|
|
219
|
+
const organizationMemberService = resolveOnce(OrganizationMemberServiceTag)
|
|
220
|
+
const userService = resolveOnce(UserServiceTag)
|
|
221
|
+
const recentActivityService = resolveOnce(RecentActivityServiceTag)
|
|
222
|
+
const recentActivityTitleService = resolveOnce(RecentActivityTitleServiceTag)
|
|
223
|
+
const executionPlanService = resolveOnce(ExecutionPlanServiceTag)
|
|
224
|
+
const planDeadlineService = resolveOnce(PlanDeadlineServiceTag)
|
|
225
|
+
const planExecutorService = resolveOnce(PlanExecutorServiceTag)
|
|
226
|
+
const planRunService = resolveOnce(PlanRunServiceTag)
|
|
227
|
+
const planTemplateService = resolveOnce(PlanTemplateServiceTag)
|
|
228
|
+
const planCoordinationService = resolveOnce(PlanCoordinationServiceTag)
|
|
229
|
+
const planSchedulerService = resolveOnce(PlanSchedulerServiceTag)
|
|
230
|
+
const planAgentHeartbeatService = resolveOnce(PlanAgentHeartbeatServiceTag)
|
|
231
|
+
const planAgentQueryService = resolveOnce(PlanAgentQueryServiceTag)
|
|
232
|
+
const planCycleService = resolveOnce(PlanCycleServiceTag)
|
|
233
|
+
const threadMessageService = resolveOnce(ThreadMessageServiceTag)
|
|
234
|
+
const threadService = resolveOnce(ThreadServiceTag) as AwaitableThreadService
|
|
235
|
+
const threadTitleService = resolveOnce(ThreadTitleServiceTag)
|
|
236
|
+
|
|
237
|
+
// ── Resolve the raw service instances once for inline Effect composition ──
|
|
238
|
+
// `toAwaitableService` (used above) erases Effect return types so callers
|
|
239
|
+
// can `await` directly. The `getMessage`/`sendMessage`/`continueApproval`
|
|
240
|
+
// wrappers below need the raw Effect-returning signatures so they can
|
|
241
|
+
// `yield*` service methods inside their `Effect.gen` blocks.
|
|
242
|
+
const threadMessageServiceRaw = resolveRaw(ThreadMessageServiceTag)
|
|
243
|
+
const threadServiceRaw = resolveRaw(ThreadServiceTag)
|
|
244
|
+
|
|
245
|
+
const services: LotaRuntimeServices = {
|
|
246
|
+
database: db,
|
|
247
|
+
redis: redisManager,
|
|
248
|
+
closeRedisConnection: () => runPromise(effectTryPromise(() => redisManager.closeConnection())),
|
|
249
|
+
agentActivityService,
|
|
250
|
+
artifactService,
|
|
251
|
+
attachmentService,
|
|
252
|
+
autonomousJobService,
|
|
253
|
+
contextCompactionService,
|
|
254
|
+
documentChunkService,
|
|
255
|
+
generatedDocumentStorageService,
|
|
256
|
+
memoryService,
|
|
257
|
+
rerankService,
|
|
258
|
+
verifyMutatingApproval,
|
|
259
|
+
organizationService,
|
|
260
|
+
organizationMemberService,
|
|
261
|
+
userService,
|
|
262
|
+
recentActivityService,
|
|
263
|
+
recentActivityTitleService,
|
|
264
|
+
socialChatHistoryService,
|
|
265
|
+
executionPlanService,
|
|
266
|
+
planDeadlineService,
|
|
267
|
+
planExecutorService,
|
|
268
|
+
planRunService,
|
|
269
|
+
planTemplateService,
|
|
270
|
+
planCoordinationService,
|
|
271
|
+
planSchedulerService,
|
|
272
|
+
planAgentHeartbeatService,
|
|
273
|
+
planAgentQueryService,
|
|
274
|
+
planCycleService,
|
|
275
|
+
threadMessageService,
|
|
276
|
+
threadService,
|
|
277
|
+
threadTitleService,
|
|
278
|
+
createThreadApprovalContinuationStream: (...args) =>
|
|
279
|
+
runPromise(threadTurnService.createThreadApprovalContinuationStream(...args)),
|
|
280
|
+
createThreadNativeToolApprovalStream: (...args) =>
|
|
281
|
+
runPromise(threadTurnService.createThreadNativeToolApprovalStream(...args)),
|
|
282
|
+
createThreadTurnStream: (...args) => runPromise(threadTurnService.createThreadTurnStream(...args)),
|
|
283
|
+
isApprovalContinuationRequest: isApprovalContinuationRequestFn,
|
|
284
|
+
runThreadTurnInBackground: (...args) => runPromise(threadTurnService.runThreadTurnInBackground(...args)),
|
|
285
|
+
triggerPlanNodeTurn: (...args) => runPromise(threadTurnService.triggerPlanNodeTurn(...args)),
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const lota: LotaRuntimeLota = {
|
|
289
|
+
organizations: {
|
|
290
|
+
create: (...args) => organizationService.createOrganization(...args),
|
|
291
|
+
upsert: (...args) => organizationService.upsertOrganization(...args),
|
|
292
|
+
get: (...args) => organizationService.getOrganization(...args),
|
|
293
|
+
list: (...args) => organizationService.listOrganizations(...args),
|
|
294
|
+
update: (...args) => organizationService.updateOrganization(...args),
|
|
295
|
+
delete: (...args) => organizationService.deleteOrganization(...args),
|
|
296
|
+
},
|
|
297
|
+
users: {
|
|
298
|
+
upsert: (...args) => userService.upsertUser(...args),
|
|
299
|
+
get: (...args) => userService.getUser(...args),
|
|
300
|
+
list: (...args) => userService.listUsers(...args),
|
|
301
|
+
update: (...args) => userService.updateUser(...args),
|
|
302
|
+
delete: (...args) => userService.deleteUser(...args),
|
|
303
|
+
},
|
|
304
|
+
memberships: {
|
|
305
|
+
add: (...args) => organizationMemberService.addMembership(...args),
|
|
306
|
+
listForOrganization: (...args) => organizationMemberService.listMembershipsForOrganization(...args),
|
|
307
|
+
listForUser: (...args) => organizationMemberService.listMembershipsForUser(...args),
|
|
308
|
+
remove: (...args) => organizationMemberService.removeMembership(...args),
|
|
309
|
+
isMember: (...args) => organizationMemberService.isMember(...args),
|
|
310
|
+
},
|
|
311
|
+
threads: {
|
|
312
|
+
create: (...args) => threadService.createThread(...args),
|
|
313
|
+
list: (...args) => threadService.listThreads(...args),
|
|
314
|
+
get: (...args) => threadService.getThread(...args),
|
|
315
|
+
update: (...args) => threadService.updateTitle(...args),
|
|
316
|
+
archive: (threadId, status = 'archived') => threadService.updateStatus(threadId, status),
|
|
317
|
+
unarchive: (threadId, status = 'active') => threadService.updateStatus(threadId, status),
|
|
318
|
+
delete: (...args) => threadService.deleteThread(...args),
|
|
319
|
+
stop: (...args) => threadService.stopActiveRun(...args),
|
|
320
|
+
listMessages: (...args) => threadMessageService.listMessageHistoryPage(...args),
|
|
321
|
+
getMessage: ({ threadId, messageId }) =>
|
|
322
|
+
runPromise(
|
|
323
|
+
Effect.gen(function* () {
|
|
324
|
+
const messages = yield* threadMessageServiceRaw.listMessages(ensureRecordId(threadId, TABLES.THREAD))
|
|
325
|
+
const message = messages.find((candidate: ChatMessage) => candidate.id === messageId)
|
|
326
|
+
if (!message) {
|
|
327
|
+
return yield* new NotFoundError({
|
|
328
|
+
resource: 'Thread message',
|
|
329
|
+
id: messageId,
|
|
330
|
+
message: `Thread message not found: ${messageId}`,
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
return message
|
|
334
|
+
}),
|
|
335
|
+
),
|
|
336
|
+
sendMessage: ({ threadId, organizationId, userId, userName, messages }) =>
|
|
337
|
+
runPromise(
|
|
338
|
+
Effect.gen(function* () {
|
|
339
|
+
const threadRef = ensureRecordId(threadId, TABLES.THREAD)
|
|
340
|
+
const thread = yield* threadServiceRaw.getThread(threadRef)
|
|
341
|
+
const routed = routeThreadChatMessages(messages)
|
|
342
|
+
if (routed.kind !== 'turn') {
|
|
343
|
+
return yield* new BadRequestError({
|
|
344
|
+
message: routed.kind === 'invalid' ? routed.message : 'Expected a user turn payload.',
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return yield* threadTurnService.createThreadTurnStream({
|
|
349
|
+
thread,
|
|
350
|
+
threadRef,
|
|
351
|
+
orgRef: ensureRecordId(organizationId, TABLES.ORGANIZATION),
|
|
352
|
+
userRef: ensureRecordId(userId, TABLES.USER),
|
|
353
|
+
userName,
|
|
354
|
+
inputMessage: routed.inputMessage,
|
|
355
|
+
})
|
|
356
|
+
}),
|
|
357
|
+
),
|
|
358
|
+
continueApproval: ({ threadId, organizationId, userId, userName, messages }) =>
|
|
359
|
+
runPromise(
|
|
360
|
+
Effect.gen(function* () {
|
|
361
|
+
const threadRef = ensureRecordId(threadId, TABLES.THREAD)
|
|
362
|
+
const thread = yield* threadServiceRaw.getThread(threadRef)
|
|
363
|
+
const routed = routeThreadChatMessages(messages)
|
|
364
|
+
if (routed.kind !== 'approval-continuation') {
|
|
365
|
+
return yield* new BadRequestError({
|
|
366
|
+
message:
|
|
367
|
+
routed.kind === 'invalid' ? routed.message : 'Expected approval continuation messages payload.',
|
|
368
|
+
})
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return yield* threadTurnService.createThreadApprovalContinuationStream({
|
|
372
|
+
thread,
|
|
373
|
+
threadRef,
|
|
374
|
+
orgRef: ensureRecordId(organizationId, TABLES.ORGANIZATION),
|
|
375
|
+
userRef: ensureRecordId(userId, TABLES.USER),
|
|
376
|
+
userName,
|
|
377
|
+
approvalMessages: routed.approvalMessages,
|
|
378
|
+
})
|
|
379
|
+
}),
|
|
380
|
+
),
|
|
381
|
+
uploadAttachment: (...args) => attachmentService.uploadThreadAttachment(...args),
|
|
382
|
+
},
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return { services, lota }
|
|
386
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-slot runtime token. `createLotaRuntime()` claims the slot on success
|
|
3
|
+
* and releases it on disconnect, enforcing the "one runtime per process"
|
|
4
|
+
* invariant. Kept as a small, typed module so the guard is trivially testable
|
|
5
|
+
* and stays out of the main entrypoint.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Effect } from 'effect'
|
|
9
|
+
|
|
10
|
+
import { ConfigurationError } from '../effect/errors'
|
|
11
|
+
|
|
12
|
+
let activeToken: symbol | null = null
|
|
13
|
+
|
|
14
|
+
function buildConflictError(): ConfigurationError {
|
|
15
|
+
return new ConfigurationError({
|
|
16
|
+
message: 'createLotaRuntime() is process-scoped. Disconnect the active runtime before creating another one.',
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Claim the process-wide runtime slot. Fails with `ConfigurationError` if it
|
|
22
|
+
* is already held. Preferred inside `Effect.gen` so the failure is expressed
|
|
23
|
+
* through the typed error channel instead of a thrown value.
|
|
24
|
+
*/
|
|
25
|
+
export function acquireRuntimeTokenEffect(): Effect.Effect<symbol, ConfigurationError> {
|
|
26
|
+
return Effect.suspend(() => {
|
|
27
|
+
if (activeToken) {
|
|
28
|
+
return Effect.fail(buildConflictError())
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const token = Symbol('lota-runtime')
|
|
32
|
+
activeToken = token
|
|
33
|
+
return Effect.succeed(token)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Release the slot only if the caller holds the current token. */
|
|
38
|
+
export function releaseRuntimeToken(token: symbol): void {
|
|
39
|
+
if (activeToken === token) {
|
|
40
|
+
activeToken = null
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Diagnostic: whether a runtime is currently active in this process. */
|
|
45
|
+
export function isRuntimeTokenActive(): boolean {
|
|
46
|
+
return activeToken !== null
|
|
47
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { stepCountIs } from 'ai'
|
|
2
|
+
import type { ToolSet } from 'ai'
|
|
3
|
+
import { Schema, Effect } from 'effect'
|
|
4
|
+
|
|
5
|
+
import { getAgentRuntimeConfig, getResolvedAgentFactoryConfig } from '../../config/agent-defaults'
|
|
6
|
+
import { aiLogger } from '../../config/logger'
|
|
7
|
+
|
|
8
|
+
interface ExecutableToolDefinition {
|
|
9
|
+
execute: (...args: unknown[]) => Promise<unknown>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class SocialChatAgentRunnerError extends Schema.TaggedErrorClass<SocialChatAgentRunnerError>()(
|
|
13
|
+
'SocialChatAgentRunnerError',
|
|
14
|
+
{ message: Schema.String, cause: Schema.optional(Schema.Defect) },
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
const SOCIAL_CHAT_RETRIEVAL_TOOL_NAMES = [
|
|
18
|
+
'memorySearch',
|
|
19
|
+
'conversationSearch',
|
|
20
|
+
'queryKnowledge',
|
|
21
|
+
'researchTopic',
|
|
22
|
+
'searchWeb',
|
|
23
|
+
'fetchWebpage',
|
|
24
|
+
'inspectWebsite',
|
|
25
|
+
] as const
|
|
26
|
+
|
|
27
|
+
export function withLoggedSocialToolSet(
|
|
28
|
+
tools: ToolSet,
|
|
29
|
+
params: { agentId: string; channelId: string; threadId: string; executedToolNames: string[] },
|
|
30
|
+
): ToolSet {
|
|
31
|
+
return Object.fromEntries(
|
|
32
|
+
Object.entries(tools).map(([toolName, toolDefinition]) => {
|
|
33
|
+
if (
|
|
34
|
+
typeof toolDefinition !== 'object' ||
|
|
35
|
+
!('execute' in toolDefinition) ||
|
|
36
|
+
typeof toolDefinition.execute !== 'function'
|
|
37
|
+
) {
|
|
38
|
+
return [toolName, toolDefinition]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const executableTool = toolDefinition as typeof toolDefinition & ExecutableToolDefinition
|
|
42
|
+
return [
|
|
43
|
+
toolName,
|
|
44
|
+
{
|
|
45
|
+
...toolDefinition,
|
|
46
|
+
execute: (...args: unknown[]): Promise<unknown> =>
|
|
47
|
+
Effect.runPromise(
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
aiLogger.debug`Slack social-chat tool start: agentId=${params.agentId}, tool=${toolName}, channelId=${params.channelId}, threadId=${params.threadId}`
|
|
50
|
+
const result: unknown = yield* Effect.tryPromise({
|
|
51
|
+
try: () => executableTool.execute(...args),
|
|
52
|
+
catch: (error: unknown) => new SocialChatAgentRunnerError({ message: String(error), cause: error }),
|
|
53
|
+
})
|
|
54
|
+
params.executedToolNames.push(toolName)
|
|
55
|
+
aiLogger.debug`Slack social-chat tool finish: agentId=${params.agentId}, tool=${toolName}, channelId=${params.channelId}, threadId=${params.threadId}`
|
|
56
|
+
return result
|
|
57
|
+
}).pipe(
|
|
58
|
+
Effect.tapError((error) =>
|
|
59
|
+
Effect.sync(() => {
|
|
60
|
+
aiLogger.warn`Slack social-chat tool failed: agentId=${params.agentId}, tool=${toolName}, channelId=${params.channelId}, threadId=${params.threadId}, error=${error}`
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
63
|
+
),
|
|
64
|
+
),
|
|
65
|
+
},
|
|
66
|
+
]
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function runSocialAgentTurnEffect(params: {
|
|
72
|
+
agentId: string
|
|
73
|
+
mode: 'fixedThreadMode' | 'threadMode'
|
|
74
|
+
threadType: 'group'
|
|
75
|
+
onboardingActive: boolean
|
|
76
|
+
linearInstalled: boolean
|
|
77
|
+
systemWorkspaceDetails?: string
|
|
78
|
+
preSeededMemoriesSection?: string
|
|
79
|
+
retrievedKnowledgeSection?: string
|
|
80
|
+
learnedSkillsSection?: string
|
|
81
|
+
userMessageText?: string
|
|
82
|
+
additionalInstructionSections?: string[]
|
|
83
|
+
prompt: string
|
|
84
|
+
tools: ToolSet
|
|
85
|
+
abortSignal: AbortSignal
|
|
86
|
+
}): Effect.Effect<{ text: string; displayName: string }, SocialChatAgentRunnerError> {
|
|
87
|
+
return Effect.gen(function* () {
|
|
88
|
+
const runtimeConfig = getAgentRuntimeConfig({
|
|
89
|
+
agentId: params.agentId,
|
|
90
|
+
threadType: params.threadType,
|
|
91
|
+
mode: params.mode,
|
|
92
|
+
onboardingActive: params.onboardingActive,
|
|
93
|
+
linearInstalled: params.linearInstalled,
|
|
94
|
+
systemWorkspaceDetails: params.systemWorkspaceDetails,
|
|
95
|
+
preSeededMemoriesSection: params.preSeededMemoriesSection,
|
|
96
|
+
retrievedKnowledgeSection: params.retrievedKnowledgeSection,
|
|
97
|
+
learnedSkillsSection: params.learnedSkillsSection,
|
|
98
|
+
userMessageText: params.userMessageText,
|
|
99
|
+
ruleOptions: {
|
|
100
|
+
includeMemr3Rule: SOCIAL_CHAT_RETRIEVAL_TOOL_NAMES.some((toolName) => toolName in params.tools),
|
|
101
|
+
includeDomainReasoningFallbackRule: params.agentId === 'cpo' || params.agentId === 'mentor',
|
|
102
|
+
},
|
|
103
|
+
additionalInstructionSections: params.additionalInstructionSections,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const agentFactoryEntry = getResolvedAgentFactoryConfig().createAgent[runtimeConfig.id]
|
|
107
|
+
if (typeof agentFactoryEntry !== 'function') {
|
|
108
|
+
return yield* new SocialChatAgentRunnerError({
|
|
109
|
+
message: `Social chat agent factory is not configured for ${runtimeConfig.id}`,
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
const agentFactory = agentFactoryEntry
|
|
113
|
+
|
|
114
|
+
const agent = agentFactory({
|
|
115
|
+
mode: params.mode,
|
|
116
|
+
tools: params.tools,
|
|
117
|
+
extraInstructions: runtimeConfig.extraInstructions,
|
|
118
|
+
stopWhen: [stepCountIs(runtimeConfig.maxSteps)],
|
|
119
|
+
})
|
|
120
|
+
const response = yield* Effect.tryPromise({
|
|
121
|
+
try: () => agent.generate({ prompt: params.prompt, abortSignal: params.abortSignal }),
|
|
122
|
+
catch: (cause) =>
|
|
123
|
+
new SocialChatAgentRunnerError({ message: `Social chat agent ${params.agentId} generate() failed.`, cause }),
|
|
124
|
+
})
|
|
125
|
+
const text = response.text.trim()
|
|
126
|
+
if (!text) {
|
|
127
|
+
return yield* new SocialChatAgentRunnerError({
|
|
128
|
+
message: `Social chat agent ${params.agentId} returned an empty response.`,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
text,
|
|
134
|
+
displayName:
|
|
135
|
+
typeof runtimeConfig.displayName === 'string' && runtimeConfig.displayName.trim()
|
|
136
|
+
? runtimeConfig.displayName.trim()
|
|
137
|
+
: params.agentId,
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function runSocialAgentTurn(params: {
|
|
143
|
+
agentId: string
|
|
144
|
+
mode: 'fixedThreadMode' | 'threadMode'
|
|
145
|
+
threadType: 'group'
|
|
146
|
+
onboardingActive: boolean
|
|
147
|
+
linearInstalled: boolean
|
|
148
|
+
systemWorkspaceDetails?: string
|
|
149
|
+
preSeededMemoriesSection?: string
|
|
150
|
+
retrievedKnowledgeSection?: string
|
|
151
|
+
learnedSkillsSection?: string
|
|
152
|
+
userMessageText?: string
|
|
153
|
+
additionalInstructionSections?: string[]
|
|
154
|
+
prompt: string
|
|
155
|
+
tools: ToolSet
|
|
156
|
+
abortSignal: AbortSignal
|
|
157
|
+
}): Promise<{ text: string; displayName: string }> {
|
|
158
|
+
return Effect.runPromise(runSocialAgentTurnEffect(params))
|
|
159
|
+
}
|