forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrangler: add AI related commands (cloudflare#3986)
* wrangler: add AI related commands * wrangler: truncate based on terminal size
- Loading branch information
Showing
13 changed files
with
996 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
Added AI related CLI commands |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as ListCatalog from "./listCatalog"; | ||
import type { CommonYargsArgv } from "../yargs-types"; | ||
|
||
export function ai(yargs: CommonYargsArgv) { | ||
return yargs.command( | ||
"models", | ||
"List catalog models", | ||
ListCatalog.options, | ||
ListCatalog.handler | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { withConfig } from "../config"; | ||
import { logger } from "../logger"; | ||
import { requireAuth } from "../user"; | ||
import { asJson } from "./options"; | ||
import { listCatalogEntries, truncate } from "./utils"; | ||
import type { | ||
CommonYargsArgv, | ||
StrictYargsOptionsToInterface, | ||
} from "../yargs-types"; | ||
|
||
export function options(yargs: CommonYargsArgv) { | ||
return asJson(yargs); | ||
} | ||
|
||
function truncateDescription( | ||
description: string | undefined, | ||
alreadyUsed: number | ||
): string { | ||
if (description === undefined || description === null) { | ||
return ""; | ||
} | ||
|
||
if (process.stdout.columns === undefined) { | ||
return truncate(description, 100); | ||
} | ||
|
||
return truncate(description, process.stdout.columns - alreadyUsed); | ||
} | ||
|
||
type HandlerOptions = StrictYargsOptionsToInterface<typeof options>; | ||
export const handler = withConfig<HandlerOptions>( | ||
async ({ json, config }): Promise<void> => { | ||
const accountId = await requireAuth(config); | ||
const entries = await listCatalogEntries(accountId); | ||
|
||
if (json) { | ||
logger.log(JSON.stringify(entries, null, 2)); | ||
} else { | ||
logger.table( | ||
entries.map((entry) => ({ | ||
model: entry.id, | ||
name: entry.name, | ||
description: truncateDescription( | ||
entry.description, | ||
entry.id.length + | ||
entry.name.length + | ||
(entry.task ? entry.task.name.length : 0) + | ||
10 | ||
), | ||
task: entry.task ? entry.task.name : "", | ||
})) | ||
); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { CommonYargsArgv } from "../yargs-types"; | ||
|
||
export function takeName(yargs: CommonYargsArgv) { | ||
return yargs.positional("name", { | ||
describe: "The name of the project", | ||
type: "string", | ||
demandOption: true, | ||
}); | ||
} | ||
|
||
export function asJson(yargs: CommonYargsArgv) { | ||
return yargs.option("json", { | ||
describe: "return output as clean JSON", | ||
type: "boolean", | ||
default: false, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type Task = { | ||
id: string; | ||
name: string; | ||
description: string; | ||
}; | ||
|
||
export type Model = { | ||
id: string; | ||
source: number; | ||
task?: Task; | ||
tags: string[]; | ||
name: string; | ||
description?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { fetchResult } from "../cfetch"; | ||
import type { Model } from "./types"; | ||
|
||
export async function aiList<ResponseType>( | ||
accountId: string, | ||
partialUrl: string | ||
): Promise<Array<ResponseType>> { | ||
const pageSize = 50; | ||
let page = 1; | ||
const results = []; | ||
while (results.length % pageSize === 0) { | ||
const json: Array<ResponseType> = await fetchResult( | ||
`/accounts/${accountId}/ai/${partialUrl}`, | ||
{}, | ||
new URLSearchParams({ | ||
per_page: pageSize.toString(), | ||
page: page.toString(), | ||
}) | ||
); | ||
page++; | ||
results.push(...json); | ||
if (json.length < pageSize) { | ||
break; | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
export const listCatalogEntries = async ( | ||
accountId: string | ||
): Promise<Array<Model>> => { | ||
return await aiList(accountId, "models/search"); | ||
}; | ||
|
||
export function truncate(str: string, maxLen: number) { | ||
return str.slice(0, maxLen) + (str.length > maxLen ? "..." : ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters