@mastra/code-sdk 1.6.1-alpha.1 → 1.7.0-alpha.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/dist/agents/instructions.d.ts +14 -2
- package/dist/agents/instructions.d.ts.map +1 -1
- package/dist/agents/instructions.js +12 -4
- package/dist/agents/instructions.js.map +1 -1
- package/dist/agents/memory.d.ts +15 -4
- package/dist/agents/memory.d.ts.map +1 -1
- package/dist/agents/memory.js +29 -17
- package/dist/agents/memory.js.map +1 -1
- package/dist/agents/prompts/index.d.ts +3 -0
- package/dist/agents/prompts/index.d.ts.map +1 -1
- package/dist/agents/prompts/index.js +9 -0
- package/dist/agents/prompts/index.js.map +1 -1
- package/dist/agents/prompts/tool-guidance.d.ts +3 -0
- package/dist/agents/prompts/tool-guidance.d.ts.map +1 -1
- package/dist/agents/prompts/tool-guidance.js +31 -1
- package/dist/agents/prompts/tool-guidance.js.map +1 -1
- package/dist/agents/subagents/execute.d.ts +3 -0
- package/dist/agents/subagents/execute.d.ts.map +1 -0
- package/dist/agents/subagents/execute.js +27 -0
- package/dist/agents/subagents/execute.js.map +1 -0
- package/dist/agents/subagents/explore.d.ts +3 -0
- package/dist/agents/subagents/explore.d.ts.map +1 -0
- package/dist/agents/subagents/explore.js +18 -0
- package/dist/agents/subagents/explore.js.map +1 -0
- package/dist/agents/subagents/plan.d.ts +3 -0
- package/dist/agents/subagents/plan.d.ts.map +1 -0
- package/dist/agents/subagents/plan.js +18 -0
- package/dist/agents/subagents/plan.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -5
- package/dist/index.js.map +1 -1
- package/dist/knowledge-inspector.d.ts.map +1 -1
- package/dist/knowledge-inspector.js +16 -9
- package/dist/knowledge-inspector.js.map +1 -1
- package/dist/knowledge-scope.d.ts +35 -0
- package/dist/knowledge-scope.d.ts.map +1 -0
- package/dist/knowledge-scope.js +47 -0
- package/dist/knowledge-scope.js.map +1 -0
- package/dist/providers/amazon-bedrock-gateway.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock-gateway.js +1 -0
- package/dist/providers/amazon-bedrock-gateway.js.map +1 -1
- package/dist/providers/amazon-bedrock.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock.js +20 -1
- package/dist/providers/amazon-bedrock.js.map +1 -1
- package/dist/utils/slash-command-processor.d.ts.map +1 -1
- package/dist/utils/slash-command-processor.js +7 -3
- package/dist/utils/slash-command-processor.js.map +1 -1
- package/package.json +10 -10
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knowledge-inspector.js","names":["#knowledge","#session","#handles","#cursors","#invalidateIdnode","#binding","#scope","#consumeCursor","#recordSummary","#sampledRelationshipCounts","#assertStable","#mintCursor","#rankedNodeSnapshot","#readHandle","#assertVisible","#outgoingTargets","#incomingParents","#activityRecord","#fingerprint","#identityKey","#mintHandle","#outgoingNodeRecords","#incomingParentRecords","#pruneOpaqueEntries"],"sources":["../src/knowledge-inspector.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto';\n\nimport type { Session } from '@mastra/core/agent-controller';\nimport { createKnowledgeNodeCursor, isKnowledgeScopeVisible, parseKnowledgeWikilinks } from '@mastra/core/storage';\nimport type {\n KnowledgeActivityEvent,\n KnowledgeRecord,\n KnowledgeNode,\n KnowledgeScope,\n KnowledgeStorage,\n MastraCompositeStore,\n} from '@mastra/core/storage';\n\nimport type { MastraCodeState } from './schema.js';\n\nexport type KnowledgeInspectorScopeLevel = 'org' | 'resource' | 'thread';\nexport type KnowledgeInspectorRecordType = 'node';\nexport type KnowledgeInspectorNodeSort = 'relevant' | 'recent' | 'connected';\n\nexport interface KnowledgeInspectorScopeRoot {\n level: KnowledgeInspectorScopeLevel;\n id?: string;\n available: boolean;\n reason?: string;\n}\n\nexport interface KnowledgeInspectorScopeTree {\n identityKey: string;\n defaultLevel: 'resource';\n roots: KnowledgeInspectorScopeRoot[];\n}\n\nexport interface KnowledgeInspectorScopeBadge {\n level: KnowledgeInspectorScopeLevel;\n id: string;\n}\n\nexport interface KnowledgeInspectorRelationshipCounts {\n records: number;\n outgoing: number;\n incoming: number;\n sampled: boolean;\n}\n\nexport interface KnowledgeInspectorNodeSummary {\n handle: string;\n type: KnowledgeInspectorRecordType;\n name: string;\n kind?: string;\n scope: KnowledgeInspectorScopeBadge;\n version: number;\n updatedAt: string;\n relationshipCounts?: KnowledgeInspectorRelationshipCounts;\n}\n\nexport interface KnowledgeInspectorRecordSummary {\n text: string;\n scope: KnowledgeInspectorScopeBadge;\n sourceThreadId: string;\n capturedAt: string;\n when?: string;\n}\n\nexport interface KnowledgeInspectorNodeList {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n nodes: KnowledgeInspectorNodeSummary[];\n nextCursor?: string;\n sort?: KnowledgeInspectorNodeSort;\n coverage?: 'exact' | 'recent-window';\n}\n\nexport interface KnowledgeInspectorRelationshipPreview {\n nodes: KnowledgeInspectorNodeSummary[];\n partial: boolean;\n}\n\nexport interface KnowledgeInspectorNodeDetail {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n node: KnowledgeInspectorNodeSummary;\n records: KnowledgeInspectorRecordSummary[];\n recordsNextCursor?: string;\n mentioningRecords: KnowledgeInspectorRecordSummary[];\n mentioningRecordsNextCursor?: string;\n outgoingTargets: KnowledgeInspectorRelationshipPreview;\n incomingParents: KnowledgeInspectorRelationshipPreview;\n relationshipCounts: KnowledgeInspectorRelationshipCounts;\n content?: string;\n contentTruncated: boolean;\n links: Array<{ label: string; node?: KnowledgeInspectorNodeSummary }>;\n}\n\nexport interface KnowledgeInspectorActivityEvent {\n action: KnowledgeActivityEvent['action'];\n recordType: KnowledgeActivityEvent['recordType'];\n scope: KnowledgeInspectorScopeBadge;\n sourceThreadId?: string;\n createdAt: string;\n record?: KnowledgeInspectorNodeSummary;\n}\n\nexport interface KnowledgeInspectorActivityList {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n events: KnowledgeInspectorActivityEvent[];\n nextCursor?: string;\n}\n\nexport interface KnowledgeInspector {\n getScopeTree(): Promise<KnowledgeInspectorScopeTree>;\n listNodes(input: {\n level: KnowledgeInspectorScopeLevel;\n namePrefix?: string;\n kind?: string;\n sort?: KnowledgeInspectorNodeSort;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorNodeList>;\n getNode(input: {\n handle: string;\n recordsCursor?: string;\n mentioningRecordsCursor?: string;\n recordLimit?: number;\n }): Promise<KnowledgeInspectorNodeDetail>;\n listActivity(input: {\n level: KnowledgeInspectorScopeLevel;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorActivityList>;\n}\n\nexport class KnowledgeInspectorError extends Error {\n constructor(\n readonly code: 'unavailable' | 'invalid-handle' | 'stale-handle' | 'invalid-cursor' | 'not-visible',\n message: string,\n ) {\n super(message);\n this.name = 'KnowledgeInspectorError';\n }\n}\n\ninterface Binding {\n ownerId: string;\n resourceId: string;\n threadId?: string;\n fingerprint: string;\n identityKey: string;\n}\n\ninterface HandleEntry {\n identityKey: string;\n level: KnowledgeInspectorScopeLevel;\n type: KnowledgeInspectorRecordType;\n recordId: string;\n expiresAt: number;\n}\n\ninterface CursorEntry {\n identityKey: string;\n level: KnowledgeInspectorScopeLevel;\n kind: 'node' | 'ranked-node' | 'records' | 'mentioning-records' | 'activity';\n value: string;\n filters?: { namePrefix?: string; kind?: string; sort?: KnowledgeInspectorNodeSort };\n expiresAt: number;\n}\n\ninterface RankedNodeSnapshot {\n offset: number;\n entries: { id: string; degree: number; counts: KnowledgeInspectorRelationshipCounts }[];\n}\n\ninterface RelationshipRecords {\n nodes: KnowledgeNode[];\n truncated: boolean;\n}\n\nconst HANDLE_TTL_MS = 5 * 60_000;\nconst MAX_OPAQUE_ENTRIES = 1_000;\nconst DEFAULT_RECORD_LIMIT = 50;\nconst MAX_RECORD_LIMIT = 50;\nconst DEFAULT_FACT_LIMIT = 25;\nconst MAX_FACT_LIMIT = 100;\nconst DEFAULT_ACTIVITY_LIMIT = 20;\nconst MAX_ACTIVITY_LIMIT = 100;\nconst MAX_RELATED_RECORDS = 25;\nconst MAX_RANK_CANDIDATES = 50;\nconst MAX_RANK_FACTS = 100;\nconst RRF_K = 60;\nconst MAX_NODE_CONTENT_BYTES = 32 * 1024;\n\nfunction opaqueToken(): string {\n return randomBytes(24).toString('base64url');\n}\n\nfunction boundedLimit(value: number | undefined, fallback: number, maximum: number): number {\n if (value === undefined) return fallback;\n if (!Number.isInteger(value) || value < 1) return fallback;\n return Math.min(value, maximum);\n}\n\nfunction scopeBadge(scope: KnowledgeScope): KnowledgeInspectorScopeBadge {\n const entry = scope.at(-1);\n if (!entry) throw new KnowledgeInspectorError('unavailable', 'Knowledge record has no scope.');\n const separator = entry.indexOf(':');\n return {\n level: entry.slice(0, separator) as KnowledgeInspectorScopeLevel,\n id: entry.slice(separator + 1),\n };\n}\n\nfunction knowledgeSummary(record: KnowledgeRecord): KnowledgeInspectorRecordSummary {\n return {\n text: record.text,\n scope: scopeBadge(record.scope),\n sourceThreadId: record.sourceThreadId,\n capturedAt: record.capturedAt.toISOString(),\n when: record.when?.toISOString(),\n };\n}\n\nfunction truncateUtf8(value: string, maxBytes: number): { value: string; truncated: boolean } {\n const encoded = Buffer.from(value);\n if (encoded.byteLength <= maxBytes) return { value, truncated: false };\n let end = maxBytes;\n let truncated = encoded.subarray(0, end).toString('utf8');\n while (Buffer.byteLength(truncated) > maxBytes) {\n truncated = encoded.subarray(0, --end).toString('utf8');\n }\n return { value: truncated, truncated: true };\n}\n\nclass ScopedKnowledgeInspector implements KnowledgeInspector {\n readonly #knowledge: KnowledgeStorage;\n readonly #session: Session<MastraCodeState>;\n readonly #handles = new Map<string, HandleEntry>();\n readonly #cursors = new Map<string, CursorEntry>();\n #fingerprint?: string;\n #identityKey = opaqueToken();\n\n constructor(input: { knowledge: KnowledgeStorage; session: Session<MastraCodeState> }) {\n this.#knowledge = input.knowledge;\n this.#session = input.session;\n this.#session.subscribe(event => {\n if (event.type === 'thread_changed' || event.type === 'thread_created' || event.type === 'thread_deleted') {\n this.#invalidateIdnode();\n }\n });\n }\n\n async getScopeTree(): Promise<KnowledgeInspectorScopeTree> {\n const binding = await this.#binding();\n return {\n identityKey: binding.identityKey,\n defaultLevel: 'resource',\n roots: [\n { level: 'org', id: binding.ownerId, available: true },\n { level: 'resource', id: binding.resourceId, available: true },\n binding.threadId\n ? { level: 'thread', id: binding.threadId, available: true }\n : { level: 'thread', available: false, reason: 'No active thread belongs to this project.' },\n ],\n };\n }\n\n async listNodes(input: {\n level: KnowledgeInspectorScopeLevel;\n namePrefix?: string;\n kind?: string;\n sort?: KnowledgeInspectorNodeSort;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorNodeList> {\n const binding = await this.#binding();\n const scope = this.#scope(binding, input.level);\n const limit = boundedLimit(input.limit, DEFAULT_RECORD_LIMIT, MAX_RECORD_LIMIT);\n const sort = input.sort ?? 'relevant';\n if (sort === 'recent') {\n const cursor = this.#consumeCursor(input.cursor, binding, input.level, 'node', {\n namePrefix: input.namePrefix,\n kind: input.kind,\n sort,\n });\n const records = await this.#knowledge.listNodes({\n scope,\n namePrefix: input.namePrefix,\n kind: input.kind,\n cursor,\n limit,\n });\n const nodes: KnowledgeInspectorNodeSummary[] = await Promise.all(\n records.map(async record => ({\n ...this.#recordSummary(record, binding, input.level),\n relationshipCounts: (await this.#sampledRelationshipCounts(record, scope)).counts,\n })),\n );\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n nodes,\n nextCursor:\n records.length === limit\n ? this.#mintCursor(\n binding,\n input.level,\n 'node',\n createKnowledgeNodeCursor(records.at(-1)!, {\n namePrefix: input.namePrefix,\n kind: input.kind,\n }),\n {\n namePrefix: input.namePrefix,\n kind: input.kind,\n sort,\n },\n )\n : undefined,\n sort,\n coverage: 'exact',\n };\n }\n\n const filters = { namePrefix: input.namePrefix, kind: input.kind, sort };\n const encodedSnapshot = this.#consumeCursor(input.cursor, binding, input.level, 'ranked-node', filters);\n const snapshot = encodedSnapshot\n ? (JSON.parse(encodedSnapshot) as RankedNodeSnapshot)\n : await this.#rankedNodeSnapshot(scope, input.namePrefix, input.kind, sort);\n const page = snapshot.entries.slice(snapshot.offset, snapshot.offset + limit);\n const nodes: KnowledgeInspectorNodeSummary[] = [];\n for (const entry of page) {\n const node = await this.#knowledge.getNode(entry.id);\n if (!node || !isKnowledgeScopeVisible(node.scope, scope)) continue;\n nodes.push({ ...this.#recordSummary(node, binding, input.level), relationshipCounts: entry.counts });\n }\n const nextOffset = snapshot.offset + limit;\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n nodes,\n nextCursor:\n nextOffset < snapshot.entries.length\n ? this.#mintCursor(\n binding,\n input.level,\n 'ranked-node',\n JSON.stringify({ ...snapshot, offset: nextOffset } satisfies RankedNodeSnapshot),\n filters,\n )\n : undefined,\n sort,\n coverage: 'recent-window',\n };\n }\n\n async getNode(input: {\n handle: string;\n recordsCursor?: string;\n mentioningRecordsCursor?: string;\n recordLimit?: number;\n }): Promise<KnowledgeInspectorNodeDetail> {\n const binding = await this.#binding();\n const handle = this.#readHandle(input.handle, binding, 'node');\n const scope = this.#scope(binding, handle.level);\n const node = await this.#knowledge.getNode(handle.recordId);\n this.#assertVisible(node, scope);\n const limit = boundedLimit(input.recordLimit, DEFAULT_FACT_LIMIT, MAX_FACT_LIMIT);\n const recordsAfter = this.#consumeCursor(input.recordsCursor, binding, handle.level, 'records');\n const mentioningAfter = this.#consumeCursor(\n input.mentioningRecordsCursor,\n binding,\n handle.level,\n 'mentioning-records',\n );\n const [recordsResult, mentioningResult] = await Promise.all([\n this.#knowledge.listKnowledgeAbout({ node: node.id, scope, after: recordsAfter, limit }),\n this.#knowledge.listKnowledgeMentioning({ node: node.id, scope, after: mentioningAfter, limit }),\n ]);\n const mentioningRecords = mentioningResult.records.filter(record => record.node !== node.id);\n const content = truncateUtf8(node.content ?? '', MAX_NODE_CONTENT_BYTES);\n const [outgoingTargets, incomingParents, relationship] = await Promise.all([\n this.#outgoingTargets(node, recordsResult.records, scope, binding, handle.level),\n this.#incomingParents(node, mentioningRecords, scope, binding, handle.level),\n this.#sampledRelationshipCounts(node, scope),\n ]);\n const links = await Promise.all(\n parseKnowledgeWikilinks(content.value)\n .slice(0, MAX_RELATED_RECORDS)\n .map(async label => {\n const target = await this.#knowledge.resolveNode({ name: label, scope });\n return {\n label,\n node: target ? this.#recordSummary(target, binding, handle.level) : undefined,\n };\n }),\n );\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: handle.level,\n node: { ...this.#recordSummary(node, binding, handle.level), relationshipCounts: relationship.counts },\n records: recordsResult.records.map(knowledgeSummary),\n recordsNextCursor: recordsResult.nextCursor\n ? this.#mintCursor(binding, handle.level, 'records', recordsResult.nextCursor)\n : undefined,\n mentioningRecords: mentioningRecords.map(knowledgeSummary),\n mentioningRecordsNextCursor: mentioningResult.nextCursor\n ? this.#mintCursor(binding, handle.level, 'mentioning-records', mentioningResult.nextCursor)\n : undefined,\n outgoingTargets: {\n nodes: outgoingTargets.nodes,\n partial: outgoingTargets.partial || Boolean(recordsResult.nextCursor),\n },\n incomingParents: {\n nodes: incomingParents.nodes,\n partial: incomingParents.partial || Boolean(mentioningResult.nextCursor),\n },\n relationshipCounts: relationship.counts,\n content: node.content === undefined ? undefined : content.value,\n contentTruncated: content.truncated,\n links,\n };\n }\n\n async listActivity(input: {\n level: KnowledgeInspectorScopeLevel;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorActivityList> {\n const binding = await this.#binding();\n const scope = this.#scope(binding, input.level);\n const limit = boundedLimit(input.limit, DEFAULT_ACTIVITY_LIMIT, MAX_ACTIVITY_LIMIT);\n const after = this.#consumeCursor(input.cursor, binding, input.level, 'activity');\n const events = await this.#knowledge.listActivity({ scope, after, limit });\n const activityEvents: KnowledgeInspectorActivityEvent[] = [];\n for (const event of events) {\n const record = await this.#activityRecord(event, scope, binding, input.level);\n activityEvents.push({\n action: event.action,\n recordType: event.recordType,\n scope: scopeBadge(event.scope),\n sourceThreadId: record ? event.sourceThreadId : undefined,\n createdAt: event.createdAt.toISOString(),\n record,\n });\n }\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n events: activityEvents,\n nextCursor:\n events.length === limit ? this.#mintCursor(binding, input.level, 'activity', events.at(-1)!.id) : undefined,\n };\n }\n\n async #binding(): Promise<Binding> {\n const ownerId = this.#session.identity.getOwnerId();\n const resourceId = this.#session.identity.getResourceId();\n if (!ownerId || !resourceId) {\n throw new KnowledgeInspectorError('unavailable', 'Knowledge inspection requires an active owner and project.');\n }\n const activeThreadId = this.#session.thread.getId() ?? undefined;\n const thread = activeThreadId ? await this.#session.thread.getById({ threadId: activeThreadId }) : null;\n const threadId = thread?.resourceId === resourceId ? thread.id : undefined;\n const fingerprint = `${ownerId}\\0${resourceId}\\0${threadId ?? ''}`;\n if (this.#fingerprint !== fingerprint) {\n this.#fingerprint = fingerprint;\n this.#identityKey = opaqueToken();\n this.#handles.clear();\n this.#cursors.clear();\n }\n return { ownerId, resourceId, threadId, fingerprint, identityKey: this.#identityKey };\n }\n\n #scope(binding: Binding, level: KnowledgeInspectorScopeLevel): KnowledgeScope {\n if (level === 'org') return [`org:${binding.ownerId}`];\n const scope = [`org:${binding.ownerId}`, `resource:${binding.resourceId}`];\n if (level === 'resource') return scope;\n if (!binding.threadId) {\n throw new KnowledgeInspectorError('unavailable', 'The active thread does not belong to this project.');\n }\n return [...scope, `thread:${binding.threadId}`];\n }\n\n async #assertStable(binding: Binding): Promise<void> {\n const current = await this.#binding();\n if (current.identityKey !== binding.identityKey || current.fingerprint !== binding.fingerprint) {\n throw new KnowledgeInspectorError('stale-handle', 'Knowledge scope changed while the request was running.');\n }\n }\n\n #assertVisible<T extends KnowledgeNode>(record: T | null, scope: KnowledgeScope): asserts record is T {\n if (!record || !isKnowledgeScopeVisible(record.scope, scope)) {\n throw new KnowledgeInspectorError('not-visible', 'Knowledge record is not visible in the selected scope.');\n }\n }\n\n #recordSummary(\n record: KnowledgeNode,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): KnowledgeInspectorNodeSummary {\n const type: KnowledgeInspectorRecordType = 'node';\n return {\n handle: this.#mintHandle(binding, level, type, record.id),\n type,\n name: record.name,\n kind: record.kind,\n scope: scopeBadge(record.scope),\n version: record.version,\n updatedAt: record.updatedAt.toISOString(),\n };\n }\n\n async #rankedNodeSnapshot(\n scope: KnowledgeScope,\n namePrefix: string | undefined,\n kind: string | undefined,\n sort: Exclude<KnowledgeInspectorNodeSort, 'recent'>,\n ): Promise<RankedNodeSnapshot> {\n const records = await this.#knowledge.listNodes({\n scope,\n namePrefix,\n kind,\n limit: MAX_RANK_CANDIDATES,\n });\n const ranked = await Promise.all(\n records.map(async (node, recencyRank) => ({\n node,\n recencyRank,\n ...(await this.#sampledRelationshipCounts(node, scope)),\n })),\n );\n const connected = [...ranked].sort(\n (a, b) => b.degree - a.degree || a.recencyRank - b.recencyRank || a.node.id.localeCompare(b.node.id),\n );\n const connectedRank = new Map(connected.map((entry, index) => [entry.node.id, index]));\n const ordered =\n sort === 'connected'\n ? connected\n : [...ranked].sort((a, b) => {\n const aScore = 1 / (RRF_K + a.recencyRank + 1) + 1 / (RRF_K + connectedRank.get(a.node.id)! + 1);\n const bScore = 1 / (RRF_K + b.recencyRank + 1) + 1 / (RRF_K + connectedRank.get(b.node.id)! + 1);\n return bScore - aScore || a.recencyRank - b.recencyRank || a.node.id.localeCompare(b.node.id);\n });\n return {\n offset: 0,\n entries: ordered.map(entry => ({ id: entry.node.id, degree: entry.degree, counts: entry.counts })),\n };\n }\n\n async #sampledRelationshipCounts(\n node: KnowledgeNode,\n scope: KnowledgeScope,\n ): Promise<{ degree: number; counts: KnowledgeInspectorRelationshipCounts }> {\n const [aboutResult, mentioningResult] = await Promise.all([\n this.#knowledge.listKnowledgeAbout({ node: node.id, scope, limit: MAX_RANK_FACTS }),\n this.#knowledge.listKnowledgeMentioning({ node: node.id, scope, limit: MAX_RANK_FACTS }),\n ]);\n const [outgoing, incoming] = await Promise.all([\n this.#outgoingNodeRecords(node, aboutResult.records, scope),\n this.#incomingParentRecords(node, mentioningResult.records, scope),\n ]);\n const mentioningRecords = mentioningResult.records.filter(record => record.node !== node.id);\n const degree = new Set([...outgoing.nodes, ...incoming.nodes].map(record => record.id)).size;\n return {\n degree,\n counts: {\n records: aboutResult.records.length + mentioningRecords.length,\n outgoing: outgoing.nodes.length,\n incoming: incoming.nodes.length,\n sampled: Boolean(\n aboutResult.nextCursor || mentioningResult.nextCursor || outgoing.truncated || incoming.truncated,\n ),\n },\n };\n }\n\n async #outgoingNodeRecords(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n ): Promise<RelationshipRecords> {\n const related = new Map<string, KnowledgeNode>();\n let truncated = false;\n const sources = [current.content ?? '', ...records.map(record => record.text)];\n for (const source of sources) {\n for (const name of parseKnowledgeWikilinks(source)) {\n if (related.size >= MAX_RELATED_RECORDS) {\n truncated = true;\n break;\n }\n const node = await this.#knowledge.resolveNode({ name, scope });\n if (node && node.id !== current.id && isKnowledgeScopeVisible(node.scope, scope)) {\n related.set(node.id, node);\n }\n }\n if (truncated) break;\n }\n return { nodes: [...related.values()], truncated };\n }\n\n async #incomingParentRecords(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n ): Promise<RelationshipRecords> {\n const related = new Map<string, KnowledgeNode>();\n let truncated = false;\n for (const record of records) {\n if (record.node === current.id || related.has(record.node)) continue;\n if (related.size >= MAX_RELATED_RECORDS) {\n truncated = true;\n break;\n }\n const node = await this.#knowledge.getNode(record.node);\n if (node && isKnowledgeScopeVisible(node.scope, scope)) related.set(node.id, node);\n }\n return { nodes: [...related.values()], truncated };\n }\n\n async #outgoingTargets(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorRelationshipPreview> {\n const related = await this.#outgoingNodeRecords(current, records, scope);\n return {\n nodes: related.nodes.map(node => this.#recordSummary(node, binding, level)),\n partial: related.truncated,\n };\n }\n\n async #incomingParents(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorRelationshipPreview> {\n const related = await this.#incomingParentRecords(current, records, scope);\n return {\n nodes: related.nodes.map(node => this.#recordSummary(node, binding, level)),\n partial: related.truncated,\n };\n }\n\n async #activityRecord(\n event: KnowledgeActivityEvent,\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorNodeSummary | undefined> {\n if (event.recordType === 'node') {\n const node = await this.#knowledge.getNode(event.recordId);\n return node && isKnowledgeScopeVisible(node.scope, scope) ? this.#recordSummary(node, binding, level) : undefined;\n }\n const record = await this.#knowledge.getKnowledge({ id: event.recordId, includeDeleted: true });\n if (!record || !isKnowledgeScopeVisible(record.scope, scope)) return undefined;\n const node = await this.#knowledge.getNode(record.node);\n return node && isKnowledgeScopeVisible(node.scope, scope) ? this.#recordSummary(node, binding, level) : undefined;\n }\n\n #mintHandle(\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n type: KnowledgeInspectorRecordType,\n recordId: string,\n ): string {\n this.#pruneOpaqueEntries();\n const token = opaqueToken();\n this.#handles.set(token, {\n identityKey: binding.identityKey,\n level,\n type,\n recordId,\n expiresAt: Date.now() + HANDLE_TTL_MS,\n });\n return token;\n }\n\n #readHandle(handle: string, binding: Binding, expectedType: KnowledgeInspectorRecordType): HandleEntry {\n const entry = this.#handles.get(handle);\n if (!entry || entry.expiresAt < Date.now() || entry.type !== expectedType) {\n throw new KnowledgeInspectorError('invalid-handle', 'Knowledge record handle is invalid or expired.');\n }\n if (entry.identityKey !== binding.identityKey) {\n throw new KnowledgeInspectorError('stale-handle', 'Knowledge record handle belongs to a previous scope.');\n }\n return entry;\n }\n\n #mintCursor(\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n kind: CursorEntry['kind'],\n value: string,\n filters?: CursorEntry['filters'],\n ): string {\n this.#pruneOpaqueEntries();\n const token = opaqueToken();\n this.#cursors.set(token, {\n identityKey: binding.identityKey,\n level,\n kind,\n value,\n filters,\n expiresAt: Date.now() + HANDLE_TTL_MS,\n });\n return token;\n }\n\n #consumeCursor(\n cursor: string | undefined,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n kind: CursorEntry['kind'],\n filters?: CursorEntry['filters'],\n ): string | undefined {\n if (!cursor) return undefined;\n const entry = this.#cursors.get(cursor);\n if (\n !entry ||\n entry.expiresAt < Date.now() ||\n entry.identityKey !== binding.identityKey ||\n entry.level !== level ||\n entry.kind !== kind ||\n entry.filters?.namePrefix !== filters?.namePrefix ||\n entry.filters?.kind !== filters?.kind ||\n entry.filters?.sort !== filters?.sort\n ) {\n throw new KnowledgeInspectorError(\n 'invalid-cursor',\n 'Knowledge cursor does not match the active scope and filters.',\n );\n }\n return entry.value;\n }\n\n #pruneOpaqueEntries(): void {\n const now = Date.now();\n for (const [token, entry] of this.#handles) {\n if (entry.expiresAt < now) this.#handles.delete(token);\n }\n for (const [token, entry] of this.#cursors) {\n if (entry.expiresAt < now) this.#cursors.delete(token);\n }\n while (this.#handles.size + this.#cursors.size >= MAX_OPAQUE_ENTRIES) {\n const handle = this.#handles.keys().next().value;\n if (handle) this.#handles.delete(handle);\n else {\n const cursor = this.#cursors.keys().next().value;\n if (cursor) this.#cursors.delete(cursor);\n else break;\n }\n }\n }\n\n #invalidateIdnode(): void {\n this.#fingerprint = undefined;\n this.#identityKey = opaqueToken();\n this.#handles.clear();\n this.#cursors.clear();\n }\n}\n\nexport async function createKnowledgeInspector(input: {\n storage: MastraCompositeStore;\n session: Session<MastraCodeState>;\n}): Promise<KnowledgeInspector | undefined> {\n const knowledge = await input.storage.getStore('knowledge');\n return knowledge ? new ScopedKnowledgeInspector({ knowledge, session: input.session }) : undefined;\n}\n"],"mappings":";;;AAoIA,IAAa,0BAAb,cAA6C,MAAM;CAEtC;CADX,YACE,MACA,SACA;EACA,MAAM,OAAO;EAHJ,KAAA,OAAA;EAIT,KAAK,OAAO;CACd;AACF;AAqCA,MAAM,gBAAgB,IAAI;AAC1B,MAAM,qBAAqB;AAC3B,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AACzB,MAAM,qBAAqB;AAC3B,MAAM,iBAAiB;AACvB,MAAM,yBAAyB;AAC/B,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAC5B,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB;AACvB,MAAM,QAAQ;AACd,MAAM,yBAAyB,KAAK;AAEpC,SAAS,cAAsB;CAC7B,OAAO,YAAY,EAAE,CAAC,CAAC,SAAS,WAAW;AAC7C;AAEA,SAAS,aAAa,OAA2B,UAAkB,SAAyB;CAC1F,IAAI,UAAU,KAAA,GAAW,OAAO;CAChC,IAAI,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,GAAG,OAAO;CAClD,OAAO,KAAK,IAAI,OAAO,OAAO;AAChC;AAEA,SAAS,WAAW,OAAqD;CACvE,MAAM,QAAQ,MAAM,GAAG,EAAE;CACzB,IAAI,CAAC,OAAO,MAAM,IAAI,wBAAwB,eAAe,gCAAgC;CAC7F,MAAM,YAAY,MAAM,QAAQ,GAAG;CACnC,OAAO;EACL,OAAO,MAAM,MAAM,GAAG,SAAS;EAC/B,IAAI,MAAM,MAAM,YAAY,CAAC;CAC/B;AACF;AAEA,SAAS,iBAAiB,QAA0D;CAClF,OAAO;EACL,MAAM,OAAO;EACb,OAAO,WAAW,OAAO,KAAK;EAC9B,gBAAgB,OAAO;EACvB,YAAY,OAAO,WAAW,YAAY;EAC1C,MAAM,OAAO,MAAM,YAAY;CACjC;AACF;AAEA,S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K,MAAM,CAAC,OAAO,UAAU,KAAKD,UAChC,IAAI,MAAM,YAAY,KAAK,KAAKA,SAAS,OAAO,KAAK;EAEvD,KAAK,MAAM,CAAC,OAAO,UAAU,KAAKC,UAChC,IAAI,MAAM,YAAY,KAAK,KAAKA,SAAS,OAAO,KAAK;EAEvD,OAAO,KAAKD,SAAS,OAAO,KAAKC,SAAS,QAAQ,oBAAoB;GACpE,MAAM,SAAS,KAAKD,SAAS,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GAC3C,IAAI,QAAQ,KAAKA,SAAS,OAAO,MAAM;QAClC;IACH,MAAM,SAAS,KAAKC,SAAS,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;IAC3C,IAAI,QAAQ,KAAKA,SAAS,OAAO,MAAM;SAClC;GACP;EACF;CACF;CAEA,oBAA0B;EACxB,KAAKe,eAAe,KAAA;EACpB,KAAKC,eAAe,YAAY;EAChC,KAAKjB,SAAS,MAAM;EACpB,KAAKC,SAAS,MAAM;CACtB;AACF;AAEA,eAAsB,yBAAyB,OAGH;CAC1C,MAAM,YAAY,MAAM,MAAM,QAAQ,SAAS,WAAW;CAC1D,OAAO,YAAY,IAAI,yBAAyB;EAAE;EAAW,SAAS,MAAM;CAAQ,CAAC,IAAI,KAAA;AAC3F"}
|
|
1
|
+
{"version":3,"file":"knowledge-inspector.js","names":["#knowledge","#session","#handles","#cursors","#invalidateIdnode","#binding","#scope","#consumeCursor","#recordSummary","#sampledRelationshipCounts","#assertStable","#mintCursor","#rankedNodeSnapshot","#readHandle","#assertVisible","#outgoingTargets","#incomingParents","#activityRecord","#fingerprint","#identityKey","#mintHandle","#outgoingNodeRecords","#incomingParentRecords","#pruneOpaqueEntries"],"sources":["../src/knowledge-inspector.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto';\n\nimport type { Session } from '@mastra/core/agent-controller';\nimport { createKnowledgeNodeCursor, isKnowledgeScopeVisible, parseKnowledgeWikilinks } from '@mastra/core/storage';\nimport type {\n KnowledgeActivityEvent,\n KnowledgeRecord,\n KnowledgeNode,\n KnowledgeScope,\n KnowledgeStorage,\n MastraCompositeStore,\n} from '@mastra/core/storage';\n\nimport { resolveKnowledgeScopeIdentity } from './knowledge-scope.js';\nimport type { MastraCodeState } from './schema.js';\n\nexport type KnowledgeInspectorScopeLevel = 'org' | 'resource' | 'thread';\nexport type KnowledgeInspectorRecordType = 'node';\nexport type KnowledgeInspectorNodeSort = 'relevant' | 'recent' | 'connected';\n\nexport interface KnowledgeInspectorScopeRoot {\n level: KnowledgeInspectorScopeLevel;\n id?: string;\n available: boolean;\n reason?: string;\n}\n\nexport interface KnowledgeInspectorScopeTree {\n identityKey: string;\n defaultLevel: 'resource';\n roots: KnowledgeInspectorScopeRoot[];\n}\n\nexport interface KnowledgeInspectorScopeBadge {\n level: KnowledgeInspectorScopeLevel;\n id: string;\n}\n\nexport interface KnowledgeInspectorRelationshipCounts {\n records: number;\n outgoing: number;\n incoming: number;\n sampled: boolean;\n}\n\nexport interface KnowledgeInspectorNodeSummary {\n handle: string;\n type: KnowledgeInspectorRecordType;\n name: string;\n kind?: string;\n scope: KnowledgeInspectorScopeBadge;\n version: number;\n updatedAt: string;\n relationshipCounts?: KnowledgeInspectorRelationshipCounts;\n}\n\nexport interface KnowledgeInspectorRecordSummary {\n text: string;\n scope: KnowledgeInspectorScopeBadge;\n sourceThreadId: string;\n capturedAt: string;\n when?: string;\n}\n\nexport interface KnowledgeInspectorNodeList {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n nodes: KnowledgeInspectorNodeSummary[];\n nextCursor?: string;\n sort?: KnowledgeInspectorNodeSort;\n coverage?: 'exact' | 'recent-window';\n}\n\nexport interface KnowledgeInspectorRelationshipPreview {\n nodes: KnowledgeInspectorNodeSummary[];\n partial: boolean;\n}\n\nexport interface KnowledgeInspectorNodeDetail {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n node: KnowledgeInspectorNodeSummary;\n records: KnowledgeInspectorRecordSummary[];\n recordsNextCursor?: string;\n mentioningRecords: KnowledgeInspectorRecordSummary[];\n mentioningRecordsNextCursor?: string;\n outgoingTargets: KnowledgeInspectorRelationshipPreview;\n incomingParents: KnowledgeInspectorRelationshipPreview;\n relationshipCounts: KnowledgeInspectorRelationshipCounts;\n content?: string;\n contentTruncated: boolean;\n links: Array<{ label: string; node?: KnowledgeInspectorNodeSummary }>;\n}\n\nexport interface KnowledgeInspectorActivityEvent {\n action: KnowledgeActivityEvent['action'];\n recordType: KnowledgeActivityEvent['recordType'];\n scope: KnowledgeInspectorScopeBadge;\n sourceThreadId?: string;\n createdAt: string;\n record?: KnowledgeInspectorNodeSummary;\n}\n\nexport interface KnowledgeInspectorActivityList {\n identityKey: string;\n scopeLevel: KnowledgeInspectorScopeLevel;\n events: KnowledgeInspectorActivityEvent[];\n nextCursor?: string;\n}\n\nexport interface KnowledgeInspector {\n getScopeTree(): Promise<KnowledgeInspectorScopeTree>;\n listNodes(input: {\n level: KnowledgeInspectorScopeLevel;\n namePrefix?: string;\n kind?: string;\n sort?: KnowledgeInspectorNodeSort;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorNodeList>;\n getNode(input: {\n handle: string;\n recordsCursor?: string;\n mentioningRecordsCursor?: string;\n recordLimit?: number;\n }): Promise<KnowledgeInspectorNodeDetail>;\n listActivity(input: {\n level: KnowledgeInspectorScopeLevel;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorActivityList>;\n}\n\nexport class KnowledgeInspectorError extends Error {\n constructor(\n readonly code: 'unavailable' | 'invalid-handle' | 'stale-handle' | 'invalid-cursor' | 'not-visible',\n message: string,\n ) {\n super(message);\n this.name = 'KnowledgeInspectorError';\n }\n}\n\ninterface Binding {\n /** Org rung shared with the subconscious writers (see knowledge-scope.ts). */\n organizationId: string;\n /** Resource rung of the knowledge scope: the Factory project id, else the session resource. */\n resourceId: string;\n threadId?: string;\n fingerprint: string;\n identityKey: string;\n}\n\ninterface HandleEntry {\n identityKey: string;\n level: KnowledgeInspectorScopeLevel;\n type: KnowledgeInspectorRecordType;\n recordId: string;\n expiresAt: number;\n}\n\ninterface CursorEntry {\n identityKey: string;\n level: KnowledgeInspectorScopeLevel;\n kind: 'node' | 'ranked-node' | 'records' | 'mentioning-records' | 'activity';\n value: string;\n filters?: { namePrefix?: string; kind?: string; sort?: KnowledgeInspectorNodeSort };\n expiresAt: number;\n}\n\ninterface RankedNodeSnapshot {\n offset: number;\n entries: { id: string; degree: number; counts: KnowledgeInspectorRelationshipCounts }[];\n}\n\ninterface RelationshipRecords {\n nodes: KnowledgeNode[];\n truncated: boolean;\n}\n\nconst HANDLE_TTL_MS = 5 * 60_000;\nconst MAX_OPAQUE_ENTRIES = 1_000;\nconst DEFAULT_RECORD_LIMIT = 50;\nconst MAX_RECORD_LIMIT = 50;\nconst DEFAULT_FACT_LIMIT = 25;\nconst MAX_FACT_LIMIT = 100;\nconst DEFAULT_ACTIVITY_LIMIT = 20;\nconst MAX_ACTIVITY_LIMIT = 100;\nconst MAX_RELATED_RECORDS = 25;\nconst MAX_RANK_CANDIDATES = 50;\nconst MAX_RANK_FACTS = 100;\nconst RRF_K = 60;\nconst MAX_NODE_CONTENT_BYTES = 32 * 1024;\n\nfunction opaqueToken(): string {\n return randomBytes(24).toString('base64url');\n}\n\nfunction boundedLimit(value: number | undefined, fallback: number, maximum: number): number {\n if (value === undefined) return fallback;\n if (!Number.isInteger(value) || value < 1) return fallback;\n return Math.min(value, maximum);\n}\n\nfunction scopeBadge(scope: KnowledgeScope): KnowledgeInspectorScopeBadge {\n const entry = scope.at(-1);\n if (!entry) throw new KnowledgeInspectorError('unavailable', 'Knowledge record has no scope.');\n const separator = entry.indexOf(':');\n return {\n level: entry.slice(0, separator) as KnowledgeInspectorScopeLevel,\n id: entry.slice(separator + 1),\n };\n}\n\nfunction knowledgeSummary(record: KnowledgeRecord): KnowledgeInspectorRecordSummary {\n return {\n text: record.text,\n scope: scopeBadge(record.scope),\n sourceThreadId: record.sourceThreadId,\n capturedAt: record.capturedAt.toISOString(),\n when: record.when?.toISOString(),\n };\n}\n\nfunction truncateUtf8(value: string, maxBytes: number): { value: string; truncated: boolean } {\n const encoded = Buffer.from(value);\n if (encoded.byteLength <= maxBytes) return { value, truncated: false };\n let end = maxBytes;\n let truncated = encoded.subarray(0, end).toString('utf8');\n while (Buffer.byteLength(truncated) > maxBytes) {\n truncated = encoded.subarray(0, --end).toString('utf8');\n }\n return { value: truncated, truncated: true };\n}\n\nclass ScopedKnowledgeInspector implements KnowledgeInspector {\n readonly #knowledge: KnowledgeStorage;\n readonly #session: Session<MastraCodeState>;\n readonly #handles = new Map<string, HandleEntry>();\n readonly #cursors = new Map<string, CursorEntry>();\n #fingerprint?: string;\n #identityKey = opaqueToken();\n\n constructor(input: { knowledge: KnowledgeStorage; session: Session<MastraCodeState> }) {\n this.#knowledge = input.knowledge;\n this.#session = input.session;\n this.#session.subscribe(event => {\n if (event.type === 'thread_changed' || event.type === 'thread_created' || event.type === 'thread_deleted') {\n this.#invalidateIdnode();\n }\n });\n }\n\n async getScopeTree(): Promise<KnowledgeInspectorScopeTree> {\n const binding = await this.#binding();\n return {\n identityKey: binding.identityKey,\n defaultLevel: 'resource',\n roots: [\n { level: 'org', id: binding.organizationId, available: true },\n { level: 'resource', id: binding.resourceId, available: true },\n binding.threadId\n ? { level: 'thread', id: binding.threadId, available: true }\n : { level: 'thread', available: false, reason: 'No active thread belongs to this project.' },\n ],\n };\n }\n\n async listNodes(input: {\n level: KnowledgeInspectorScopeLevel;\n namePrefix?: string;\n kind?: string;\n sort?: KnowledgeInspectorNodeSort;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorNodeList> {\n const binding = await this.#binding();\n const scope = this.#scope(binding, input.level);\n const limit = boundedLimit(input.limit, DEFAULT_RECORD_LIMIT, MAX_RECORD_LIMIT);\n const sort = input.sort ?? 'relevant';\n if (sort === 'recent') {\n const cursor = this.#consumeCursor(input.cursor, binding, input.level, 'node', {\n namePrefix: input.namePrefix,\n kind: input.kind,\n sort,\n });\n const records = await this.#knowledge.listNodes({\n scope,\n namePrefix: input.namePrefix,\n kind: input.kind,\n cursor,\n limit,\n });\n const nodes: KnowledgeInspectorNodeSummary[] = await Promise.all(\n records.map(async record => ({\n ...this.#recordSummary(record, binding, input.level),\n relationshipCounts: (await this.#sampledRelationshipCounts(record, scope)).counts,\n })),\n );\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n nodes,\n nextCursor:\n records.length === limit\n ? this.#mintCursor(\n binding,\n input.level,\n 'node',\n createKnowledgeNodeCursor(records.at(-1)!, {\n namePrefix: input.namePrefix,\n kind: input.kind,\n }),\n {\n namePrefix: input.namePrefix,\n kind: input.kind,\n sort,\n },\n )\n : undefined,\n sort,\n coverage: 'exact',\n };\n }\n\n const filters = { namePrefix: input.namePrefix, kind: input.kind, sort };\n const encodedSnapshot = this.#consumeCursor(input.cursor, binding, input.level, 'ranked-node', filters);\n const snapshot = encodedSnapshot\n ? (JSON.parse(encodedSnapshot) as RankedNodeSnapshot)\n : await this.#rankedNodeSnapshot(scope, input.namePrefix, input.kind, sort);\n const page = snapshot.entries.slice(snapshot.offset, snapshot.offset + limit);\n const nodes: KnowledgeInspectorNodeSummary[] = [];\n for (const entry of page) {\n const node = await this.#knowledge.getNode(entry.id);\n if (!node || !isKnowledgeScopeVisible(node.scope, scope)) continue;\n nodes.push({ ...this.#recordSummary(node, binding, input.level), relationshipCounts: entry.counts });\n }\n const nextOffset = snapshot.offset + limit;\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n nodes,\n nextCursor:\n nextOffset < snapshot.entries.length\n ? this.#mintCursor(\n binding,\n input.level,\n 'ranked-node',\n JSON.stringify({ ...snapshot, offset: nextOffset } satisfies RankedNodeSnapshot),\n filters,\n )\n : undefined,\n sort,\n coverage: 'recent-window',\n };\n }\n\n async getNode(input: {\n handle: string;\n recordsCursor?: string;\n mentioningRecordsCursor?: string;\n recordLimit?: number;\n }): Promise<KnowledgeInspectorNodeDetail> {\n const binding = await this.#binding();\n const handle = this.#readHandle(input.handle, binding, 'node');\n const scope = this.#scope(binding, handle.level);\n const node = await this.#knowledge.getNode(handle.recordId);\n this.#assertVisible(node, scope);\n const limit = boundedLimit(input.recordLimit, DEFAULT_FACT_LIMIT, MAX_FACT_LIMIT);\n const recordsAfter = this.#consumeCursor(input.recordsCursor, binding, handle.level, 'records');\n const mentioningAfter = this.#consumeCursor(\n input.mentioningRecordsCursor,\n binding,\n handle.level,\n 'mentioning-records',\n );\n const [recordsResult, mentioningResult] = await Promise.all([\n this.#knowledge.listKnowledgeAbout({ node: node.id, scope, after: recordsAfter, limit }),\n this.#knowledge.listKnowledgeMentioning({ node: node.id, scope, after: mentioningAfter, limit }),\n ]);\n const mentioningRecords = mentioningResult.records.filter(record => record.node !== node.id);\n const content = truncateUtf8(node.content ?? '', MAX_NODE_CONTENT_BYTES);\n const [outgoingTargets, incomingParents, relationship] = await Promise.all([\n this.#outgoingTargets(node, recordsResult.records, scope, binding, handle.level),\n this.#incomingParents(node, mentioningRecords, scope, binding, handle.level),\n this.#sampledRelationshipCounts(node, scope),\n ]);\n const links = await Promise.all(\n parseKnowledgeWikilinks(content.value)\n .slice(0, MAX_RELATED_RECORDS)\n .map(async label => {\n const target = await this.#knowledge.resolveNode({ name: label, scope });\n return {\n label,\n node: target ? this.#recordSummary(target, binding, handle.level) : undefined,\n };\n }),\n );\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: handle.level,\n node: { ...this.#recordSummary(node, binding, handle.level), relationshipCounts: relationship.counts },\n records: recordsResult.records.map(knowledgeSummary),\n recordsNextCursor: recordsResult.nextCursor\n ? this.#mintCursor(binding, handle.level, 'records', recordsResult.nextCursor)\n : undefined,\n mentioningRecords: mentioningRecords.map(knowledgeSummary),\n mentioningRecordsNextCursor: mentioningResult.nextCursor\n ? this.#mintCursor(binding, handle.level, 'mentioning-records', mentioningResult.nextCursor)\n : undefined,\n outgoingTargets: {\n nodes: outgoingTargets.nodes,\n partial: outgoingTargets.partial || Boolean(recordsResult.nextCursor),\n },\n incomingParents: {\n nodes: incomingParents.nodes,\n partial: incomingParents.partial || Boolean(mentioningResult.nextCursor),\n },\n relationshipCounts: relationship.counts,\n content: node.content === undefined ? undefined : content.value,\n contentTruncated: content.truncated,\n links,\n };\n }\n\n async listActivity(input: {\n level: KnowledgeInspectorScopeLevel;\n cursor?: string;\n limit?: number;\n }): Promise<KnowledgeInspectorActivityList> {\n const binding = await this.#binding();\n const scope = this.#scope(binding, input.level);\n const limit = boundedLimit(input.limit, DEFAULT_ACTIVITY_LIMIT, MAX_ACTIVITY_LIMIT);\n const after = this.#consumeCursor(input.cursor, binding, input.level, 'activity');\n const events = await this.#knowledge.listActivity({ scope, after, limit });\n const activityEvents: KnowledgeInspectorActivityEvent[] = [];\n for (const event of events) {\n const record = await this.#activityRecord(event, scope, binding, input.level);\n activityEvents.push({\n action: event.action,\n recordType: event.recordType,\n scope: scopeBadge(event.scope),\n sourceThreadId: record ? event.sourceThreadId : undefined,\n createdAt: event.createdAt.toISOString(),\n record,\n });\n }\n await this.#assertStable(binding);\n return {\n identityKey: binding.identityKey,\n scopeLevel: input.level,\n events: activityEvents,\n nextCursor:\n events.length === limit ? this.#mintCursor(binding, input.level, 'activity', events.at(-1)!.id) : undefined,\n };\n }\n\n async #binding(): Promise<Binding> {\n // Scope rungs come from the same resolver the subconscious writes under; an\n // owner id is a USER id and never names a knowledge org.\n const identity = resolveKnowledgeScopeIdentity(this.#session.state.get());\n const sessionResourceId = this.#session.identity.getResourceId();\n if (!identity.resolved) {\n // An unresolved org is its own identity: handles minted under the\n // previous scope must not survive into whatever resolves next.\n this.#invalidateIdnode();\n throw new KnowledgeInspectorError(\n 'unavailable',\n identity.reason\n ? `Knowledge inspection unavailable: ${identity.reason}`\n : 'Knowledge inspection requires a resolved organization for this session.',\n );\n }\n if (!sessionResourceId) {\n throw new KnowledgeInspectorError('unavailable', 'Knowledge inspection requires an active project.');\n }\n const organizationId = identity.organizationId;\n const resourceId = identity.knowledgeResourceId ?? sessionResourceId;\n const activeThreadId = this.#session.thread.getId() ?? undefined;\n const thread = activeThreadId ? await this.#session.thread.getById({ threadId: activeThreadId }) : null;\n const threadId = thread?.resourceId === sessionResourceId ? thread.id : undefined;\n const fingerprint = `${organizationId}\\0${resourceId}\\0${threadId ?? ''}`;\n if (this.#fingerprint !== fingerprint) {\n this.#fingerprint = fingerprint;\n this.#identityKey = opaqueToken();\n this.#handles.clear();\n this.#cursors.clear();\n }\n return { organizationId, resourceId, threadId, fingerprint, identityKey: this.#identityKey };\n }\n\n #scope(binding: Binding, level: KnowledgeInspectorScopeLevel): KnowledgeScope {\n if (level === 'org') return [`org:${binding.organizationId}`];\n const scope = [`org:${binding.organizationId}`, `resource:${binding.resourceId}`];\n if (level === 'resource') return scope;\n if (!binding.threadId) {\n throw new KnowledgeInspectorError('unavailable', 'The active thread does not belong to this project.');\n }\n return [...scope, `thread:${binding.threadId}`];\n }\n\n async #assertStable(binding: Binding): Promise<void> {\n const current = await this.#binding();\n if (current.identityKey !== binding.identityKey || current.fingerprint !== binding.fingerprint) {\n throw new KnowledgeInspectorError('stale-handle', 'Knowledge scope changed while the request was running.');\n }\n }\n\n #assertVisible<T extends KnowledgeNode>(record: T | null, scope: KnowledgeScope): asserts record is T {\n if (!record || !isKnowledgeScopeVisible(record.scope, scope)) {\n throw new KnowledgeInspectorError('not-visible', 'Knowledge record is not visible in the selected scope.');\n }\n }\n\n #recordSummary(\n record: KnowledgeNode,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): KnowledgeInspectorNodeSummary {\n const type: KnowledgeInspectorRecordType = 'node';\n return {\n handle: this.#mintHandle(binding, level, type, record.id),\n type,\n name: record.name,\n kind: record.kind,\n scope: scopeBadge(record.scope),\n version: record.version,\n updatedAt: record.updatedAt.toISOString(),\n };\n }\n\n async #rankedNodeSnapshot(\n scope: KnowledgeScope,\n namePrefix: string | undefined,\n kind: string | undefined,\n sort: Exclude<KnowledgeInspectorNodeSort, 'recent'>,\n ): Promise<RankedNodeSnapshot> {\n const records = await this.#knowledge.listNodes({\n scope,\n namePrefix,\n kind,\n limit: MAX_RANK_CANDIDATES,\n });\n const ranked = await Promise.all(\n records.map(async (node, recencyRank) => ({\n node,\n recencyRank,\n ...(await this.#sampledRelationshipCounts(node, scope)),\n })),\n );\n const connected = [...ranked].sort(\n (a, b) => b.degree - a.degree || a.recencyRank - b.recencyRank || a.node.id.localeCompare(b.node.id),\n );\n const connectedRank = new Map(connected.map((entry, index) => [entry.node.id, index]));\n const ordered =\n sort === 'connected'\n ? connected\n : [...ranked].sort((a, b) => {\n const aScore = 1 / (RRF_K + a.recencyRank + 1) + 1 / (RRF_K + connectedRank.get(a.node.id)! + 1);\n const bScore = 1 / (RRF_K + b.recencyRank + 1) + 1 / (RRF_K + connectedRank.get(b.node.id)! + 1);\n return bScore - aScore || a.recencyRank - b.recencyRank || a.node.id.localeCompare(b.node.id);\n });\n return {\n offset: 0,\n entries: ordered.map(entry => ({ id: entry.node.id, degree: entry.degree, counts: entry.counts })),\n };\n }\n\n async #sampledRelationshipCounts(\n node: KnowledgeNode,\n scope: KnowledgeScope,\n ): Promise<{ degree: number; counts: KnowledgeInspectorRelationshipCounts }> {\n const [aboutResult, mentioningResult] = await Promise.all([\n this.#knowledge.listKnowledgeAbout({ node: node.id, scope, limit: MAX_RANK_FACTS }),\n this.#knowledge.listKnowledgeMentioning({ node: node.id, scope, limit: MAX_RANK_FACTS }),\n ]);\n const [outgoing, incoming] = await Promise.all([\n this.#outgoingNodeRecords(node, aboutResult.records, scope),\n this.#incomingParentRecords(node, mentioningResult.records, scope),\n ]);\n const mentioningRecords = mentioningResult.records.filter(record => record.node !== node.id);\n const degree = new Set([...outgoing.nodes, ...incoming.nodes].map(record => record.id)).size;\n return {\n degree,\n counts: {\n records: aboutResult.records.length + mentioningRecords.length,\n outgoing: outgoing.nodes.length,\n incoming: incoming.nodes.length,\n sampled: Boolean(\n aboutResult.nextCursor || mentioningResult.nextCursor || outgoing.truncated || incoming.truncated,\n ),\n },\n };\n }\n\n async #outgoingNodeRecords(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n ): Promise<RelationshipRecords> {\n const related = new Map<string, KnowledgeNode>();\n let truncated = false;\n const sources = [current.content ?? '', ...records.map(record => record.text)];\n for (const source of sources) {\n for (const name of parseKnowledgeWikilinks(source)) {\n if (related.size >= MAX_RELATED_RECORDS) {\n truncated = true;\n break;\n }\n const node = await this.#knowledge.resolveNode({ name, scope });\n if (node && node.id !== current.id && isKnowledgeScopeVisible(node.scope, scope)) {\n related.set(node.id, node);\n }\n }\n if (truncated) break;\n }\n return { nodes: [...related.values()], truncated };\n }\n\n async #incomingParentRecords(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n ): Promise<RelationshipRecords> {\n const related = new Map<string, KnowledgeNode>();\n let truncated = false;\n for (const record of records) {\n if (record.node === current.id || related.has(record.node)) continue;\n if (related.size >= MAX_RELATED_RECORDS) {\n truncated = true;\n break;\n }\n const node = await this.#knowledge.getNode(record.node);\n if (node && isKnowledgeScopeVisible(node.scope, scope)) related.set(node.id, node);\n }\n return { nodes: [...related.values()], truncated };\n }\n\n async #outgoingTargets(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorRelationshipPreview> {\n const related = await this.#outgoingNodeRecords(current, records, scope);\n return {\n nodes: related.nodes.map(node => this.#recordSummary(node, binding, level)),\n partial: related.truncated,\n };\n }\n\n async #incomingParents(\n current: KnowledgeNode,\n records: KnowledgeRecord[],\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorRelationshipPreview> {\n const related = await this.#incomingParentRecords(current, records, scope);\n return {\n nodes: related.nodes.map(node => this.#recordSummary(node, binding, level)),\n partial: related.truncated,\n };\n }\n\n async #activityRecord(\n event: KnowledgeActivityEvent,\n scope: KnowledgeScope,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n ): Promise<KnowledgeInspectorNodeSummary | undefined> {\n if (event.recordType === 'node') {\n const node = await this.#knowledge.getNode(event.recordId);\n return node && isKnowledgeScopeVisible(node.scope, scope) ? this.#recordSummary(node, binding, level) : undefined;\n }\n const record = await this.#knowledge.getKnowledge({ id: event.recordId, includeDeleted: true });\n if (!record || !isKnowledgeScopeVisible(record.scope, scope)) return undefined;\n const node = await this.#knowledge.getNode(record.node);\n return node && isKnowledgeScopeVisible(node.scope, scope) ? this.#recordSummary(node, binding, level) : undefined;\n }\n\n #mintHandle(\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n type: KnowledgeInspectorRecordType,\n recordId: string,\n ): string {\n this.#pruneOpaqueEntries();\n const token = opaqueToken();\n this.#handles.set(token, {\n identityKey: binding.identityKey,\n level,\n type,\n recordId,\n expiresAt: Date.now() + HANDLE_TTL_MS,\n });\n return token;\n }\n\n #readHandle(handle: string, binding: Binding, expectedType: KnowledgeInspectorRecordType): HandleEntry {\n const entry = this.#handles.get(handle);\n if (!entry || entry.expiresAt < Date.now() || entry.type !== expectedType) {\n throw new KnowledgeInspectorError('invalid-handle', 'Knowledge record handle is invalid or expired.');\n }\n if (entry.identityKey !== binding.identityKey) {\n throw new KnowledgeInspectorError('stale-handle', 'Knowledge record handle belongs to a previous scope.');\n }\n return entry;\n }\n\n #mintCursor(\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n kind: CursorEntry['kind'],\n value: string,\n filters?: CursorEntry['filters'],\n ): string {\n this.#pruneOpaqueEntries();\n const token = opaqueToken();\n this.#cursors.set(token, {\n identityKey: binding.identityKey,\n level,\n kind,\n value,\n filters,\n expiresAt: Date.now() + HANDLE_TTL_MS,\n });\n return token;\n }\n\n #consumeCursor(\n cursor: string | undefined,\n binding: Binding,\n level: KnowledgeInspectorScopeLevel,\n kind: CursorEntry['kind'],\n filters?: CursorEntry['filters'],\n ): string | undefined {\n if (!cursor) return undefined;\n const entry = this.#cursors.get(cursor);\n if (\n !entry ||\n entry.expiresAt < Date.now() ||\n entry.identityKey !== binding.identityKey ||\n entry.level !== level ||\n entry.kind !== kind ||\n entry.filters?.namePrefix !== filters?.namePrefix ||\n entry.filters?.kind !== filters?.kind ||\n entry.filters?.sort !== filters?.sort\n ) {\n throw new KnowledgeInspectorError(\n 'invalid-cursor',\n 'Knowledge cursor does not match the active scope and filters.',\n );\n }\n return entry.value;\n }\n\n #pruneOpaqueEntries(): void {\n const now = Date.now();\n for (const [token, entry] of this.#handles) {\n if (entry.expiresAt < now) this.#handles.delete(token);\n }\n for (const [token, entry] of this.#cursors) {\n if (entry.expiresAt < now) this.#cursors.delete(token);\n }\n while (this.#handles.size + this.#cursors.size >= MAX_OPAQUE_ENTRIES) {\n const handle = this.#handles.keys().next().value;\n if (handle) this.#handles.delete(handle);\n else {\n const cursor = this.#cursors.keys().next().value;\n if (cursor) this.#cursors.delete(cursor);\n else break;\n }\n }\n }\n\n #invalidateIdnode(): void {\n this.#fingerprint = undefined;\n this.#identityKey = opaqueToken();\n this.#handles.clear();\n this.#cursors.clear();\n }\n}\n\nexport async function createKnowledgeInspector(input: {\n storage: MastraCompositeStore;\n session: Session<MastraCodeState>;\n}): Promise<KnowledgeInspector | undefined> {\n const knowledge = await input.storage.getStore('knowledge');\n return knowledge ? new ScopedKnowledgeInspector({ knowledge, session: input.session }) : undefined;\n}\n"],"mappings":";;;;AAqIA,IAAa,0BAAb,cAA6C,MAAM;CAEtC;CADX,YACE,MACA,SACA;EACA,MAAM,OAAO;EAHJ,KAAA,OAAA;EAIT,KAAK,OAAO;CACd;AACF;AAuCA,MAAM,gBAAgB,IAAI;AAC1B,MAAM,qBAAqB;AAC3B,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AACzB,MAAM,qBAAqB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,KAAK,KAAKA,SAAS,OAAO,KAAK;EAEvD,OAAO,KAAKD,SAAS,OAAO,KAAKC,SAAS,QAAQ,oBAAoB;GACpE,MAAM,SAAS,KAAKD,SAAS,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GAC3C,IAAI,QAAQ,KAAKA,SAAS,OAAO,MAAM;QAClC;IACH,MAAM,SAAS,KAAKC,SAAS,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;IAC3C,IAAI,QAAQ,KAAKA,SAAS,OAAO,MAAM;SAClC;GACP;EACF;CACF;CAEA,oBAA0B;EACxB,KAAKe,eAAe,KAAA;EACpB,KAAKC,eAAe,YAAY;EAChC,KAAKjB,SAAS,MAAM;EACpB,KAAKC,SAAS,MAAM;CACtB;AACF;AAEA,eAAsB,yBAAyB,OAGH;CAC1C,MAAM,YAAY,MAAM,MAAM,QAAQ,SAAS,WAAW;CAC1D,OAAO,YAAY,IAAI,yBAAyB;EAAE;EAAW,SAAS,MAAM;CAAQ,CAAC,IAAI,KAAA;AAC3F"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { MastraCodeState } from './schema.js';
|
|
2
|
+
/**
|
|
3
|
+
* The organization rung local (TUI/studio) knowledge is curated under. A fixed
|
|
4
|
+
* literal on purpose: deriving it from a hostname, machine id, or path would
|
|
5
|
+
* fragment local knowledge per machine or checkout, and a user running several
|
|
6
|
+
* machines against one shared store could never share knowledge between them.
|
|
7
|
+
* Resolving a real org id (settings, /knowledge, login) is future work; when it
|
|
8
|
+
* lands, it replaces this default here.
|
|
9
|
+
*/
|
|
10
|
+
export declare const LOCAL_KNOWLEDGE_ORG_ID = "local";
|
|
11
|
+
export type KnowledgeScopeIdentity = {
|
|
12
|
+
resolved: true;
|
|
13
|
+
organizationId: string;
|
|
14
|
+
/** Set when the resource rung is anchored on something other than the session resource. */
|
|
15
|
+
knowledgeResourceId?: string;
|
|
16
|
+
} | {
|
|
17
|
+
resolved: false;
|
|
18
|
+
knowledgeResourceId?: string;
|
|
19
|
+
reason?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The single source of truth for which org/resource rungs a session's knowledge
|
|
23
|
+
* lives under. Every reader and writer of the knowledge store (the subconscious
|
|
24
|
+
* memory factory, the /knowledge inspector) must derive its scope from here so
|
|
25
|
+
* that what curation writes is what the browser reads.
|
|
26
|
+
*
|
|
27
|
+
* - Factory seeds the authoritative org id into session state. There is no
|
|
28
|
+
* fallback: a session owner is a USER id, never an organization, so a
|
|
29
|
+
* Factory-owned session without an org resolves to nothing (fail closed).
|
|
30
|
+
* - Factory runs share one knowledge graph per project, so the resource rung is
|
|
31
|
+
* anchored on the project id.
|
|
32
|
+
* - TUI/studio sessions use {@link LOCAL_KNOWLEDGE_ORG_ID}.
|
|
33
|
+
*/
|
|
34
|
+
export declare function resolveKnowledgeScopeIdentity(state: MastraCodeState | undefined): KnowledgeScopeIdentity;
|
|
35
|
+
//# sourceMappingURL=knowledge-scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knowledge-scope.d.ts","sourceRoot":"","sources":["../src/knowledge-scope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAE9C,MAAM,MAAM,sBAAsB,GAC9B;IACE,QAAQ,EAAE,IAAI,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,2FAA2F;IAC3F,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,GACD;IAAE,QAAQ,EAAE,KAAK,CAAC;IAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE;;;;;;;;;;;;GAYG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,eAAe,GAAG,SAAS,GAAG,sBAAsB,CAoBxG"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/knowledge-scope.ts
|
|
2
|
+
/**
|
|
3
|
+
* The organization rung local (TUI/studio) knowledge is curated under. A fixed
|
|
4
|
+
* literal on purpose: deriving it from a hostname, machine id, or path would
|
|
5
|
+
* fragment local knowledge per machine or checkout, and a user running several
|
|
6
|
+
* machines against one shared store could never share knowledge between them.
|
|
7
|
+
* Resolving a real org id (settings, /knowledge, login) is future work; when it
|
|
8
|
+
* lands, it replaces this default here.
|
|
9
|
+
*/
|
|
10
|
+
const LOCAL_KNOWLEDGE_ORG_ID = "local";
|
|
11
|
+
/**
|
|
12
|
+
* The single source of truth for which org/resource rungs a session's knowledge
|
|
13
|
+
* lives under. Every reader and writer of the knowledge store (the subconscious
|
|
14
|
+
* memory factory, the /knowledge inspector) must derive its scope from here so
|
|
15
|
+
* that what curation writes is what the browser reads.
|
|
16
|
+
*
|
|
17
|
+
* - Factory seeds the authoritative org id into session state. There is no
|
|
18
|
+
* fallback: a session owner is a USER id, never an organization, so a
|
|
19
|
+
* Factory-owned session without an org resolves to nothing (fail closed).
|
|
20
|
+
* - Factory runs share one knowledge graph per project, so the resource rung is
|
|
21
|
+
* anchored on the project id.
|
|
22
|
+
* - TUI/studio sessions use {@link LOCAL_KNOWLEDGE_ORG_ID}.
|
|
23
|
+
*/
|
|
24
|
+
function resolveKnowledgeScopeIdentity(state) {
|
|
25
|
+
const factoryProjectId = state?.factoryProjectId;
|
|
26
|
+
const isFactory = typeof factoryProjectId === "string" && factoryProjectId.trim().length > 0;
|
|
27
|
+
const factoryOrgId = state?.factoryOrgId;
|
|
28
|
+
const factoryOwned = isFactory || state?.factoryOrgUnresolved === true;
|
|
29
|
+
const organizationId = typeof factoryOrgId === "string" ? factoryOrgId.trim() : "";
|
|
30
|
+
if (organizationId) return {
|
|
31
|
+
resolved: true,
|
|
32
|
+
organizationId,
|
|
33
|
+
knowledgeResourceId: isFactory ? factoryProjectId : void 0
|
|
34
|
+
};
|
|
35
|
+
if (factoryOwned) return {
|
|
36
|
+
resolved: false,
|
|
37
|
+
knowledgeResourceId: isFactory ? factoryProjectId : void 0
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
resolved: true,
|
|
41
|
+
organizationId: LOCAL_KNOWLEDGE_ORG_ID
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { LOCAL_KNOWLEDGE_ORG_ID, resolveKnowledgeScopeIdentity };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=knowledge-scope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knowledge-scope.js","names":[],"sources":["../src/knowledge-scope.ts"],"sourcesContent":["import type { MastraCodeState } from './schema.js';\n\n/**\n * The organization rung local (TUI/studio) knowledge is curated under. A fixed\n * literal on purpose: deriving it from a hostname, machine id, or path would\n * fragment local knowledge per machine or checkout, and a user running several\n * machines against one shared store could never share knowledge between them.\n * Resolving a real org id (settings, /knowledge, login) is future work; when it\n * lands, it replaces this default here.\n */\nexport const LOCAL_KNOWLEDGE_ORG_ID = 'local';\n\nexport type KnowledgeScopeIdentity =\n | {\n resolved: true;\n organizationId: string;\n /** Set when the resource rung is anchored on something other than the session resource. */\n knowledgeResourceId?: string;\n }\n | { resolved: false; knowledgeResourceId?: string; reason?: string };\n\n/**\n * The single source of truth for which org/resource rungs a session's knowledge\n * lives under. Every reader and writer of the knowledge store (the subconscious\n * memory factory, the /knowledge inspector) must derive its scope from here so\n * that what curation writes is what the browser reads.\n *\n * - Factory seeds the authoritative org id into session state. There is no\n * fallback: a session owner is a USER id, never an organization, so a\n * Factory-owned session without an org resolves to nothing (fail closed).\n * - Factory runs share one knowledge graph per project, so the resource rung is\n * anchored on the project id.\n * - TUI/studio sessions use {@link LOCAL_KNOWLEDGE_ORG_ID}.\n */\nexport function resolveKnowledgeScopeIdentity(state: MastraCodeState | undefined): KnowledgeScopeIdentity {\n const factoryProjectId = state?.factoryProjectId;\n const isFactory = typeof factoryProjectId === 'string' && factoryProjectId.trim().length > 0;\n const factoryOrgId = state?.factoryOrgId;\n const factoryOwned = isFactory || state?.factoryOrgUnresolved === true;\n\n // Trimmed to match what the Factory org seeder stores; not every seam routes\n // its seed through it.\n const organizationId = typeof factoryOrgId === 'string' ? factoryOrgId.trim() : '';\n if (organizationId) {\n return {\n resolved: true,\n organizationId,\n knowledgeResourceId: isFactory ? factoryProjectId : undefined,\n };\n }\n if (factoryOwned) {\n return { resolved: false, knowledgeResourceId: isFactory ? factoryProjectId : undefined };\n }\n return { resolved: true, organizationId: LOCAL_KNOWLEDGE_ORG_ID };\n}\n"],"mappings":";;;;;;;;;AAUA,MAAa,yBAAyB;;;;;;;;;;;;;;AAwBtC,SAAgB,8BAA8B,OAA4D;CACxG,MAAM,mBAAmB,OAAO;CAChC,MAAM,YAAY,OAAO,qBAAqB,YAAY,iBAAiB,KAAK,CAAC,CAAC,SAAS;CAC3F,MAAM,eAAe,OAAO;CAC5B,MAAM,eAAe,aAAa,OAAO,yBAAyB;CAIlE,MAAM,iBAAiB,OAAO,iBAAiB,WAAW,aAAa,KAAK,IAAI;CAChF,IAAI,gBACF,OAAO;EACL,UAAU;EACV;EACA,qBAAqB,YAAY,mBAAmB,KAAA;CACtD;CAEF,IAAI,cACF,OAAO;EAAE,UAAU;EAAO,qBAAqB,YAAY,mBAAmB,KAAA;CAAU;CAE1F,OAAO;EAAE,UAAU;EAAM,gBAAgB;CAAuB;AAClE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"amazon-bedrock-gateway.d.ts","sourceRoot":"","sources":["../../src/providers/amazon-bedrock-gateway.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,kBAAkB,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AAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIxC,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlD,WAAW,CAAC,QAAQ,EAAE,kBAAkB,GAAG,iBAAiB,GAAG,SAAS;IAUxE,oBAAoB,CAAC,IAAI,EAAE;QACzB,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACjC,SAAS,CAAC,EAAE,GAAG,CAAC;QAChB,kBAAkB,CAAC,EAAE,GAAG,CAAC;KAC1B,GAAG,oBAAoB;CAGzB;AAED,wBAAgB,0BAA0B,IAAI,oBAAoB,CAEjE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"amazon-bedrock-gateway.js","names":[],"sources":["../../src/providers/amazon-bedrock-gateway.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\nimport path from 'node:path';\nimport { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';\nimport { fromNodeProviderChain } from '@aws-sdk/credential-providers';\nimport { MastraModelGateway } from '@mastra/core/llm';\nimport type { GatewayAuthRequest, GatewayAuthResult, GatewayLanguageModel, ProviderConfig } from '@mastra/core/llm';\nimport { getBedrockModelCatalog } from './amazon-bedrock.js';\n\ntype ModelRequestHeaders = Record<string, string>;\ntype BedrockModel = ReturnType<ReturnType<typeof createAmazonBedrock>>;\ntype BedrockPrompt = Parameters<BedrockModel['doGenerate']>[0]['prompt'];\n\nconst CACHEABLE_BEDROCK_MODEL_IDS = [\n 'anthropic.claude-3-5-haiku-',\n 'anthropic.claude-3-5-sonnet-20241022-v2:0',\n 'anthropic.claude-3-7-sonnet-',\n 'anthropic.claude-fable-5',\n 'anthropic.claude-haiku-4-5-',\n 'anthropic.claude-opus-4-',\n 'anthropic.claude-sonnet-4-',\n 'anthropic.claude-sonnet-5',\n];\n\nexport function supportsBedrockPromptCaching(modelId: string): boolean {\n return CACHEABLE_BEDROCK_MODEL_IDS.some(cacheableModelId => modelId.includes(cacheableModelId));\n}\n\nexport function addBedrockCachePoints(prompt: BedrockPrompt): BedrockPrompt {\n const result = [...prompt];\n const markCachePoint = (message: (typeof result)[number]) => ({\n ...message,\n providerOptions: {\n ...message.providerOptions,\n bedrock: {\n ...message.providerOptions?.bedrock,\n cachePoint: { type: 'default' },\n },\n },\n });\n\n let lastSystemIndex = -1;\n for (let index = result.length - 1; index >= 0; index--) {\n if (result[index]!.role === 'system') {\n lastSystemIndex = index;\n break;\n }\n }\n\n if (lastSystemIndex >= 0) {\n result[lastSystemIndex] = markCachePoint(result[lastSystemIndex]!);\n }\n\n let lastNonSystemIndex = -1;\n for (let index = result.length - 1; index >= 0; index--) {\n if (result[index]!.role !== 'system') {\n lastNonSystemIndex = index;\n break;\n }\n }\n\n if (lastNonSystemIndex >= 0) {\n result[lastNonSystemIndex] = markCachePoint(result[lastNonSystemIndex]!);\n }\n\n return result;\n}\n\nexport function withBedrockCache(model: BedrockModel): BedrockModel {\n return new Proxy(model, {\n get(target, property, receiver) {\n if (property === 'doGenerate') {\n return (options: Parameters<BedrockModel['doGenerate']>[0]) =>\n target.doGenerate({ ...options, prompt: addBedrockCachePoints(options.prompt) });\n }\n if (property === 'doStream') {\n return (options: Parameters<BedrockModel['doStream']>[0]) =>\n target.doStream({ ...options, prompt: addBedrockCachePoints(options.prompt) });\n }\n return Reflect.get(target, property, receiver);\n },\n });\n}\n\nexport const AMAZON_BEDROCK_GATEWAY_ID = 'amazon-bedrock';\n\n/**\n * Whether AWS credentials look available for Bedrock.\n *\n * Amazon Bedrock authenticates with AWS SigV4 rather than a bearer API key, so\n * this only governs whether Bedrock models are offered as \"authenticated\" in the\n * picker. We look for the common signals (env vars, bearer token, a configured\n * profile, or a shared credentials/config file) rather than resolving\n * credentials here, since the auth checker must stay sync.\n */\nexport function hasAwsCredentials(): boolean {\n if (\n process.env.AWS_BEARER_TOKEN_BEDROCK ||\n (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) ||\n process.env.AWS_SHARED_CREDENTIALS_FILE ||\n process.env.AWS_CONFIG_FILE ||\n process.env.AWS_PROFILE ||\n process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI ||\n process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI ||\n process.env.AWS_WEB_IDENTITY_TOKEN_FILE\n ) {\n return true;\n }\n const home = process.env.HOME || process.env.USERPROFILE;\n if (home) {\n const awsDir = path.join(home, '.aws');\n const credentialsPath = process.env.AWS_SHARED_CREDENTIALS_FILE ?? path.join(awsDir, 'credentials');\n const configPath = process.env.AWS_CONFIG_FILE ?? path.join(awsDir, 'config');\n if (existsSync(credentialsPath) || existsSync(configPath)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Create an Amazon Bedrock model.\n *\n * Bedrock authenticates with AWS SigV4 rather than a bearer API key, so this\n * resolves credentials through the standard AWS provider chain\n * (`fromNodeProviderChain`): environment variables, shared `~/.aws` config and\n * SSO profiles, and container/instance roles — the same resolution order the AWS\n * CLI uses. The region falls back to `us-east-1` to match the AWS SDK default.\n *\n * When `AWS_BEARER_TOKEN_BEDROCK` is set, `@ai-sdk/amazon-bedrock` uses bearer\n * auth instead and ignores the credential provider, so we leave that path to the\n * SDK and only wire up SigV4 here.\n */\nfunction bedrockProvider(modelId: string, headers?: ModelRequestHeaders) {\n const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';\n const bedrock = createAmazonBedrock({\n region,\n credentialProvider: fromNodeProviderChain(),\n headers,\n });\n const model = bedrock(modelId);\n return supportsBedrockPromptCaching(modelId) ? withBedrockCache(model) : model;\n}\n\n/**\n * Standalone Amazon Bedrock gateway.\n *\n * Bedrock is resolved directly via AWS SigV4 (not through the model router) and\n * its models are surfaced from the public models.dev catalog. It is exposed as\n * its own gateway/provider (`amazon-bedrock/...`) rather than nested under the\n * MastraCode gateway namespace.\n */\nexport class AmazonBedrockGateway extends MastraModelGateway {\n readonly id = AMAZON_BEDROCK_GATEWAY_ID;\n readonly name = 'Amazon Bedrock';\n\n shouldEnable(): boolean {\n return hasAwsCredentials();\n }\n\n async fetchProviders(): Promise<Record<string, ProviderConfig>> {\n const providers: Record<string, ProviderConfig> = {};\n try {\n const bedrockModels = await getBedrockModelCatalog();\n providers['amazon-bedrock'] = {\n name: 'Amazon Bedrock',\n apiKeyEnvVar: '',\n apiKeyHeader: 'Authorization',\n gateway: this.id,\n models: bedrockModels.map(model => model.id),\n };\n } catch (error) {\n console.warn('Failed to load Amazon Bedrock model catalog:', error);\n }\n return providers;\n }\n\n buildUrl(_modelId: string): string | undefined {\n return undefined;\n }\n\n async getApiKey(_modelId: string): Promise<string> {\n return hasAwsCredentials() ? 'aws-credential-chain' : '';\n }\n\n resolveAuth(_request: GatewayAuthRequest): GatewayAuthResult | undefined {\n // Amazon Bedrock authenticates via the AWS credential chain rather than a\n // stored API key, so report it as authenticated whenever AWS credentials look\n // available. The actual SigV4 signing happens inside `bedrockProvider()`.\n if (hasAwsCredentials()) {\n return { apiKey: 'aws-credential-chain', source: 'gateway' };\n }\n return undefined;\n }\n\n resolveLanguageModel(args: {\n modelId: string;\n providerId: string;\n apiKey: string;\n headers?: Record<string, string>;\n transport?: any;\n responsesWebSocket?: any;\n }): GatewayLanguageModel {\n return bedrockProvider(args.modelId, args.headers) as unknown as GatewayLanguageModel;\n }\n}\n\nexport function createAmazonBedrockGateway(): AmazonBedrockGateway {\n return new AmazonBedrockGateway();\n}\n"],"mappings":";;;;;;;AAYA,MAAM,8BAA8B;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAAgB,6BAA6B,SAA0B;CACrE,OAAO,4BAA4B,MAAK,qBAAoB,QAAQ,SAAS,gBAAgB,CAAC;AAChG;AAEA,SAAgB,sBAAsB,QAAsC;CAC1E,MAAM,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CACnD;AACF;AAEA,SAAgB,6BAAmD;CACjE,OAAO,IAAI,qBAAqB;AAClC"}
|
|
1
|
+
{"version":3,"file":"amazon-bedrock-gateway.js","names":[],"sources":["../../src/providers/amazon-bedrock-gateway.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\nimport path from 'node:path';\nimport { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';\nimport { fromNodeProviderChain } from '@aws-sdk/credential-providers';\nimport { MastraModelGateway } from '@mastra/core/llm';\nimport type { GatewayAuthRequest, GatewayAuthResult, GatewayLanguageModel, ProviderConfig } from '@mastra/core/llm';\nimport { getBedrockModelCatalog } from './amazon-bedrock.js';\n\ntype ModelRequestHeaders = Record<string, string>;\ntype BedrockModel = ReturnType<ReturnType<typeof createAmazonBedrock>>;\ntype BedrockPrompt = Parameters<BedrockModel['doGenerate']>[0]['prompt'];\n\nconst CACHEABLE_BEDROCK_MODEL_IDS = [\n 'anthropic.claude-3-5-haiku-',\n 'anthropic.claude-3-5-sonnet-20241022-v2:0',\n 'anthropic.claude-3-7-sonnet-',\n 'anthropic.claude-fable-5',\n 'anthropic.claude-haiku-4-5-',\n 'anthropic.claude-opus-4-',\n 'anthropic.claude-opus-5',\n 'anthropic.claude-sonnet-4-',\n 'anthropic.claude-sonnet-5',\n];\n\nexport function supportsBedrockPromptCaching(modelId: string): boolean {\n return CACHEABLE_BEDROCK_MODEL_IDS.some(cacheableModelId => modelId.includes(cacheableModelId));\n}\n\nexport function addBedrockCachePoints(prompt: BedrockPrompt): BedrockPrompt {\n const result = [...prompt];\n const markCachePoint = (message: (typeof result)[number]) => ({\n ...message,\n providerOptions: {\n ...message.providerOptions,\n bedrock: {\n ...message.providerOptions?.bedrock,\n cachePoint: { type: 'default' },\n },\n },\n });\n\n let lastSystemIndex = -1;\n for (let index = result.length - 1; index >= 0; index--) {\n if (result[index]!.role === 'system') {\n lastSystemIndex = index;\n break;\n }\n }\n\n if (lastSystemIndex >= 0) {\n result[lastSystemIndex] = markCachePoint(result[lastSystemIndex]!);\n }\n\n let lastNonSystemIndex = -1;\n for (let index = result.length - 1; index >= 0; index--) {\n if (result[index]!.role !== 'system') {\n lastNonSystemIndex = index;\n break;\n }\n }\n\n if (lastNonSystemIndex >= 0) {\n result[lastNonSystemIndex] = markCachePoint(result[lastNonSystemIndex]!);\n }\n\n return result;\n}\n\nexport function withBedrockCache(model: BedrockModel): BedrockModel {\n return new Proxy(model, {\n get(target, property, receiver) {\n if (property === 'doGenerate') {\n return (options: Parameters<BedrockModel['doGenerate']>[0]) =>\n target.doGenerate({ ...options, prompt: addBedrockCachePoints(options.prompt) });\n }\n if (property === 'doStream') {\n return (options: Parameters<BedrockModel['doStream']>[0]) =>\n target.doStream({ ...options, prompt: addBedrockCachePoints(options.prompt) });\n }\n return Reflect.get(target, property, receiver);\n },\n });\n}\n\nexport const AMAZON_BEDROCK_GATEWAY_ID = 'amazon-bedrock';\n\n/**\n * Whether AWS credentials look available for Bedrock.\n *\n * Amazon Bedrock authenticates with AWS SigV4 rather than a bearer API key, so\n * this only governs whether Bedrock models are offered as \"authenticated\" in the\n * picker. We look for the common signals (env vars, bearer token, a configured\n * profile, or a shared credentials/config file) rather than resolving\n * credentials here, since the auth checker must stay sync.\n */\nexport function hasAwsCredentials(): boolean {\n if (\n process.env.AWS_BEARER_TOKEN_BEDROCK ||\n (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) ||\n process.env.AWS_SHARED_CREDENTIALS_FILE ||\n process.env.AWS_CONFIG_FILE ||\n process.env.AWS_PROFILE ||\n process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI ||\n process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI ||\n process.env.AWS_WEB_IDENTITY_TOKEN_FILE\n ) {\n return true;\n }\n const home = process.env.HOME || process.env.USERPROFILE;\n if (home) {\n const awsDir = path.join(home, '.aws');\n const credentialsPath = process.env.AWS_SHARED_CREDENTIALS_FILE ?? path.join(awsDir, 'credentials');\n const configPath = process.env.AWS_CONFIG_FILE ?? path.join(awsDir, 'config');\n if (existsSync(credentialsPath) || existsSync(configPath)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Create an Amazon Bedrock model.\n *\n * Bedrock authenticates with AWS SigV4 rather than a bearer API key, so this\n * resolves credentials through the standard AWS provider chain\n * (`fromNodeProviderChain`): environment variables, shared `~/.aws` config and\n * SSO profiles, and container/instance roles — the same resolution order the AWS\n * CLI uses. The region falls back to `us-east-1` to match the AWS SDK default.\n *\n * When `AWS_BEARER_TOKEN_BEDROCK` is set, `@ai-sdk/amazon-bedrock` uses bearer\n * auth instead and ignores the credential provider, so we leave that path to the\n * SDK and only wire up SigV4 here.\n */\nfunction bedrockProvider(modelId: string, headers?: ModelRequestHeaders) {\n const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';\n const bedrock = createAmazonBedrock({\n region,\n credentialProvider: fromNodeProviderChain(),\n headers,\n });\n const model = bedrock(modelId);\n return supportsBedrockPromptCaching(modelId) ? withBedrockCache(model) : model;\n}\n\n/**\n * Standalone Amazon Bedrock gateway.\n *\n * Bedrock is resolved directly via AWS SigV4 (not through the model router) and\n * its models are surfaced from the public models.dev catalog. It is exposed as\n * its own gateway/provider (`amazon-bedrock/...`) rather than nested under the\n * MastraCode gateway namespace.\n */\nexport class AmazonBedrockGateway extends MastraModelGateway {\n readonly id = AMAZON_BEDROCK_GATEWAY_ID;\n readonly name = 'Amazon Bedrock';\n\n shouldEnable(): boolean {\n return hasAwsCredentials();\n }\n\n async fetchProviders(): Promise<Record<string, ProviderConfig>> {\n const providers: Record<string, ProviderConfig> = {};\n try {\n const bedrockModels = await getBedrockModelCatalog();\n providers['amazon-bedrock'] = {\n name: 'Amazon Bedrock',\n apiKeyEnvVar: '',\n apiKeyHeader: 'Authorization',\n gateway: this.id,\n models: bedrockModels.map(model => model.id),\n };\n } catch (error) {\n console.warn('Failed to load Amazon Bedrock model catalog:', error);\n }\n return providers;\n }\n\n buildUrl(_modelId: string): string | undefined {\n return undefined;\n }\n\n async getApiKey(_modelId: string): Promise<string> {\n return hasAwsCredentials() ? 'aws-credential-chain' : '';\n }\n\n resolveAuth(_request: GatewayAuthRequest): GatewayAuthResult | undefined {\n // Amazon Bedrock authenticates via the AWS credential chain rather than a\n // stored API key, so report it as authenticated whenever AWS credentials look\n // available. The actual SigV4 signing happens inside `bedrockProvider()`.\n if (hasAwsCredentials()) {\n return { apiKey: 'aws-credential-chain', source: 'gateway' };\n }\n return undefined;\n }\n\n resolveLanguageModel(args: {\n modelId: string;\n providerId: string;\n apiKey: string;\n headers?: Record<string, string>;\n transport?: any;\n responsesWebSocket?: any;\n }): GatewayLanguageModel {\n return bedrockProvider(args.modelId, args.headers) as unknown as GatewayLanguageModel;\n }\n}\n\nexport function createAmazonBedrockGateway(): AmazonBedrockGateway {\n return new AmazonBedrockGateway();\n}\n"],"mappings":";;;;;;;AAYA,MAAM,8BAA8B;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAAgB,6BAA6B,SAA0B;CACrE,OAAO,4BAA4B,MAAK,qBAAoB,QAAQ,SAAS,gBAAgB,CAAC;AAChG;AAEA,SAAgB,sBAAsB,QAAsC;CAC1E,MAAM,SAAS,CAAC,GAAG,MAAM;CACzB,MAAM,kBAAkB,aAAsC;EAC5D,GAAG;EACH,iBAAiB;GACf,GAAG,QAAQ;GACX,SAAS;IACP,GAAG,QAAQ,iBAAiB;IAC5B,YAAY,EAAE,MAAM,UAAU;GAChC;EACF;CACF;CAEA,IAAI,kBAAkB;CACtB,KAAK,IAAI,QAAQ,OAAO,SAAS,GAAG,SAAS,GAAG,SAC9C,IAAI,OAAO,MAAM,CAAE,SAAS,UAAU;EACpC,kBAAkB;EAClB;CACF;CAGF,IAAI,mBAAmB,GACrB,OAAO,mBAAmB,eAAe,OAAO,gBAAiB;CAGnE,IAAI,qBAAqB;CACzB,KAAK,IAAI,QAAQ,OAAO,SAAS,GAAG,SAAS,G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sB;EAGzE,oBAAoB,sBAAsB;EAC1C;CACF,CACoB,CAAC,CAAC,OAAO;CAC7B,OAAO,6BAA6B,OAAO,IAAI,iBAAiB,KAAK,IAAI;AAC3E;;;;;;;;;AAUA,IAAa,uBAAb,cAA0C,mBAAmB;CAC3D,KAAc;CACd,OAAgB;CAEhB,eAAwB;EACtB,OAAO,kBAAkB;CAC3B;CAEA,MAAM,iBAA0D;EAC9D,MAAM,YAA4C,CAAC;EACnD,IAAI;GACF,MAAM,gBAAgB,MAAM,uBAAuB;GACnD,UAAU,oBAAoB;IAC5B,MAAM;IACN,cAAc;IACd,cAAc;IACd,SAAS,KAAK;IACd,QAAQ,cAAc,KAAI,UAAS,MAAM,EAAE;GAC7C;EACF,SAAS,OAAO;GACd,QAAQ,KAAK,gDAAgD,KAAK;EACpE;EACA,OAAO;CACT;CAEA,SAAS,UAAsC,CAE/C;CAEA,MAAM,UAAU,UAAmC;EACjD,OAAO,kBAAkB,IAAI,yBAAyB;CACxD;CAEA,YAAY,UAA6D;EAIvE,IAAI,kBAAkB,GACpB,OAAO;GAAE,QAAQ;GAAwB,QAAQ;EAAU;CAG/D;CAEA,qBAAqB,MAOI;EACvB,OAAO,gBAAgB,KAAK,SAAS,KAAK,OAAO;CACnD;AACF;AAEA,SAAgB,6BAAmD;CACjE,OAAO,IAAI,qBAAqB;AAClC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"amazon-bedrock.d.ts","sourceRoot":"","sources":["../../src/providers/amazon-bedrock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;CACZ;
|
|
1
|
+
{"version":3,"file":"amazon-bedrock.d.ts","sourceRoot":"","sources":["../../src/providers/amazon-bedrock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;CACZ;AAyDD,8DAA8D;AAC9D,wBAAgB,wBAAwB,IAAI,IAAI,CAG/C;AAiBD;;;;;;;;;;GAUG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAiC3E"}
|
|
@@ -15,6 +15,25 @@ const CATALOG_TTL_MS = 3600 * 1e3;
|
|
|
15
15
|
const CATALOG_FAILURE_TTL_MS = 60 * 1e3;
|
|
16
16
|
const CATALOG_FETCH_TIMEOUT_MS = 5e3;
|
|
17
17
|
/**
|
|
18
|
+
* True when the catalog entry asks for a transport this catalog cannot serve.
|
|
19
|
+
*
|
|
20
|
+
* Every id returned here is handed to `createAmazonBedrock()(modelId)` by
|
|
21
|
+
* `amazon-bedrock-gateway.ts`, i.e. the Converse API, unconditionally. A
|
|
22
|
+
* `provider` override in the catalog means the model wants a different endpoint
|
|
23
|
+
* and API shape: the nine `bedrock-mantle` models point at
|
|
24
|
+
* `https://bedrock-mantle.${AWS_REGION}.api.aws/openai/v1` with
|
|
25
|
+
* `shape: "responses"`. Advertising them makes them selectable in `/models` and
|
|
26
|
+
* in packs even though the request that follows cannot reach them.
|
|
27
|
+
*
|
|
28
|
+
* Filtering is deliberately conservative: it hides what is currently unusable
|
|
29
|
+
* rather than guessing at a transport. Routing these to Mantle properly is a
|
|
30
|
+
* separate change and a design call for the maintainers.
|
|
31
|
+
*/
|
|
32
|
+
function needsUnsupportedTransport(entry) {
|
|
33
|
+
const provider = entry?.provider;
|
|
34
|
+
return Boolean(provider && (provider.npm || provider.api || provider.shape));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
18
37
|
* A small, stable fallback so Bedrock packs keep working offline or when
|
|
19
38
|
* models.dev is unreachable. Intentionally minimal — the live catalog is the
|
|
20
39
|
* source of truth and supersedes this within one fetch.
|
|
@@ -35,7 +54,7 @@ async function fetchBedrockModels(signal) {
|
|
|
35
54
|
const response = await fetch(MODELS_DEV_API_URL, { signal });
|
|
36
55
|
if (!response.ok) throw new Error(`models.dev returned ${response.status}`);
|
|
37
56
|
const models = (await response.json())[BEDROCK_PROVIDER_ID]?.models ?? {};
|
|
38
|
-
return Object.
|
|
57
|
+
return Object.entries(models).filter(([, entry]) => !needsUnsupportedTransport(entry)).map(([id]) => id).sort().map((id) => ({ id }));
|
|
39
58
|
}
|
|
40
59
|
/**
|
|
41
60
|
* Return the available Amazon Bedrock models.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"amazon-bedrock.js","names":[],"sources":["../../src/providers/amazon-bedrock.ts"],"sourcesContent":["/**\n * Amazon Bedrock model catalog.\n *\n * Bedrock is not part of mastracode's gateway-synced model router (it\n * authenticates with AWS SigV4 rather than an API key / base URL, so it is\n * resolved directly in `agents/model.ts`). To still offer Bedrock models in the\n * `/models` picker and packs, we fetch the public models.dev catalog — the same\n * source the model router uses — and expose the `amazon-bedrock` provider's\n * model list. This mirrors the GitHub Copilot catalog approach.\n */\n\nconst MODELS_DEV_API_URL = 'https://models.dev/api.json';\nconst BEDROCK_PROVIDER_ID = 'amazon-bedrock';\n\nconst CATALOG_TTL_MS = 60 * 60 * 1000;\nconst CATALOG_FAILURE_TTL_MS = 60 * 1000;\nconst CATALOG_FETCH_TIMEOUT_MS = 5_000;\n\nexport interface BedrockModelEntry {\n id: string;\n}\n\n/**\n * A small, stable fallback so Bedrock packs keep working offline or when\n * models.dev is unreachable. Intentionally minimal — the live catalog is the\n * source of truth and supersedes this within one fetch.\n */\nconst BEDROCK_FALLBACK_MODELS: BedrockModelEntry[] = [\n { id: 'us.anthropic.claude-opus-4-6-v1' },\n { id: 'us.anthropic.claude-sonnet-4-5-20250929-v1:0' },\n { id: 'us.anthropic.claude-haiku-4-5-20251001-v1:0' },\n];\n\ninterface CatalogCacheEntry {\n fetchedAt: number;\n ttl: number;\n models: BedrockModelEntry[];\n}\n\nlet catalogCache: CatalogCacheEntry | null = null;\nlet inflightFetch: Promise<BedrockModelEntry[]> | null = null;\n\n/** Reset the in-process Bedrock catalog cache (test seam). */\nexport function clearBedrockCatalogCache(): void {\n catalogCache = null;\n inflightFetch = null;\n}\n\nasync function fetchBedrockModels(signal: AbortSignal): Promise<BedrockModelEntry[]> {\n const response = await fetch(MODELS_DEV_API_URL, { signal });\n if (!response.ok) {\n throw new Error(`models.dev returned ${response.status}`);\n }\n const data = (await response.json()) as Record<string, { models?: Record<string, unknown> }>;\n const provider = data[BEDROCK_PROVIDER_ID];\n const models = provider?.models ?? {};\n return Object.
|
|
1
|
+
{"version":3,"file":"amazon-bedrock.js","names":[],"sources":["../../src/providers/amazon-bedrock.ts"],"sourcesContent":["/**\n * Amazon Bedrock model catalog.\n *\n * Bedrock is not part of mastracode's gateway-synced model router (it\n * authenticates with AWS SigV4 rather than an API key / base URL, so it is\n * resolved directly in `agents/model.ts`). To still offer Bedrock models in the\n * `/models` picker and packs, we fetch the public models.dev catalog — the same\n * source the model router uses — and expose the `amazon-bedrock` provider's\n * model list. This mirrors the GitHub Copilot catalog approach.\n */\n\nconst MODELS_DEV_API_URL = 'https://models.dev/api.json';\nconst BEDROCK_PROVIDER_ID = 'amazon-bedrock';\n\nconst CATALOG_TTL_MS = 60 * 60 * 1000;\nconst CATALOG_FAILURE_TTL_MS = 60 * 1000;\nconst CATALOG_FETCH_TIMEOUT_MS = 5_000;\n\nexport interface BedrockModelEntry {\n id: string;\n}\n\n/**\n * A models.dev catalog entry for one Bedrock model.\n *\n * Most entries carry no `provider` block: they are served by the SigV4\n * `bedrock-runtime` Converse API, which is what this catalog feeds. An entry\n * that *does* carry one is overriding that transport.\n */\ninterface BedrockCatalogEntry {\n provider?: {\n npm?: string;\n api?: string;\n shape?: string;\n };\n}\n\n/**\n * True when the catalog entry asks for a transport this catalog cannot serve.\n *\n * Every id returned here is handed to `createAmazonBedrock()(modelId)` by\n * `amazon-bedrock-gateway.ts`, i.e. the Converse API, unconditionally. A\n * `provider` override in the catalog means the model wants a different endpoint\n * and API shape: the nine `bedrock-mantle` models point at\n * `https://bedrock-mantle.${AWS_REGION}.api.aws/openai/v1` with\n * `shape: \"responses\"`. Advertising them makes them selectable in `/models` and\n * in packs even though the request that follows cannot reach them.\n *\n * Filtering is deliberately conservative: it hides what is currently unusable\n * rather than guessing at a transport. Routing these to Mantle properly is a\n * separate change and a design call for the maintainers.\n */\nfunction needsUnsupportedTransport(entry: unknown): boolean {\n const provider = (entry as BedrockCatalogEntry | null)?.provider;\n return Boolean(provider && (provider.npm || provider.api || provider.shape));\n}\n\n/**\n * A small, stable fallback so Bedrock packs keep working offline or when\n * models.dev is unreachable. Intentionally minimal — the live catalog is the\n * source of truth and supersedes this within one fetch.\n */\nconst BEDROCK_FALLBACK_MODELS: BedrockModelEntry[] = [\n { id: 'us.anthropic.claude-opus-4-6-v1' },\n { id: 'us.anthropic.claude-sonnet-4-5-20250929-v1:0' },\n { id: 'us.anthropic.claude-haiku-4-5-20251001-v1:0' },\n];\n\ninterface CatalogCacheEntry {\n fetchedAt: number;\n ttl: number;\n models: BedrockModelEntry[];\n}\n\nlet catalogCache: CatalogCacheEntry | null = null;\nlet inflightFetch: Promise<BedrockModelEntry[]> | null = null;\n\n/** Reset the in-process Bedrock catalog cache (test seam). */\nexport function clearBedrockCatalogCache(): void {\n catalogCache = null;\n inflightFetch = null;\n}\n\nasync function fetchBedrockModels(signal: AbortSignal): Promise<BedrockModelEntry[]> {\n const response = await fetch(MODELS_DEV_API_URL, { signal });\n if (!response.ok) {\n throw new Error(`models.dev returned ${response.status}`);\n }\n const data = (await response.json()) as Record<string, { models?: Record<string, unknown> }>;\n const provider = data[BEDROCK_PROVIDER_ID];\n const models = provider?.models ?? {};\n return Object.entries(models)\n .filter(([, entry]) => !needsUnsupportedTransport(entry))\n .map(([id]) => id)\n .sort()\n .map(id => ({ id }));\n}\n\n/**\n * Return the available Amazon Bedrock models.\n *\n * - Returns the cached list when a recent fetch succeeded.\n * - On cache miss / expiry, fetches the models.dev catalog with a 5s timeout and\n * caches it for an hour.\n * - On fetch failure, returns a small hard-coded fallback (so packs still work\n * offline) and caches that briefly to avoid hammering the network.\n *\n * Concurrent calls during a fetch share the inflight promise.\n */\nexport async function getBedrockModelCatalog(): Promise<BedrockModelEntry[]> {\n const now = Date.now();\n if (catalogCache && now - catalogCache.fetchedAt < catalogCache.ttl) {\n return catalogCache.models;\n }\n\n if (inflightFetch) return inflightFetch;\n\n inflightFetch = (async (): Promise<BedrockModelEntry[]> => {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), CATALOG_FETCH_TIMEOUT_MS);\n try {\n const models = await fetchBedrockModels(controller.signal);\n catalogCache = { fetchedAt: Date.now(), ttl: CATALOG_TTL_MS, models };\n return models;\n } catch (error) {\n catalogCache = {\n fetchedAt: Date.now(),\n ttl: CATALOG_FAILURE_TTL_MS,\n models: BEDROCK_FALLBACK_MODELS,\n };\n console.warn(\n 'Failed to fetch live Amazon Bedrock models, using fallback list:',\n error instanceof Error ? error.message : error,\n );\n return BEDROCK_FALLBACK_MODELS;\n } finally {\n clearTimeout(timer);\n inflightFetch = null;\n }\n })();\n\n return inflightFetch;\n}\n"],"mappings":";;;;;;;;;;;AAWA,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAE5B,MAAM,iBAAiB,OAAU;AACjC,MAAM,yBAAyB,KAAK;AACpC,MAAM,2BAA2B;;;;;;;;;;;;;;;;AAoCjC,SAAS,0BAA0B,OAAyB;CAC1D,MAAM,WAAY,OAAsC;CACxD,OAAO,QAAQ,aAAa,SAAS,OAAO,SAAS,OAAO,SAAS,MAAM;AAC7E;;;;;;AAOA,MAAM,0BAA+C;CACnD,EAAE,IAAI,kCAAkC;CACxC,EAAE,IAAI,+CAA+C;CACrD,EAAE,IAAI,8CAA8C;AACtD;AAQA,IAAI,eAAyC;AAC7C,IAAI,gBAAqD;;AAGzD,SAAgB,2BAAiC;CAC/C,eAAe;CACf,gBAAgB;AAClB;AAEA,eAAe,mBAAmB,QAAmD;CACnF,MAAM,WAAW,MAAM,MAAM,oBAAoB,EAAE,OAAO,CAAC;CAC3D,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,MAAM,uBAAuB,SAAS,QAAQ;CAI1D,MAAM,UADW,MADG,SAAS,KAAK,EAAA,CACZ,oBACC,EAAE,UAAU,CAAC;CACpC,OAAO,OAAO,QAAQ,MAAM,CAAC,CAC1B,QAAQ,GAAG,WAAW,CAAC,0BAA0B,KAAK,CAAC,CAAC,CACxD,KAAK,CAAC,QAAQ,EAAE,CAAC,CACjB,KAAK,CAAC,CACN,KAAI,QAAO,EAAE,GAAG,EAAE;AACvB;;;;;;;;;;;;AAaA,eAAsB,yBAAuD;CAE3E,IAAI,gBADQ,KAAK,IACK,IAAI,aAAa,YAAY,aAAa,KAC9D,OAAO,aAAa;CAGtB,IAAI,eAAe,OAAO;CAE1B,iBAAiB,YAA0C;EACzD,MAAM,aAAa,IAAI,gBAAgB;EACvC,MAAM,QAAQ,iBAAiB,WAAW,MAAM,GAAG,wBAAwB;EAC3E,IAAI;GACF,MAAM,SAAS,MAAM,mBAAmB,WAAW,MAAM;GACzD,eAAe;IAAE,WAAW,KAAK,IAAI;IAAG,KAAK;IAAgB;GAAO;GACpE,OAAO;EACT,SAAS,OAAO;GACd,eAAe;IACb,WAAW,KAAK,IAAI;IACpB,KAAK;IACL,QAAQ;GACV;GACA,QAAQ,KACN,oEACA,iBAAiB,QAAQ,MAAM,UAAU,KAC3C;GACA,OAAO;EACT,UAAU;GACR,aAAa,KAAK;GAClB,gBAAgB;EAClB;CACF,EAAA,CAAG;CAEH,OAAO;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slash-command-processor.d.ts","sourceRoot":"","sources":["../../src/utils/slash-command-processor.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEtE;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,oBAAoB,EAC7B,IAAI,EAAE,MAAM,EAAE,EACd,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,CAgBjB;
|
|
1
|
+
{"version":3,"file":"slash-command-processor.d.ts","sourceRoot":"","sources":["../../src/utils/slash-command-processor.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEtE;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,oBAAoB,EAC7B,IAAI,EAAE,MAAM,EAAE,EACd,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAkGD;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,oBAAoB,GAAG,MAAM,CAQ7E;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,oBAAoB,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,oBAAoB,EAAE,CAAC,CAc9G"}
|
|
@@ -64,21 +64,25 @@ async function replaceShellOutput(template, workingDir) {
|
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Replace file references with file content
|
|
67
|
-
*
|
|
67
|
+
* Formats: @filename, @path/to/file, @path\to\file, @C:\path\to\file, or @C:/path/to/file
|
|
68
|
+
* Spaces, quoted paths, and glob patterns are not supported.
|
|
68
69
|
*/
|
|
69
70
|
async function replaceFileReferences(template, workingDir) {
|
|
70
|
-
const matches = [...template.matchAll(/@([\w
|
|
71
|
+
const matches = [...template.matchAll(/@((?:[A-Za-z]:[\\/])?[\w./\\-]+)/g)];
|
|
71
72
|
let result = template;
|
|
72
73
|
for (const match of matches) {
|
|
73
74
|
const [fullMatch, filePath] = match;
|
|
74
75
|
try {
|
|
75
|
-
const fullPath =
|
|
76
|
+
const fullPath = resolveFileReferencePath(workingDir, filePath);
|
|
76
77
|
const content = await promises.readFile(fullPath, "utf-8");
|
|
77
78
|
result = result.replace(fullMatch, content);
|
|
78
79
|
} catch {}
|
|
79
80
|
}
|
|
80
81
|
return result;
|
|
81
82
|
}
|
|
83
|
+
function resolveFileReferencePath(workingDir, filePath) {
|
|
84
|
+
return ([workingDir, filePath].some((value) => /^[A-Za-z]:[\\/]/.test(value) || value.includes("\\")) ? path$1.win32 : path$1).resolve(workingDir, filePath);
|
|
85
|
+
}
|
|
82
86
|
/**
|
|
83
87
|
* Format a command for display in help/autocomplete
|
|
84
88
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slash-command-processor.js","names":["
|
|
1
|
+
{"version":3,"file":"slash-command-processor.js","names":["fs","path"],"sources":["../../src/utils/slash-command-processor.ts"],"sourcesContent":["import { execSync } from 'node:child_process';\nimport { promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport type { SlashCommandMetadata } from './slash-command-loader.js';\n\n/**\n * Process a slash command by replacing variables and executing shell commands\n */\nexport async function processSlashCommand(\n command: SlashCommandMetadata,\n args: string[],\n workingDir: string,\n): Promise<string> {\n const { result: withArgs, shouldAppendRawArgs } = replaceArguments(command.template, args);\n let result = withArgs;\n\n // Replace shell commands\n result = await replaceShellOutput(result, workingDir);\n\n // Replace file references\n result = await replaceFileReferences(result, workingDir);\n\n // Append raw args after shell/file processing to avoid executing user input\n if (shouldAppendRawArgs) {\n result = result.trimEnd() + `\\n\\nARGUMENTS: ${args.join(' ')}`;\n }\n\n return result;\n}\n\n/**\n * Replace argument variables in template\n * $ARGUMENTS - all arguments joined\n * $1, $2, etc. - positional arguments\n */\nfunction replaceArguments(template: string, args: string[]): { result: string; shouldAppendRawArgs: boolean } {\n let result = template;\n\n // Check if template references any argument variables\n const hasArgumentsVar = /\\$ARGUMENTS/.test(template);\n const hasPositionalVar = /\\$[1-9]\\d*/.test(template);\n\n // Replace $ARGUMENTS with all args joined\n result = result.replace(/\\$ARGUMENTS/g, args.join(' '));\n\n // Replace range arguments $1+, $2+, etc. before single positional arguments.\n args.forEach((_, index) => {\n const argNumber = index + 1;\n const pattern = new RegExp(`\\\\\\$${argNumber}\\\\+`, 'g');\n result = result.replace(pattern, args.slice(index).join(' '));\n });\n\n // Replace positional arguments $1, $2, etc.\n args.forEach((arg, index) => {\n const pattern = new RegExp(`\\\\\\$${index + 1}`, 'g');\n result = result.replace(pattern, arg);\n });\n\n // Clear unused positional and range arguments\n result = result.replace(/\\$[1-9]\\d*\\+?/g, '');\n\n return {\n result,\n shouldAppendRawArgs: !hasArgumentsVar && !hasPositionalVar && args.length > 0,\n };\n}\n\n/**\n * Replace shell command references with their output\n * Format: !`command`\n */\nasync function replaceShellOutput(template: string, workingDir: string): Promise<string> {\n const shellPattern = /!`([^`]+)`/g;\n const matches = [...template.matchAll(shellPattern)];\n\n let result = template;\n for (const match of matches) {\n const [fullMatch, command] = match;\n try {\n const output = execSync(command!, {\n cwd: workingDir,\n encoding: 'utf-8',\n timeout: 30000,\n maxBuffer: 1024 * 1024, // 1MB buffer\n });\n result = result.replace(fullMatch, output.trim());\n } catch (error) {\n console.error(`Error executing shell command \"${command}\":`, error);\n result = result.replace(fullMatch, `[Error: Failed to execute \"${command}\"]`);\n }\n }\n\n return result;\n}\n\n/**\n * Replace file references with file content\n * Formats: @filename, @path/to/file, @path\\to\\file, @C:\\path\\to\\file, or @C:/path/to/file\n * Spaces, quoted paths, and glob patterns are not supported.\n */\nasync function replaceFileReferences(template: string, workingDir: string): Promise<string> {\n const filePattern = /@((?:[A-Za-z]:[\\\\/])?[\\w./\\\\-]+)/g;\n const matches = [...template.matchAll(filePattern)];\n\n let result = template;\n for (const match of matches) {\n const [fullMatch, filePath] = match;\n try {\n const fullPath = resolveFileReferencePath(workingDir, filePath!);\n const content = await fs.readFile(fullPath, 'utf-8');\n result = result.replace(fullMatch, content);\n } catch {\n // Leave literal @mentions/search qualifiers such as @me intact when they do not resolve to files.\n }\n }\n\n return result;\n}\n\nfunction resolveFileReferencePath(workingDir: string, filePath: string): string {\n const usesWindowsPaths = [workingDir, filePath].some(value => /^[A-Za-z]:[\\\\/]/.test(value) || value.includes('\\\\'));\n const pathResolver = usesWindowsPaths ? path.win32 : path;\n\n return pathResolver.resolve(workingDir, filePath);\n}\n\n/**\n * Format a command for display in help/autocomplete\n */\nexport function formatCommandForDisplay(command: SlashCommandMetadata): string {\n const parts = [command.name];\n\n if (command.description) {\n parts.push(`- ${command.description}`);\n }\n\n return parts.join(' ');\n}\n\n/**\n * Group commands by namespace for display\n */\nexport function groupCommandsByNamespace(commands: SlashCommandMetadata[]): Map<string, SlashCommandMetadata[]> {\n const groups = new Map<string, SlashCommandMetadata[]>();\n\n for (const command of commands) {\n const namespace = command.namespace || command.name.split(':')[0] || 'general';\n\n if (!groups.has(namespace)) {\n groups.set(namespace, []);\n }\n\n groups.get(namespace)!.push(command);\n }\n\n return groups;\n}\n"],"mappings":";;;;;;;AAQA,eAAsB,oBACpB,SACA,MACA,YACiB;CACjB,MAAM,EAAE,QAAQ,UAAU,wBAAwB,iBAAiB,QAAQ,UAAU,IAAI;CACzF,IAAI,SAAS;CAGb,SAAS,MAAM,mBAAmB,QAAQ,UAAU;CAGpD,SAAS,MAAM,sBAAsB,QAAQ,UAAU;CAGvD,IAAI,qBACF,SAAS,OAAO,QAAQ,IAAI,kBAAkB,KAAK,KAAK,GAAG;CAG7D,OAAO;AACT;;;;;;AAOA,SAAS,iBAAiB,UAAkB,MAAkE;CAC5G,IAAI,SAAS;CAGb,MAAM,kBAAkB,cAAc,KAAK,QAAQ;CACnD,MAAM,mBAAmB,aAAa,KAAK,QAAQ;CAGnD,SAAS,OAAO,QAAQ,gBAAgB,KAAK,KAAK,GAAG,CAAC;CAGtD,KAAK,SAAS,GAAG,UAAU;EACzB,MAAM,YAAY,QAAQ;EAC1B,MAAM,UAAU,IAAI,OAAO,OAAO,UAAU,MAAM,GAAG;EACrD,SAAS,OAAO,QAAQ,SAAS,KAAK,MAAM,KAAK,CAAC,CAAC,KAAK,GAAG,CAAC;CAC9D,CAAC;CAGD,KAAK,SAAS,KAAK,UAAU;EAC3B,MAAM,UAAU,IAAI,OAAO,OAAO,QAAQ,KAAK,GAAG;EAClD,SAAS,OAAO,QAAQ,SAAS,GAAG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AQ,IAAI;CAE3B,IAAI,QAAQ,aACV,MAAM,KAAK,KAAK,QAAQ,aAAa;CAGvC,OAAO,MAAM,KAAK,GAAG;AACvB;;;;AAKA,SAAgB,yBAAyB,UAAuE;CAC9G,MAAM,yBAAS,IAAI,IAAoC;CAEvD,KAAK,MAAM,WAAW,UAAU;EAC9B,MAAM,YAAY,QAAQ,aAAa,QAAQ,KAAK,MAAM,GAAG,CAAC,CAAC,MAAM;EAErE,IAAI,CAAC,OAAO,IAAI,SAAS,GACvB,OAAO,IAAI,WAAW,CAAC,CAAC;EAG1B,OAAO,IAAI,SAAS,CAAC,CAAE,KAAK,OAAO;CACrC;CAEA,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/code-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0-alpha.10",
|
|
4
4
|
"description": "Mastra Code SDK: the agent core behind Mastra Code (everything except the TUI) — build your own UIs and surfaces on top of it",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -56,18 +56,18 @@
|
|
|
56
56
|
"vscode-languageserver-protocol": "^3.17.5",
|
|
57
57
|
"yaml": "^2.7.1",
|
|
58
58
|
"zod": "^4.3.6",
|
|
59
|
-
"@mastra/
|
|
60
|
-
"@mastra/core": "1.65.0-alpha.1",
|
|
61
|
-
"@mastra/duckdb": "1.6.4",
|
|
59
|
+
"@mastra/core": "1.65.0-alpha.9",
|
|
62
60
|
"@mastra/agent-browser": "0.5.2",
|
|
61
|
+
"@mastra/fastembed": "1.3.1",
|
|
63
62
|
"@mastra/github-signals": "0.4.0",
|
|
64
|
-
"@mastra/
|
|
63
|
+
"@mastra/duckdb": "1.7.0-alpha.1",
|
|
64
|
+
"@mastra/libsql": "1.22.4-alpha.0",
|
|
65
|
+
"@mastra/memory": "1.28.3-alpha.3",
|
|
65
66
|
"@mastra/mcp": "1.17.3",
|
|
66
|
-
"@mastra/memory": "1.28.3-alpha.0",
|
|
67
67
|
"@mastra/parallel": "0.1.1",
|
|
68
|
-
"@mastra/
|
|
68
|
+
"@mastra/observability": "1.17.6-alpha.1",
|
|
69
|
+
"@mastra/pg": "1.23.0-alpha.3",
|
|
69
70
|
"@mastra/schema-compat": "1.3.8",
|
|
70
|
-
"@mastra/observability": "1.17.6-alpha.0",
|
|
71
71
|
"@mastra/stagehand": "0.3.4",
|
|
72
72
|
"@mastra/tavily": "1.1.2"
|
|
73
73
|
},
|
|
@@ -80,9 +80,9 @@
|
|
|
80
80
|
"typescript": "^6.0.3",
|
|
81
81
|
"typescript-eslint": "^8.57.0",
|
|
82
82
|
"vitest": "4.1.10",
|
|
83
|
-
"@internal/lint": "0.0.130",
|
|
84
83
|
"@internal/types-builder": "0.0.105",
|
|
85
|
-
"@internal/workspace-test-utils": "0.0.74"
|
|
84
|
+
"@internal/workspace-test-utils": "0.0.74",
|
|
85
|
+
"@internal/lint": "0.0.130"
|
|
86
86
|
},
|
|
87
87
|
"engines": {
|
|
88
88
|
"node": ">=22.19.0"
|