Skip to content

Commit ca1ac82

Browse files
committed
log tokens in convert
1 parent f8317c5 commit ca1ac82

5 files changed

Lines changed: 49 additions & 22 deletions

File tree

packages/cli/src/convert.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
2-
CONVERTS_DIR_NAME,
32
FILES_NOT_FOUND_ERROR_CODE,
43
GENAI_ANY_REGEX,
5-
GENAI_ANYTS_REGEX,
64
HTTPS_REGEX,
75
JSON5_REGEX,
86
TRACE_FILENAME,
@@ -30,6 +28,8 @@ import { toSignal } from "../../core/src/cancellation"
3028
import { normalizeInt } from "../../core/src/cleaners"
3129
import { YAMLStringify } from "../../core/src/yaml"
3230
import { ensureDotGenaiscriptPath, getConvertDir } from "../../core/src/workdir"
31+
import { GenerationStats } from "../../core/src/usage"
32+
import { measure } from "../../core/src/performance"
3333

3434
export async function convertFiles(
3535
scriptId: string,
@@ -134,6 +134,7 @@ export async function convertFiles(
134134
(filename) => ({ filename }) as WorkspaceFile
135135
)
136136

137+
const usage = new GenerationStats("")
137138
const results: Record<string, string> = {}
138139
const p = new PLimitPromiseQueue(normalizeInt(concurrency) || 1)
139140
await p.mapAll(files, async (file) => {
@@ -147,6 +148,7 @@ export async function convertFiles(
147148
const fileTrace = convertTrace.startTraceDetails(file.filename)
148149
convertTrace.item(link("trace", fileOutTrace))
149150
logVerbose(`trace: ${fileOutTrace}`)
151+
const m = measure("convert")
150152
try {
151153
// apply AI transformation
152154
const result = await run(script.filename, file.filename, {
@@ -228,6 +230,8 @@ export async function convertFiles(
228230
}
229231

230232
results[file.filename] = text
233+
const end = m()
234+
usage.addUsage(result.stats, end)
231235
} catch (error) {
232236
logError(error)
233237
fileTrace.error(undefined, error)
@@ -237,5 +241,6 @@ export async function convertFiles(
237241
}
238242
})
239243

244+
usage.log()
240245
logVerbose(`trace: ${outTraceFilename}`)
241246
}

packages/core/src/chat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ async function processChatMessage(
787787
choices,
788788
} = options
789789

790-
stats.addUsage(req, resp)
790+
stats.addRequestUsage(req, resp)
791791
const assisantMessage = parseAssistantMessage(resp)
792792
if (assisantMessage) {
793793
messages.push(assisantMessage)

packages/core/src/promptrunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async function resolveExpansionVars(
118118
// Main function to run a template with given options
119119
/**
120120
* Executes a prompt template with specified options.
121-
*
121+
*
122122
* @param prj The project context providing runtime and configuration.
123123
* @param template The prompt script template to execute.
124124
* @param fragment Additional context such as files and metadata.

packages/core/src/usage.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,9 @@ export class GenerationStats {
301301
logVerbose(`missing pricing for ${[...unknowns].join(", ")}`)
302302
}
303303

304-
/**
305-
* Adds usage statistics to the current instance.
306-
*
307-
* @param req - The request containing details about the chat completion.
308-
* @param usage - The usage statistics to be added.
309-
*/
310-
addUsage(req: CreateChatCompletionRequest, resp: ChatCompletionResponse) {
311-
const {
312-
usage = { completion_tokens: 0, prompt_tokens: 0, total_tokens: 0 },
313-
model,
314-
cached,
315-
duration,
316-
} = resp
317-
const { messages } = req
318-
319-
if (!cached) {
320-
this.usage.duration += duration ?? 0
321-
304+
addUsage(usage: ChatCompletionUsage, duration?: number) {
305+
this.usage.duration += duration ?? 0
306+
if (usage) {
322307
this.usage.completion_tokens += usage.completion_tokens ?? 0
323308
this.usage.prompt_tokens += usage.prompt_tokens ?? 0
324309
this.usage.total_tokens += usage.total_tokens ?? 0
@@ -336,6 +321,29 @@ export class GenerationStats {
336321
this.usage.completion_tokens_details.rejected_prediction_tokens +=
337322
usage.completion_tokens_details?.rejected_prediction_tokens ?? 0
338323
}
324+
}
325+
326+
/**
327+
* Adds usage statistics to the current instance.
328+
*
329+
* @param req - The request containing details about the chat completion.
330+
* @param usage - The usage statistics to be added.
331+
*/
332+
addRequestUsage(
333+
req: CreateChatCompletionRequest,
334+
resp: ChatCompletionResponse
335+
) {
336+
const {
337+
usage = { completion_tokens: 0, prompt_tokens: 0, total_tokens: 0 },
338+
model,
339+
cached,
340+
duration,
341+
} = resp
342+
const { messages } = req
343+
344+
if (!cached) {
345+
this.addUsage(usage, duration)
346+
}
339347

340348
const { provider } = parseModelIdentifier(this.model)
341349
const chatTurn = {

packages/sample/genaisrc/ast-docs-update.genai.mts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const { output } = env
1414
const { applyEdits } = env.vars
1515
const file = env.files[0]
1616

17+
const prettier = async (file) => {
18+
// format
19+
const res = await host.exec("prettier", [
20+
"--write",
21+
file.filename,
22+
])
23+
if (!res.exitCode) throw new Error(res.stderr)
24+
}
25+
26+
// normalize spacing
27+
await prettier(file)
28+
1729
// find all exported functions with comments
1830
const sg = await host.astGrep()
1931
const { matches, replace, commitEdits } = await sg.search("ts", file.filename, {
@@ -79,6 +91,8 @@ for (const match of matches) {
7991
const modified = await commitEdits()
8092
if (applyEdits) {
8193
await workspace.writeFiles(modified)
94+
// normalize spacing
95+
await prettier(file)
8296
} else if (modified.length) {
8397
output.diff(file, modified[0])
8498
output.warn(

0 commit comments

Comments
 (0)