Skip to content

Commit 4568ec3

Browse files
committed
refactor: improve argument handling and dependency resolution in apply and push processes
- Updated `apply.ts` to ensure the `--force` option does not affect pull commands, preserving local changes. - Removed outdated logic for resolving `assistantId` in `pull.ts` and `resolver.ts`, streamlining the dependency resolution process. - Enhanced `push.ts` to ensure proper handling of tool and structured output dependencies, improving the clarity of auto-application logs. - Cleaned up unnecessary comments and code related to assistant overrides, simplifying the overall codebase.
1 parent a5443a1 commit 4568ec3

4 files changed

Lines changed: 40 additions & 83 deletions

File tree

src/apply.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ async function main(): Promise<void> {
2929
const allArgs = process.argv.slice(3);
3030
const hasForce = allArgs.includes("--force");
3131

32-
const pullArgs = allArgs.join(" ");
32+
// Pull never gets --force (apply's pull should always preserve local changes/deletions)
33+
const pullArgs = allArgs.filter(a => a !== "--force").join(" ");
3334
const pushArgs = allArgs.join(" ");
3435

3536
if (!env || !VALID_ENVIRONMENTS.includes(env as typeof VALID_ENVIRONMENTS[number])) {
@@ -53,7 +54,7 @@ async function main(): Promise<void> {
5354
}
5455
console.log("═══════════════════════════════════════════════════════════════\n");
5556

56-
// Step 1: Pull (--force forwarded: platform becomes source of truth, deletes orphaned local files)
57+
// Step 1: Pull (never forced — always preserves local deletions/changes)
5758
const pullCmd = `npx tsx src/pull.ts ${env} ${pullArgs}`.trim();
5859
const pullExit = runPassthrough(pullCmd);
5960
if (pullExit !== 0) {

src/pull.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { execSync } from "child_process";
22
import { existsSync, readdirSync } from "fs";
3-
import { mkdir, writeFile, unlink } from "fs/promises";
3+
import { mkdir, writeFile } from "fs/promises";
44
import { join, dirname, relative } from "path";
55
import { stringify } from "yaml";
66
import { VAPI_ENV, VAPI_BASE_URL, VAPI_TOKEN, RESOURCES_DIR, BASE_DIR, APPLY_FILTER } from "./config.ts";
@@ -335,21 +335,6 @@ function resolveReferencesToResourceIds(
335335
return dest;
336336
});
337337
}
338-
// Resolve assistantId in assistantOverrides["tools:append"][].destinations[]
339-
const overrides = resolvedMember.assistantOverrides as Record<string, unknown> | undefined;
340-
const toolsAppend = overrides?.["tools:append"] as Record<string, unknown>[] | undefined;
341-
if (Array.isArray(toolsAppend)) {
342-
for (const tool of toolsAppend) {
343-
if (Array.isArray(tool.destinations)) {
344-
tool.destinations = (tool.destinations as Record<string, unknown>[]).map((dest) => {
345-
if (typeof dest.assistantId === "string") {
346-
return { ...dest, assistantId: assistantsMap.get(dest.assistantId) ?? dest.assistantId };
347-
}
348-
return dest;
349-
});
350-
}
351-
}
352-
}
353338
return resolvedMember;
354339
});
355340
}
@@ -568,25 +553,6 @@ export async function pullResourceType(
568553
newStateSection[resourceId] = resource.id;
569554
}
570555

571-
// In force mode, delete local files for resources removed from the platform
572-
if (force) {
573-
const oldStateSection = state[resourceType];
574-
for (const [resourceId] of Object.entries(oldStateSection)) {
575-
if (newStateSection[resourceId]) continue;
576-
const folderPath = FOLDER_MAP[resourceType];
577-
const dir = join(RESOURCES_DIR, folderPath);
578-
for (const ext of [".md", ".yml", ".yaml"]) {
579-
const filePath = join(dir, `${resourceId}${ext}`);
580-
if (existsSync(filePath)) {
581-
await unlink(filePath);
582-
const relPath = relative(BASE_DIR, filePath);
583-
console.log(` 🗑️ ${resourceId} -> ${relPath} (removed from platform)`);
584-
break;
585-
}
586-
}
587-
}
588-
}
589-
590556
// Update state with new mappings
591557
state[resourceType] = newStateSection;
592558

src/push.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,12 @@ interface DependencyContext {
400400
}
401401

402402
async function ensureToolExists(toolId: string, ctx: DependencyContext): Promise<void> {
403-
if (UUID_REGEX.test(toolId) || ctx.autoApplied.has(`tools:${toolId}`)) return;
403+
if (UUID_REGEX.test(toolId) || ctx.state.tools[toolId] || ctx.autoApplied.has(`tools:${toolId}`)) return;
404404

405405
const tool = ctx.allTools.find(t => t.resourceId === toolId);
406406
if (!tool) return;
407407

408-
const isUpdate = !!ctx.state.tools[toolId];
409-
console.log(` 📦 Auto-applying dependency → tool: ${toolId}${isUpdate ? " (update)" : ""}`);
408+
console.log(` 📦 Auto-applying dependency → tool: ${toolId}`);
410409
try {
411410
const uuid = await applyTool(tool, ctx.state);
412411
ctx.state.tools[tool.resourceId] = uuid;
@@ -420,13 +419,12 @@ async function ensureToolExists(toolId: string, ctx: DependencyContext): Promise
420419
}
421420

422421
async function ensureStructuredOutputExists(outputId: string, ctx: DependencyContext): Promise<void> {
423-
if (UUID_REGEX.test(outputId) || ctx.autoApplied.has(`structuredOutputs:${outputId}`)) return;
422+
if (UUID_REGEX.test(outputId) || ctx.state.structuredOutputs[outputId] || ctx.autoApplied.has(`structuredOutputs:${outputId}`)) return;
424423

425424
const output = ctx.allStructuredOutputs.find(o => o.resourceId === outputId);
426425
if (!output) return;
427426

428-
const isUpdate = !!ctx.state.structuredOutputs[outputId];
429-
console.log(` 📦 Auto-applying dependency → structured output: ${outputId}${isUpdate ? " (update)" : ""}`);
427+
console.log(` 📦 Auto-applying dependency → structured output: ${outputId}`);
430428
try {
431429
const uuid = await applyStructuredOutput(output, ctx.state);
432430
ctx.state.structuredOutputs[output.resourceId] = uuid;
@@ -439,32 +437,55 @@ async function ensureStructuredOutputExists(outputId: string, ctx: DependencyCon
439437
}
440438
}
441439

442-
async function ensureAssistantDepsExist(assistantId: string, ctx: DependencyContext): Promise<void> {
443-
if (UUID_REGEX.test(assistantId)) return;
440+
async function ensureAssistantDepsExist(assistantId: string, ctx: DependencyContext): Promise<boolean> {
441+
if (UUID_REGEX.test(assistantId)) return false;
444442

445443
const assistant = ctx.allAssistants.find(a => a.resourceId === assistantId);
446-
if (!assistant) return;
444+
if (!assistant) return false;
447445

448446
const refs = extractReferencedIds(assistant.data as Record<string, unknown>);
447+
let depsCreated = false;
449448

450449
for (const toolId of refs.tools) {
451-
await ensureToolExists(toolId, ctx);
450+
if (!UUID_REGEX.test(toolId) && !ctx.state.tools[toolId]) {
451+
await ensureToolExists(toolId, ctx);
452+
if (ctx.state.tools[toolId]) depsCreated = true;
453+
}
452454
}
453455
for (const outputId of refs.structuredOutputs) {
454-
await ensureStructuredOutputExists(outputId, ctx);
456+
if (!UUID_REGEX.test(outputId) && !ctx.state.structuredOutputs[outputId]) {
457+
await ensureStructuredOutputExists(outputId, ctx);
458+
if (ctx.state.structuredOutputs[outputId]) depsCreated = true;
459+
}
455460
}
461+
462+
return depsCreated;
456463
}
457464

458465
async function ensureAssistantExists(assistantId: string, ctx: DependencyContext): Promise<void> {
459-
if (UUID_REGEX.test(assistantId) || ctx.autoApplied.has(`assistants:${assistantId}`)) return;
466+
if (UUID_REGEX.test(assistantId)) return;
467+
468+
// Always resolve tool/SO deps, even if the assistant already exists in state
469+
const depsCreated = await ensureAssistantDepsExist(assistantId, ctx);
470+
471+
// Assistant already on platform — update it if we just created missing deps
472+
if (ctx.state.assistants[assistantId]) {
473+
if (depsCreated) {
474+
const assistant = ctx.allAssistants.find(a => a.resourceId === assistantId);
475+
if (assistant) {
476+
console.log(` 🔄 Updating assistant with new dependencies: ${assistantId}`);
477+
await applyAssistant(assistant, ctx.state);
478+
}
479+
}
480+
return;
481+
}
460482

461-
await ensureAssistantDepsExist(assistantId, ctx);
483+
if (ctx.autoApplied.has(`assistants:${assistantId}`)) return;
462484

463485
const assistant = ctx.allAssistants.find(a => a.resourceId === assistantId);
464486
if (!assistant) return;
465487

466-
const isUpdate = !!ctx.state.assistants[assistantId];
467-
console.log(` 📦 Auto-applying dependency → assistant: ${assistantId}${isUpdate ? " (update)" : ""}`);
488+
console.log(` 📦 Auto-applying dependency → assistant: ${assistantId}`);
468489
try {
469490
const uuid = await applyAssistant(assistant, ctx.state);
470491
ctx.state.assistants[assistant.resourceId] = uuid;

src/resolver.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,6 @@ export function resolveReferences(
262262
}
263263
}
264264
}
265-
// Resolve assistantId in assistantOverrides["tools:append"][].destinations[]
266-
const overrides = member.assistantOverrides as Record<string, unknown> | undefined;
267-
const toolsAppend = overrides?.["tools:append"] as Record<string, unknown>[] | undefined;
268-
if (Array.isArray(toolsAppend)) {
269-
for (const tool of toolsAppend) {
270-
if (Array.isArray(tool.destinations)) {
271-
for (const dest of tool.destinations as Record<string, unknown>[]) {
272-
if (typeof dest.assistantId === "string") {
273-
const resolvedId = resolveAssistantId(dest.assistantId, state);
274-
if (resolvedId) {
275-
dest.assistantId = resolvedId;
276-
}
277-
}
278-
}
279-
}
280-
}
281-
}
282265
}
283266
}
284267

@@ -418,20 +401,6 @@ export function extractReferencedIds(data: Record<string, unknown>): ExtractedRe
418401
}
419402
}
420403
}
421-
// Check assistantOverrides["tools:append"][].destinations[].assistantId
422-
const overrides = member.assistantOverrides as Record<string, unknown> | undefined;
423-
const toolsAppend = overrides?.["tools:append"] as Record<string, unknown>[] | undefined;
424-
if (Array.isArray(toolsAppend)) {
425-
for (const tool of toolsAppend) {
426-
if (Array.isArray(tool.destinations)) {
427-
for (const dest of tool.destinations as Record<string, unknown>[]) {
428-
if (typeof dest.assistantId === "string") {
429-
assistants.push(cleanId(dest.assistantId));
430-
}
431-
}
432-
}
433-
}
434-
}
435404
}
436405
}
437406

0 commit comments

Comments
 (0)