( state: SyncState, repoSlug: string, entries: Record<string, string>, ifMatchChecksum?: string | null, )
| 458 | } |
| 459 | |
| 460 | async function uploadTeamMemory( |
| 461 | state: SyncState, |
| 462 | repoSlug: string, |
| 463 | entries: Record<string, string>, |
| 464 | ifMatchChecksum?: string | null, |
| 465 | ): Promise<TeamMemorySyncUploadResult> { |
| 466 | try { |
| 467 | const session = await getAuthRuntime().resolveSession({ allowRefresh: true }) |
| 468 | |
| 469 | const auth = getAuthHeaders(session) |
| 470 | if (auth.error) { |
| 471 | return { success: false, error: auth.error, errorType: 'auth' } |
| 472 | } |
| 473 | |
| 474 | const headers: Record<string, string> = { |
| 475 | ...auth.headers, |
| 476 | 'Content-Type': 'application/json', |
| 477 | } |
| 478 | if (ifMatchChecksum) { |
| 479 | headers['If-Match'] = `"${ifMatchChecksum.replace(/"/g, '')}"` |
| 480 | } |
| 481 | |
| 482 | const endpoint = getTeamMemorySyncEndpoint(repoSlug) |
| 483 | const response = await axios.put( |
| 484 | endpoint, |
| 485 | { entries }, |
| 486 | { |
| 487 | headers, |
| 488 | timeout: TEAM_MEMORY_SYNC_TIMEOUT_MS, |
| 489 | validateStatus: status => status === 200 || status === 412, |
| 490 | }, |
| 491 | ) |
| 492 | |
| 493 | if (response.status === 412) { |
| 494 | logForDebugging('team-memory-sync: conflict (412 Precondition Failed)', { |
| 495 | level: 'info', |
| 496 | }) |
| 497 | return { success: false, conflict: true, error: 'ETag mismatch' } |
| 498 | } |
| 499 | |
| 500 | const responseChecksum = response.data?.checksum |
| 501 | if (responseChecksum) { |
| 502 | state.lastKnownChecksum = responseChecksum |
| 503 | } |
| 504 | |
| 505 | logForDebugging( |
| 506 | `team-memory-sync: uploaded ${Object.keys(entries).length} entries (checksum: ${responseChecksum ?? 'none'})`, |
| 507 | { level: 'debug' }, |
| 508 | ) |
| 509 | return { |
| 510 | success: true, |
| 511 | checksum: responseChecksum, |
| 512 | lastModified: response.data?.lastModified, |
| 513 | } |
| 514 | } catch (error) { |
| 515 | const body = axios.isAxiosError(error) |
| 516 | ? JSON.stringify(error.response?.data ?? '') |
| 517 | : '' |
no test coverage detected