Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion packages/cli/src/serve/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,8 @@ describe('createServeApp', () => {
});

expect(res.status).toBe(202);
expect(res.body).toEqual({ accepted: true });
expect(res.body).toMatchObject({ accepted: true });
expect(res.body.operationId).toEqual(expect.any(String));
await vi.waitFor(() => {
expect(bridge.extensionEvents.at(-1)).toMatchObject({
status: 'installed',
Expand All @@ -2698,11 +2699,55 @@ describe('createServeApp', () => {
expect(requestSettingError).toContain(
'requires interactive configuration',
);

const poll = await request(app)
.get(
`/workspace/extensions/operations/${encodeURIComponent(
res.body.operationId as string,
)}`,
)
.set('Host', `127.0.0.1:${tokenOpts.port}`)
.set('Authorization', 'Bearer secret');
expect(poll.status).toBe(200);
expect(poll.body).toMatchObject({
v: 1,
operationId: res.body.operationId,
operation: 'install',
status: 'succeeded',
source: 'https://example.com/installed-ext',
result: {
status: 'installed',
name: 'installed-ext',
version: '1.2.3',
refreshed: 1,
failed: 0,
},
});
} finally {
restore();
}
});

it('returns 404 for unknown extension operation ids', async () => {
const tokenOpts: ServeOptions = { ...baseOpts, token: 'secret' };
const bridge = fakeBridge({ knownClientIds: ['client-1'] });
const app = createServeApp(
{ ...tokenOpts, workspace: WS_BOUND },
undefined,
{ bridge },
);

const res = await request(app)
.get('/workspace/extensions/operations/missing-operation')
.set('Host', `127.0.0.1:${tokenOpts.port}`)
.set('Authorization', 'Bearer secret');

expect(res.status).toBe(404);
expect(res.body).toMatchObject({
code: 'extension_operation_not_found',
});
});

it('broadcasts a failed extension install with redacted error details', async () => {
const restore = mockExtensionManagerMethods({
installExtension: async () => {
Expand All @@ -2729,6 +2774,7 @@ describe('createServeApp', () => {
});

expect(res.status).toBe(202);
expect(res.body.operationId).toEqual(expect.any(String));
await vi.waitFor(() => {
expect(bridge.extensionEvents.at(-1)).toMatchObject({
status: 'failed',
Expand All @@ -2739,6 +2785,24 @@ describe('createServeApp', () => {
});
});
expect(bridge.extensionEvents.at(-1)?.error).not.toContain('token');

const poll = await request(app)
.get(
`/workspace/extensions/operations/${encodeURIComponent(
res.body.operationId as string,
)}`,
)
.set('Host', `127.0.0.1:${tokenOpts.port}`)
.set('Authorization', 'Bearer secret');
expect(poll.status).toBe(200);
expect(poll.body).toMatchObject({
operationId: res.body.operationId,
operation: 'install',
status: 'failed',
source: 'https://example.com/private-ext',
error: 'https://***REDACTED***@example.com/private-ext failed',
});
expect(poll.body.error).not.toContain('token');
} finally {
restore();
}
Expand Down Expand Up @@ -2773,6 +2837,7 @@ describe('createServeApp', () => {
});

expect(res.status).toBe(202);
expect(res.body.operationId).toEqual(expect.any(String));
await vi.waitFor(() => {
expect(bridge.extensionEvents.at(-1)).toMatchObject({
status: 'installed',
Expand All @@ -2783,6 +2848,27 @@ describe('createServeApp', () => {
error: 'refresh broke',
});
});

const poll = await request(app)
.get(
`/workspace/extensions/operations/${encodeURIComponent(
res.body.operationId as string,
)}`,
)
.set('Host', `127.0.0.1:${tokenOpts.port}`)
.set('Authorization', 'Bearer secret');
expect(poll.status).toBe(200);
expect(poll.body).toMatchObject({
operationId: res.body.operationId,
operation: 'install',
status: 'succeeded_with_refresh_error',
result: {
status: 'installed',
refreshed: 0,
failed: 1,
error: 'refresh broke',
},
});
} finally {
restore();
}
Expand Down
143 changes: 132 additions & 11 deletions packages/cli/src/serve/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ export function createServeApp(
extensionInstallQueue = next.catch(() => undefined);
return next;
};
const EXTENSION_MUTATION_TIMEOUT_MS = 120_000;
const EXTENSION_MUTATION_TIMEOUT_MS = 10 * 60_000;
const EXTENSION_REFRESH_TIMEOUT_MS = 30_000;
const isExtensionQueueFullError = (err: unknown): boolean =>
err instanceof Error && err.message === 'Extension operation queue is full';
Expand Down Expand Up @@ -1429,6 +1429,51 @@ export function createServeApp(
name?: string;
version?: string;
};
type ExtensionOperationStatus = {
v: 1;
operationId: string;
operation: string;
status:
| 'queued'
| 'running'
| 'succeeded'
| 'succeeded_with_refresh_error'
| 'failed';
createdAt: number;
updatedAt: number;
source?: string;
name?: string;
result?: ExtensionMutationEvent & {
refreshed?: number;
failed?: number;
error?: string;
};
error?: string;
};
const extensionOperations = new Map<string, ExtensionOperationStatus>();
const MAX_EXTENSION_OPERATION_HISTORY = 100;
const rememberExtensionOperation = (
operation: ExtensionOperationStatus,
): void => {
extensionOperations.set(operation.operationId, operation);
while (extensionOperations.size > MAX_EXTENSION_OPERATION_HISTORY) {
Comment thread
ytahdn marked this conversation as resolved.
Resolved
const oldest = extensionOperations.keys().next().value;
if (typeof oldest !== 'string') break;
extensionOperations.delete(oldest);
Comment thread
ytahdn marked this conversation as resolved.
Resolved
}
};
const updateExtensionOperation = (
operationId: string,
patch: Partial<Omit<ExtensionOperationStatus, 'operationId' | 'createdAt'>>,
): void => {
const current = extensionOperations.get(operationId);
if (!current) return;
extensionOperations.set(operationId, {
...current,
...patch,
updatedAt: Date.now(),
});
};
const runQueuedExtensionMutation = (
operation: string,
failureContext: { source?: string; name?: string },
Expand All @@ -1441,9 +1486,24 @@ export function createServeApp(
sendExtensionQueueFull(res);
return;
}
res.status(202).json({ accepted: true });
const operationId = crypto.randomUUID();
const now = Date.now();
rememberExtensionOperation({
v: 1,
operationId,
operation,
status: 'queued',
createdAt: now,
updatedAt: now,
...(failureContext.source
? { source: redactUrlCredentials(failureContext.source) }
: {}),
...(failureContext.name ? { name: failureContext.name } : {}),
});
res.status(202).json({ accepted: true, operationId });
void enqueueExtensionInstall(async () => {
try {
updateExtensionOperation(operationId, { status: 'running' });
const extensionManager = createExtensionManager();
await extensionManager.refreshCache();
const event = await withExtensionTimeout(
Expand All @@ -1454,6 +1514,14 @@ export function createServeApp(
extensionsStatusCache = undefined;
try {
const result = await bridge.refreshExtensionsForAllSessions(event);
updateExtensionOperation(operationId, {
status: 'succeeded',
result: {
...event,
Comment thread
ytahdn marked this conversation as resolved.
Resolved
refreshed: result.refreshed,
failed: result.failed,
},
});
writeStderrLine(
`qwen serve: extensions ${operation}: refreshed ${result.refreshed} session(s), ${result.failed} failed`,
);
Expand All @@ -1463,6 +1531,15 @@ export function createServeApp(
? refreshErr.message
: String(refreshErr),
);
updateExtensionOperation(operationId, {
status: 'succeeded_with_refresh_error',
result: {
...event,
refreshed: 0,
failed: 1,
error: message.slice(0, 500),
},
});
try {
bridge.broadcastExtensionsChanged({
...event,
Expand All @@ -1487,6 +1564,10 @@ export function createServeApp(
const message = redactUrlCredentials(
err instanceof Error ? err.message : String(err),
);
updateExtensionOperation(operationId, {
Comment thread
ytahdn marked this conversation as resolved.
Resolved
status: 'failed',
error: message.slice(0, 500),
});
try {
bridge.broadcastExtensionsChanged({
status: 'failed',
Expand Down Expand Up @@ -2096,6 +2177,33 @@ export function createServeApp(
}
});

app.get('/workspace/extensions/operations/:operationId', async (req, res) => {
try {
buildWorkspaceCtx(
req,
'GET /workspace/extensions/operations/:operationId',
);
const operationId = req.params['operationId'];
if (!operationId) {
res.status(400).json({ error: 'Missing extension operation id' });
return;
}
const operation = extensionOperations.get(operationId);
if (!operation) {
res.status(404).json({
error: `Extension operation "${operationId}" not found`,
code: 'extension_operation_not_found',
});
return;
}
res.status(200).json(operation);
} catch (err) {
sendBridgeError(res, err, {
route: 'GET /workspace/extensions/operations/:operationId',
});
}
});

// POST /workspace/extensions/install — install an extension and refresh
// all active sessions asynchronously.
app.post(
Expand Down Expand Up @@ -2149,9 +2257,17 @@ export function createServeApp(
res.status(400).json({ error: '`registry` must be a string' });
return;
}
const sourceValue = source;
const refValue = typeof ref === 'string' ? ref : undefined;
const autoUpdateValue =
typeof autoUpdate === 'boolean' ? autoUpdate : undefined;
const allowPreReleaseValue =
typeof allowPreRelease === 'boolean' ? allowPreRelease : undefined;
const registryValue =
typeof registry === 'string' ? registry : undefined;
const registryUrl =
registry !== undefined
? parseExtensionRegistryUrl(registry, res)
registryValue !== undefined
? parseExtensionRegistryUrl(registryValue, res)
: undefined;
if (registryUrl === null) return;
if (consent !== true) {
Expand All @@ -2160,16 +2276,16 @@ export function createServeApp(
});
return;
}
if (!validateExtensionSourceHost(source, res)) {
if (!validateExtensionSourceHost(sourceValue, res)) {
return;
}

runQueuedExtensionMutation(
'install',
{ source },
{ source: sourceValue },
res,
async (extensionManager) => {
const installMetadata = await parseInstallSource(source);
const installMetadata = await parseInstallSource(sourceValue);

if (
installMetadata.type !== 'git' &&
Expand All @@ -2180,10 +2296,10 @@ export function createServeApp(
'Only GitHub, Git, and npm extension installs are supported over the daemon endpoint.',
);
}
if (installMetadata.type === 'npm' && ref) {
if (installMetadata.type === 'npm' && refValue) {
throw new Error('--ref is not applicable for npm extensions.');
}
if (installMetadata.type !== 'npm' && registry) {
if (installMetadata.type !== 'npm' && registryValue) {
throw new Error(
'--registry is only applicable for npm extensions.',
);
Expand All @@ -2195,12 +2311,17 @@ export function createServeApp(
installMetadata.registryUrl = registryUrl;
}
const extension = await extensionManager.installExtension(
{ ...installMetadata, ref, autoUpdate, allowPreRelease },
{
...installMetadata,
ref: refValue,
autoUpdate: autoUpdateValue,
allowPreRelease: allowPreReleaseValue,
},
() => Promise.resolve(),
);
return {
status: 'installed',
source,
source: sourceValue,
name: extension.name,
version: extension.config.version,
};
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-typescript/src/daemon/DaemonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import type {
ExtensionMutationResponse,
ExtensionInstallRequest,
ExtensionInstallResponse,
ExtensionOperationStatus,
ExtensionScopeRequest,
ExtensionRefreshResponse,
ExtensionUpdateCheckResponse,
Expand Down Expand Up @@ -710,6 +711,15 @@ export class DaemonClient {
);
}

async extensionOperationStatus(
operationId: string,
): Promise<ExtensionOperationStatus> {
return await this.jsonRequest<ExtensionOperationStatus>(
`/workspace/extensions/operations/${encodeURIComponent(operationId)}`,
'GET /workspace/extensions/operations/:operationId',
);
}

async checkExtensionUpdates(
clientId?: string,
): Promise<ExtensionUpdateCheckResponse> {
Expand Down
Loading
Loading