@ossy/consultancy 3.22.0 → 3.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Rank resume documents against a consultancy request.
3
+ * Tag overlap is the share of the request's tags that the resume also has.
4
+ * Similarity is the cosine score from the existing embedding store, when present.
5
+ */
6
+
7
+ export function tagsOf (value) {
8
+ const items = Array.isArray(value)
9
+ ? value
10
+ : (typeof value === 'string' ? value.split(',') : [])
11
+ const seen = new Set()
12
+ const tags = []
13
+ for (const item of items) {
14
+ if (typeof item !== 'string') continue
15
+ const trimmed = item.trim()
16
+ const key = trimmed.toLowerCase()
17
+ if (!key || seen.has(key)) continue
18
+ seen.add(key)
19
+ tags.push(trimmed)
20
+ }
21
+ return tags
22
+ }
23
+
24
+ export function tagOverlap (requestTags, resumeTags) {
25
+ const resumeByKey = new Map(resumeTags.map((tag) => [tag.toLowerCase(), tag]))
26
+ const shared = []
27
+ for (const tag of requestTags) {
28
+ const hit = resumeByKey.get(tag.toLowerCase())
29
+ if (hit) shared.push(hit)
30
+ }
31
+ const score = requestTags.length === 0 ? 0 : shared.length / requestTags.length
32
+ return { shared, score }
33
+ }
34
+
35
+ function line (label, value) {
36
+ if (typeof value !== 'string') return ''
37
+ const trimmed = value.trim()
38
+ if (!trimmed) return ''
39
+ return `${label}: ${trimmed}`
40
+ }
41
+
42
+ function textField (content, next, previous) {
43
+ const value = content?.[next] ?? content?.[previous]
44
+ return typeof value === 'string' ? value.trim() : ''
45
+ }
46
+
47
+ export function requestMatchText (resource) {
48
+ const content = resource?.content && typeof resource.content === 'object' ? resource.content : {}
49
+ return [
50
+ line('Title', content.title || resource?.name),
51
+ line('Summary', content.summary),
52
+ line('Role', content.role),
53
+ line('Tags', tagsOf(content.tags).join(', ')),
54
+ line('Location', content.location),
55
+ line('Duration', content.duration),
56
+ line('Start', content.start),
57
+ ].filter(Boolean).join('\n')
58
+ }
59
+
60
+ export function resumeMatchText (resume, { profile, experiences } = {}) {
61
+ const content = resume?.content && typeof resume.content === 'object' ? resume.content : {}
62
+ const lines = [
63
+ line('Resume', resume?.name),
64
+ line('Tags', tagsOf(content.tags).join(', ')),
65
+ ]
66
+ const profileContent = profile?.content && typeof profile.content === 'object' ? profile.content : null
67
+ if (profileContent) {
68
+ lines.push(
69
+ line('Name', profileContent.name),
70
+ line('Role', profileContent.role),
71
+ line('Summary', profileContent.summary),
72
+ line('Profile tags', tagsOf(profileContent.tags).join(', ')),
73
+ )
74
+ }
75
+ for (const experience of experiences || []) {
76
+ const experienceContent = experience?.content && typeof experience.content === 'object'
77
+ ? experience.content
78
+ : {}
79
+ lines.push(
80
+ line('Experience', textField(experienceContent, 'title', 'Title')),
81
+ line('Subtitle', textField(experienceContent, 'subtitle', 'Sub Title')),
82
+ line('Kind', textField(experienceContent, 'kind', 'Type of experience')),
83
+ line('Description', textField(experienceContent, 'description', 'Description')),
84
+ line('Experience tags', tagsOf(experienceContent.tags ?? experienceContent.Tags).join(', ')),
85
+ )
86
+ }
87
+ return lines.filter(Boolean).join('\n')
88
+ }
89
+
90
+ /**
91
+ * @param {{
92
+ * request: object,
93
+ * resumes: object[],
94
+ * similarities?: Array<{ resourceId: string, score: number }>,
95
+ * }} input
96
+ */
97
+ export function rankMatches ({ request, resumes, similarities = [] } = {}) {
98
+ const requestTags = tagsOf(request?.content?.tags)
99
+ const similarityById = new Map()
100
+ for (const item of similarities || []) {
101
+ if (!item?.resourceId || !Number.isFinite(item.score)) continue
102
+ similarityById.set(item.resourceId, item.score)
103
+ }
104
+
105
+ return (resumes || [])
106
+ .filter((resume) => resume?.id && resume.id !== request?.id)
107
+ .map((resume) => {
108
+ const overlap = tagOverlap(requestTags, tagsOf(resume?.content?.tags))
109
+ const similarity = similarityById.has(resume.id) ? similarityById.get(resume.id) : null
110
+ return {
111
+ resourceId: resume.id,
112
+ name: typeof resume.name === 'string' ? resume.name : '',
113
+ tags: tagsOf(resume?.content?.tags),
114
+ shared: overlap.shared,
115
+ tagScore: overlap.score,
116
+ similarity,
117
+ score: overlap.score + (similarity ?? 0),
118
+ }
119
+ })
120
+ .sort((a, b) => (
121
+ b.score - a.score
122
+ || b.shared.length - a.shared.length
123
+ || a.name.localeCompare(b.name)
124
+ ))
125
+ }
@@ -0,0 +1,58 @@
1
+ import { rankMatches, requestMatchText, resumeMatchText, tagOverlap, tagsOf } from './match-resumes.js'
2
+
3
+ const request = {
4
+ id: 'request-1',
5
+ name: 'Senior React consultant',
6
+ content: {
7
+ title: 'Senior React consultant',
8
+ summary: 'A product team wants a React and Node.js consultant.',
9
+ role: 'Senior frontend',
10
+ tags: ['React', 'Node.js'],
11
+ location: 'Stockholm',
12
+ },
13
+ }
14
+
15
+ describe('match resumes', () => {
16
+ it('reads tags from a list or a comma string', () => {
17
+ expect(tagsOf([' React ', 'react', 'Node.js'])).toEqual(['React', 'Node.js'])
18
+ expect(tagsOf('React, Node.js')).toEqual(['React', 'Node.js'])
19
+ })
20
+
21
+ it('scores tag overlap as the share of the request tags that the resume has', () => {
22
+ expect(tagOverlap(['React', 'Node.js'], ['react', 'TypeScript'])).toEqual({
23
+ shared: ['react'],
24
+ score: 0.5,
25
+ })
26
+ expect(tagOverlap([], ['React']).score).toBe(0)
27
+ })
28
+
29
+ it('writes the request and the linked resume as text an embedding can read', () => {
30
+ expect(requestMatchText(request)).toContain('Tags: React, Node.js')
31
+ expect(resumeMatchText({
32
+ name: 'React consultant',
33
+ content: { tags: ['React', 'Node.js'] },
34
+ }, {
35
+ profile: { content: { name: 'Ada Consultant', role: 'Senior React consultant' } },
36
+ experiences: [{ content: { Title: 'Platform engineer', Tags: 'React, Node.js' } }],
37
+ })).toContain('Experience: Platform engineer')
38
+ })
39
+
40
+ it('orders resumes by tag overlap plus similarity', () => {
41
+ const resumes = [
42
+ { id: 'low', name: 'Design', content: { tags: ['Figma'] } },
43
+ { id: 'tags-only', name: 'React', content: { tags: ['React', 'Node.js'] } },
44
+ { id: 'both', name: 'Full stack', content: { tags: ['React'] } },
45
+ ]
46
+ const ranked = rankMatches({
47
+ request,
48
+ resumes,
49
+ similarities: [
50
+ { resourceId: 'both', score: 0.9 },
51
+ { resourceId: 'low', score: 0.1 },
52
+ ],
53
+ })
54
+ expect(ranked.map((item) => item.resourceId)).toEqual(['both', 'tags-only', 'low'])
55
+ expect(ranked[0]).toMatchObject({ shared: ['React'], tagScore: 0.5, similarity: 0.9, score: 1.4 })
56
+ expect(ranked[1].similarity).toBeNull()
57
+ })
58
+ })
@@ -0,0 +1,67 @@
1
+ import { scoreRequests } from './refresh-matches.task.js'
2
+ import { location as resumeLocation } from '@ossy/resumes/locations'
3
+ import { ResumesSchema } from '@ossy/resumes/schema-ids'
4
+ import { ConsultancySchema } from './schema-ids.js'
5
+ import { location, matchesLocation } from './locations.js'
6
+ import { referenceId } from '@ossy/resumes/resume-links'
7
+
8
+ const GetResource = { id: '@ossy/resources/actions/get' }
9
+ const ListResources = { id: '@ossy/resources/actions/list' }
10
+
11
+ export const metadata = {
12
+ id: '@ossy/consultancy/tasks/match-resumes',
13
+ triggers: [
14
+ {
15
+ kind: 'on_action',
16
+ action: '@ossy/consultancy/actions/match-resumes',
17
+ },
18
+ ],
19
+ }
20
+
21
+ function fail (message, status) {
22
+ throw Object.assign(new Error(message), { status })
23
+ }
24
+
25
+ function asResourceList (result) {
26
+ if (Array.isArray(result)) return result
27
+ if (Array.isArray(result?.items)) return result.items
28
+ return []
29
+ }
30
+
31
+ /**
32
+ * Re-rank one request from embeddings that already exist. Does not embed.
33
+ * New and changed documents are embedded by the refresh-matches task.
34
+ */
35
+ export async function run ({ payload, req, sdk }) {
36
+ if (!sdk?.invoke) fail('sdk is required', 500)
37
+
38
+ const requestId = payload?.requestId ?? payload?.resourceId
39
+ if (!requestId) fail('requestId is required', 400)
40
+ const workspaceId = payload?.workspaceId ?? req?.workspaceId
41
+ if (!workspaceId) fail('workspaceId is required', 400)
42
+
43
+ const request = await sdk.invoke(GetResource, { resourceId: requestId })
44
+ if (request?.type !== ConsultancySchema.request) fail('request is required', 400)
45
+ if (request.belongsTo && request.belongsTo !== workspaceId) fail('request is required', 404)
46
+
47
+ const listed = async (listLocation) => asResourceList(await sdk.invoke(ListResources, {
48
+ location: listLocation,
49
+ workspaceId,
50
+ }))
51
+ const resumes = (await listed(resumeLocation)).filter((item) => item?.type === ResumesSchema.resume)
52
+ const existing = (await listed(matchesLocation)).filter((item) => (
53
+ item?.type === ConsultancySchema.requestMatches && referenceId(item?.content?.request) === request.id
54
+ ))
55
+
56
+ const saved = await scoreRequests(sdk, {
57
+ workspaceId,
58
+ requests: [request],
59
+ resumes,
60
+ existing,
61
+ })
62
+ const match = saved[0]
63
+ return {
64
+ requestId: request.id,
65
+ matches: match?.content?.matches || [],
66
+ }
67
+ }
@@ -0,0 +1,68 @@
1
+ import { beforeEach, describe, expect, it, jest } from '@jest/globals'
2
+
3
+ const loadWorkspaceEmbeddings = jest.fn()
4
+
5
+ jest.unstable_mockModule('@ossy/platform/vector/vector-store', () => ({
6
+ getEmbedding: jest.fn(),
7
+ loadWorkspaceEmbeddings,
8
+ cosineSimilarity: () => 0.8,
9
+ }))
10
+
11
+ const { run } = await import('./match-resumes.task.js')
12
+
13
+ const GetResource = { id: '@ossy/resources/actions/get' }
14
+ const ListResources = { id: '@ossy/resources/actions/list' }
15
+ const CreateResource = { id: '@ossy/resources/actions/create' }
16
+ const EmbedResource = { id: '@ossy/media-tasks/actions/embed-resource' }
17
+
18
+ const request = {
19
+ id: 'request-1',
20
+ type: '@ossy/consultancy/schema/request',
21
+ belongsTo: 'ws-1',
22
+ name: 'Senior React consultant',
23
+ content: { title: 'Senior React consultant', tags: ['React'] },
24
+ }
25
+
26
+ const resume = {
27
+ id: 'resume-1',
28
+ type: '@ossy/resumes/schema/resume',
29
+ name: 'React consultant',
30
+ content: { tags: ['React'] },
31
+ }
32
+
33
+ describe('match-resumes task', () => {
34
+ beforeEach(() => {
35
+ loadWorkspaceEmbeddings.mockReset()
36
+ loadWorkspaceEmbeddings.mockResolvedValue([
37
+ { resourceId: 'request-1', vector: [1] },
38
+ { resourceId: 'resume-1', vector: [1] },
39
+ ])
40
+ })
41
+
42
+ it('ranks from stored embeddings and does not embed', async () => {
43
+ const created = []
44
+ const sdk = {
45
+ invoke: jest.fn(async (action, payload) => {
46
+ if (action.id === GetResource.id) return request
47
+ if (action.id === ListResources.id) {
48
+ if (payload.location === '/@ossy/resumes/') return [resume]
49
+ return []
50
+ }
51
+ if (action.id === CreateResource.id) {
52
+ created.push(payload)
53
+ return { id: 'match-1', content: payload.content }
54
+ }
55
+ if (action.id === EmbedResource.id) throw new Error('should not embed')
56
+ throw new Error(action.id)
57
+ }),
58
+ }
59
+
60
+ const result = await run({
61
+ payload: { requestId: 'request-1', workspaceId: 'ws-1' },
62
+ sdk,
63
+ })
64
+
65
+ expect(created[0].content.matches[0].resourceId).toBe('resume-1')
66
+ expect(result.matches[0]).toMatchObject({ resourceId: 'resume-1', similarity: 0.8 })
67
+ })
68
+ })
@@ -0,0 +1,5 @@
1
+ export const metadata = {
2
+ id: '@ossy/consultancy/actions/refresh-matches',
3
+ access: 'workspace',
4
+ label: '@ossy/consultancy/actions/refresh-matches.label',
5
+ }
@@ -0,0 +1,42 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { referenceId, referenceIds } from '@ossy/resumes/resume-links'
3
+
4
+ export const PROFILE_SCHEMA = '@ossy/profile/schema/profile'
5
+
6
+ export function textHash (text) {
7
+ return createHash('sha256').update(text).digest('hex')
8
+ }
9
+
10
+ /** An embedding is current when it was built from this exact text. */
11
+ export function embeddingIsCurrent (stored, hash) {
12
+ return Boolean(hash) && stored?.textHash === hash
13
+ }
14
+
15
+ /** Resumes whose profile or experience links point at this resource. */
16
+ export function resumesLinking (resumes, resourceId) {
17
+ if (!resourceId) return []
18
+ return (resumes || []).filter((resume) => {
19
+ if (referenceId(resume?.content?.profile) === resourceId) return true
20
+ return referenceIds(resume?.content?.experiences).includes(resourceId)
21
+ })
22
+ }
23
+
24
+ function round (value) {
25
+ return Math.round((Number(value) || 0) * 1000) / 1000
26
+ }
27
+
28
+ function normalized (matches) {
29
+ return (matches || []).map((match) => ({
30
+ resourceId: match.resourceId,
31
+ name: match.name || '',
32
+ shared: match.shared || [],
33
+ tagScore: round(match.tagScore),
34
+ similarity: match.similarity == null ? null : round(match.similarity),
35
+ score: round(match.score),
36
+ }))
37
+ }
38
+
39
+ /** True when a saved match list would not change a later reader. */
40
+ export function matchesEqual (left, right) {
41
+ return JSON.stringify(normalized(left)) === JSON.stringify(normalized(right))
42
+ }
@@ -0,0 +1,26 @@
1
+ import { embeddingIsCurrent, matchesEqual, resumesLinking, textHash } from './refresh-matches.js'
2
+
3
+ describe('refresh matches', () => {
4
+ it('treats an embedding as current only when the text hash matches', () => {
5
+ const hash = textHash('Title: React')
6
+ expect(embeddingIsCurrent({ textHash: hash }, hash)).toBe(true)
7
+ expect(embeddingIsCurrent({ textHash: 'other' }, hash)).toBe(false)
8
+ expect(embeddingIsCurrent(null, hash)).toBe(false)
9
+ })
10
+
11
+ it('finds resumes that link a profile or an experience', () => {
12
+ const resumes = [
13
+ { id: 'a', content: { profile: { resourceId: 'profile-1' }, experiences: [] } },
14
+ { id: 'b', content: { experiences: [{ resourceId: 'experience-1' }] } },
15
+ { id: 'c', content: { profile: { resourceId: 'other' } } },
16
+ ]
17
+ expect(resumesLinking(resumes, 'profile-1').map((item) => item.id)).toEqual(['a'])
18
+ expect(resumesLinking(resumes, 'experience-1').map((item) => item.id)).toEqual(['b'])
19
+ })
20
+
21
+ it('treats the same ranking as unchanged', () => {
22
+ const matches = [{ resourceId: 'resume-1', name: 'React', shared: ['React'], tagScore: 1, similarity: 0.8, score: 1.8 }]
23
+ expect(matchesEqual(matches, [{ ...matches[0], score: 1.8004 }])).toBe(true)
24
+ expect(matchesEqual(matches, [{ ...matches[0], resourceId: 'resume-2' }])).toBe(false)
25
+ })
26
+ })
@@ -0,0 +1,196 @@
1
+ import { location as resumeLocation } from '@ossy/resumes/locations'
2
+ import { referenceId, referenceIds } from '@ossy/resumes/resume-links'
3
+ import { ResumesSchema } from '@ossy/resumes/schema-ids'
4
+ import { cosineSimilarity, getEmbedding, loadWorkspaceEmbeddings } from '@ossy/platform/vector/vector-store'
5
+ import { ConsultancySchema } from './schema-ids.js'
6
+ import { location, matchesLocation } from './locations.js'
7
+ import { rankMatches, requestMatchText, resumeMatchText } from './match-resumes.js'
8
+ import {
9
+ PROFILE_SCHEMA,
10
+ embeddingIsCurrent,
11
+ matchesEqual,
12
+ resumesLinking,
13
+ textHash,
14
+ } from './refresh-matches.js'
15
+
16
+ const GetResource = { id: '@ossy/resources/actions/get' }
17
+ const ListResources = { id: '@ossy/resources/actions/list' }
18
+ const CreateResource = { id: '@ossy/resources/actions/create' }
19
+ const UpdateContent = { id: '@ossy/resources/actions/update-content' }
20
+ const EmbedResource = { id: '@ossy/media-tasks/actions/embed-resource' }
21
+
22
+ export const metadata = {
23
+ id: '@ossy/consultancy/tasks/refresh-matches',
24
+ schedule: '*/5 * * * *',
25
+ triggers: [
26
+ { type: ConsultancySchema.request, event: 'Created' },
27
+ { type: ConsultancySchema.request, event: 'Updated' },
28
+ { type: ResumesSchema.resume, event: 'Created' },
29
+ { type: ResumesSchema.resume, event: 'Updated' },
30
+ { type: PROFILE_SCHEMA, event: 'Created' },
31
+ { type: PROFILE_SCHEMA, event: 'Updated' },
32
+ { type: ResumesSchema.resumeExperience, event: 'Created' },
33
+ { type: ResumesSchema.resumeExperience, event: 'Updated' },
34
+ ],
35
+ }
36
+
37
+ function fail (message, status) {
38
+ throw Object.assign(new Error(message), { status })
39
+ }
40
+
41
+ function asResourceList (result) {
42
+ if (Array.isArray(result)) return result
43
+ if (Array.isArray(result?.items)) return result.items
44
+ return []
45
+ }
46
+
47
+ function workspaceIdFrom ({ event, payload, req }) {
48
+ return event?.payload?.belongsTo
49
+ ?? payload?.workspaceId
50
+ ?? req?.workspaceId
51
+ ?? null
52
+ }
53
+
54
+ async function readLinked (sdk, resourceId) {
55
+ if (!resourceId) return null
56
+ try {
57
+ return await sdk.invoke(GetResource, { resourceId })
58
+ } catch (err) {
59
+ if (err?.status === 404) return null
60
+ throw err
61
+ }
62
+ }
63
+
64
+ async function listAt (sdk, listLocation, workspaceId) {
65
+ return asResourceList(await sdk.invoke(ListResources, { location: listLocation, workspaceId }))
66
+ }
67
+
68
+ /**
69
+ * Embed a request or resume when its text changed. Returns true when a new vector was written.
70
+ */
71
+ export async function ensureEmbedded (sdk, resource, workspaceId) {
72
+ const text = resource?.type === ConsultancySchema.request
73
+ ? requestMatchText(resource)
74
+ : await resumeText(sdk, resource)
75
+ if (!text) return false
76
+ const hash = textHash(text)
77
+ const stored = await getEmbedding(resource.id)
78
+ if (embeddingIsCurrent(stored, hash)) return false
79
+ await sdk.invoke(EmbedResource, {
80
+ resourceId: resource.id,
81
+ text,
82
+ textHash: hash,
83
+ workspaceId,
84
+ })
85
+ return true
86
+ }
87
+
88
+ async function resumeText (sdk, resume) {
89
+ const profile = await readLinked(sdk, referenceId(resume?.content?.profile))
90
+ const experiences = (await Promise.all(
91
+ referenceIds(resume?.content?.experiences).map((id) => readLinked(sdk, id)),
92
+ )).filter(Boolean)
93
+ return resumeMatchText(resume, { profile, experiences })
94
+ }
95
+
96
+ function similaritiesFor (queryVector, resumes, vectorById) {
97
+ if (!Array.isArray(queryVector) || !queryVector.length) return []
98
+ const scores = []
99
+ for (const resume of resumes) {
100
+ const vector = vectorById.get(resume.id)
101
+ if (!vector) continue
102
+ scores.push({ resourceId: resume.id, score: cosineSimilarity(queryVector, vector) })
103
+ }
104
+ return scores
105
+ }
106
+
107
+ async function saveMatches (sdk, { workspaceId, request, matches, existing }) {
108
+ const content = {
109
+ request: { resourceId: request.id },
110
+ matches,
111
+ }
112
+ const current = existing.find((item) => referenceId(item?.content?.request) === request.id)
113
+ if (current && matchesEqual(current.content?.matches, matches)) return current
114
+ if (current) {
115
+ return sdk.invoke(UpdateContent, { resourceId: current.id, workspaceId, content })
116
+ }
117
+ return sdk.invoke(CreateResource, {
118
+ type: ConsultancySchema.requestMatches,
119
+ location: matchesLocation,
120
+ name: request.name || request.id,
121
+ workspaceId,
122
+ content,
123
+ })
124
+ }
125
+
126
+ /**
127
+ * Rank the given requests against stored resume vectors and save a match document each.
128
+ */
129
+ export async function scoreRequests (sdk, { workspaceId, requests, resumes, existing }) {
130
+ const stored = await loadWorkspaceEmbeddings(workspaceId)
131
+ const vectorById = new Map((stored || []).map((item) => [item.resourceId, item.vector]))
132
+ const saved = []
133
+ for (const request of requests) {
134
+ const matches = rankMatches({
135
+ request,
136
+ resumes,
137
+ similarities: similaritiesFor(vectorById.get(request.id), resumes, vectorById),
138
+ })
139
+ saved.push(await saveMatches(sdk, { workspaceId, request, matches, existing }))
140
+ }
141
+ return saved
142
+ }
143
+
144
+ /**
145
+ * Keep embeddings and saved matches current.
146
+ *
147
+ * A request or resume change embeds that document. A profile or experience
148
+ * change embeds the resumes that link it. A schedule pass embeds anything
149
+ * that has no current vector, then re-scores. Unchanged text is not embedded again.
150
+ */
151
+ export async function run ({ event, payload, req, sdk }) {
152
+ if (!sdk?.invoke) fail('sdk is required', 500)
153
+ const workspaceId = workspaceIdFrom({ event, payload, req })
154
+ if (!workspaceId) fail('workspaceId is required', 400)
155
+
156
+ const requests = (await listAt(sdk, location, workspaceId))
157
+ .filter((item) => item?.type === ConsultancySchema.request)
158
+ const resumes = (await listAt(sdk, resumeLocation, workspaceId))
159
+ .filter((item) => item?.type === ResumesSchema.resume)
160
+ const existing = (await listAt(sdk, matchesLocation, workspaceId))
161
+ .filter((item) => item?.type === ConsultancySchema.requestMatches)
162
+
163
+ const resourceId = event?.resourceId || null
164
+ const type = event?.type
165
+ const scheduled = !resourceId || type === 'scheduled'
166
+
167
+ let toEmbed = []
168
+ let toScore = requests
169
+ if (scheduled) {
170
+ toEmbed = [...requests, ...resumes]
171
+ } else if (type === ConsultancySchema.request) {
172
+ toEmbed = requests.filter((item) => item.id === resourceId)
173
+ if (!toEmbed.length) {
174
+ const request = await readLinked(sdk, resourceId)
175
+ if (request?.type === ConsultancySchema.request) toEmbed = [request]
176
+ }
177
+ toScore = toEmbed
178
+ } else if (type === ResumesSchema.resume) {
179
+ toEmbed = resumes.filter((item) => item.id === resourceId)
180
+ if (!toEmbed.length) {
181
+ const resume = await readLinked(sdk, resourceId)
182
+ if (resume?.type === ResumesSchema.resume) toEmbed = [resume]
183
+ }
184
+ } else if (type === PROFILE_SCHEMA || type === ResumesSchema.resumeExperience) {
185
+ toEmbed = resumesLinking(resumes, resourceId)
186
+ if (!toEmbed.length) toScore = []
187
+ }
188
+
189
+ let embedded = 0
190
+ for (const resource of toEmbed) {
191
+ if (await ensureEmbedded(sdk, resource, workspaceId)) embedded += 1
192
+ }
193
+
194
+ const saved = await scoreRequests(sdk, { workspaceId, requests: toScore, resumes, existing })
195
+ return { embedded, requests: saved.length }
196
+ }