Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CODEBASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ don't duplicate it here.
`shouldDisableSpinner`, progress-jsonl getters (layered on cli-core's parser).
- **`permissions.ts`** — `ensureWriteAllowed`, `isMutatingMethod` (read-only-scope guard).
- **`markdown.ts`** — `preloadMarkdown` / `renderMarkdown` (cli-core renderer).
- **`usage-tracking.ts`** — `usageTracking` (cli-core's `createUsageTracking`:
the `User-Agent` / `doist-*` / `request-id` / `session-id` / `cli-command` headers on every
request), `trackCommandUsage(program)` (the `preAction` hook that sets
`cli-command`; `tdc view` adds it to its routing program too), and
`withUsageHeaders(fetch)` for requests that bypass the SDK. Any new raw
`fetch` to Comms or the OAuth server goes through `usageTracking.mergeHeaders`.
- **`search-api.ts` / `search-helpers.ts`** — extended search params/response
(`extendedSearch`) + shared `--search`/options wiring (`addSharedSearchOptions`,
`runSearch`, `printSearchResults`). See `docs/comms-search.md`.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"CHANGELOG.md"
],
"dependencies": {
"@doist/cli-core": "1.8.0",
"@doist/cli-core": "1.9.0",
"@doist/comms-sdk": "3.11.0",
"@pnpm/tabtab": "0.5.4",
"chalk": "5.6.2",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Command } from 'commander'
import { configureCommandOutput } from '../lib/command-output.js'
import { CliError } from '../lib/errors.js'
import { classifyCommsUrl } from '../lib/refs.js'
import { trackCommandUsage } from '../lib/usage-tracking.js'

function extractViewInvocation(parsedUrl: string): {
url: string
Expand All @@ -18,7 +19,9 @@ async function runRoutedCommand(
loadRegister: () => Promise<(p: Command) => void>,
argv: string[],
): Promise<void> {
const proxy = configureCommandOutput(new Command())
// Its own hook relabels the requests from `view` to the routed command
// (`thread:view`, `msg:view`, ...), which is what actually runs.
const proxy = trackCommandUsage(configureCommandOutput(new Command()))
const register = await loadRegister()
register(proxy)
await proxy.parseAsync(['node', 'tdc', ...argv])
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getRequestedUserRef, isIdsOnlyMode, isJsonMode, isNdjsonMode } from './
import { preloadMarkdown } from './lib/markdown.js'
import { formatError, formatErrorJson } from './lib/output.js'
import { startEarlySpinner, stopEarlySpinner } from './lib/spinner.js'
import { trackCommandUsage } from './lib/usage-tracking.js'

const loadWorkspaceCommand = async () =>
(await import('./commands/workspace.js')).registerWorkspaceCommand
Expand Down Expand Up @@ -113,6 +114,7 @@ Note for AI/LLM agents:
)

configureCommandOutput(program)
trackCommandUsage(program)

// Register lightweight placeholders so --help lists all commands
for (const [name, [description]] of Object.entries(commands)) {
Expand Down
33 changes: 25 additions & 8 deletions src/lib/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import packageJson from '../../package.json' with { type: 'json' }

import { CliError } from './errors.js'
import { usageTracking } from './usage-tracking.js'

// Hoisted mocks — shared across both describe blocks.
const getWorkspaceUsersMock = vi.hoisted(() => vi.fn().mockResolvedValue([]))
Expand Down Expand Up @@ -134,22 +135,38 @@ describe('wrapResult — central 403 translation', () => {

expect(sdkMocks.createClient).toHaveBeenCalledWith('test-token', {
baseUrl: 'https://comms.staging.todoist.com',
headers: {
'Doist-Platform': 'cli',
'Doist-Version': packageJson.version,
},
headers: expect.any(Function),
})
})

it('computes the CLI usage headers afresh for each SDK request', () => {
createWrappedCommsClient('test-token')
const [, options] = sdkMocks.createClient.mock.calls[0] as [
string,
{ headers: () => Record<string, string> },
]

usageTracking.setCommand('channel:list')
const first = options.headers()
usageTracking.setCommand('thread:view')
const second = options.headers()

expect(first).toMatchObject({
'User-Agent': `comms-cli/${packageJson.version}`,
'doist-platform': 'cli',
'doist-version': packageJson.version,
'cli-command': 'channel:list',
})
expect(second['cli-command']).toBe('thread:view')
expect(second['session-id']).toBe(first['session-id'])
})

it('uses the active OAuth account resource when creating the shared SDK client', async () => {
await getCommsClient()

expect(sdkMocks.createClient).toHaveBeenCalledWith('test-token', {
baseUrl: 'https://comms.staging.todoist.com',
headers: {
'Doist-Platform': 'cli',
'Doist-Version': packageJson.version,
},
headers: expect.any(Function),
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
type Workspace,
type WorkspaceUser,
} from '@doist/comms-sdk'
import { DOIST_ATTRIBUTION_HEADERS } from './attribution.js'
import { getApiTokenSnapshot } from './auth.js'
import { getConfig, updateConfig } from './config.js'
import {
Expand All @@ -25,6 +24,7 @@ import {
import { ensureMutationAllowed, isMutatingMethod } from './permissions.js'
import { getProgressTracker } from './progress.js'
import { withSpinner } from './spinner.js'
import { usageTracking } from './usage-tracking.js'

// Mapping of API method paths to user-friendly spinner messages
const API_SPINNER_MESSAGES: Record<string, { text: string; color?: 'blue' | 'green' | 'yellow' }> =
Expand Down Expand Up @@ -328,7 +328,7 @@ export function createWrappedCommsClient(
const baseUrl = process.env.COMMS_BASE_URL ?? options.baseUrl
const rawApi = new CommsApi(token, {
...(baseUrl ? { baseUrl } : {}),
headers: DOIST_ATTRIBUTION_HEADERS,
headers: () => usageTracking.headers(),
})
return createSpinnerWrappedApi(rawApi)
}
Expand Down
7 changes: 0 additions & 7 deletions src/lib/attribution.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/lib/auth-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import packageJson from '../../package.json' with { type: 'json' }

vi.mock('./api.js', () => ({ createWrappedCommsClient: vi.fn() }))

Expand Down Expand Up @@ -50,6 +51,7 @@ import {
matchCommsAccount,
READ_WRITE_SCOPES,
} from './auth-provider.js'
import { usageTracking } from './usage-tracking.js'

const mockCreateClient = vi.mocked(createWrappedCommsClient)
const TOKEN_ENV_VAR = 'COMMS_API_TOKEN'
Expand Down Expand Up @@ -221,6 +223,57 @@ describe('createCommsAuthProvider', () => {
},
)

it.each([
[
'the code exchange',
(provider: ReturnType<typeof createCommsAuthProvider>) =>
provider.exchangeCode({
code: 'code_123',
state: 'state_123',
redirectUri: 'http://localhost:8766/callback',
handshake: {
clientId: OAUTH_CLIENT_ID,
codeVerifier: 'verifier_123',
authBaseUrl: 'https://todoist.com',
resource: 'https://comms.todoist.com',
},
}),
],
[
'a refresh',
(provider: ReturnType<typeof createCommsAuthProvider>) =>
provider.refreshToken!({
refreshToken: 'refresh_old',
handshake: {
clientId: OAUTH_CLIENT_ID,
authBaseUrl: 'https://todoist.com',
resource: 'https://comms.todoist.com',
authScope: READ_WRITE_SCOPES.join(' '),
},
}),
],
])('sends the CLI usage headers with %s token request', async (_, request) => {
const fetchImpl = vi.fn().mockResolvedValue(
jsonResponse({
token_type: 'bearer',
access_token: 'tdc_access',
expires_in: 3600,
}),
)
usageTracking.setCommand('auth:login')

await request(createCommsAuthProvider(fetchImpl))

const headers = (fetchImpl.mock.calls[0][1] as RequestInit).headers
expect(headers).toMatchObject({
'content-type': expect.stringContaining('application/x-www-form-urlencoded'),
'user-agent': `comms-cli/${packageJson.version}`,
'doist-platform': 'cli',
'doist-version': packageJson.version,
'cli-command': 'auth:login',
})
})

it('keeps stored account scope metadata when a refresh response omits scope', async () => {
const fetchImpl = vi.fn().mockResolvedValue(
jsonResponse({
Expand Down
3 changes: 2 additions & 1 deletion src/lib/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { type AuthMode, getConfig, getConfigPath } from './config.js'
import { CliError } from './errors.js'
import { parseRef } from './refs.js'
import { splitScopeString } from './scopes.js'
import { withUsageHeaders } from './usage-tracking.js'
import { createCommsUserRecordStore, getDefaultUserRecord } from './user-records.js'

const DEFAULT_TODOIST_AUTH_BASE_URL = 'https://todoist.com'
Expand Down Expand Up @@ -155,7 +156,7 @@ export function createCommsAuthProvider(
clientId: ({ handshake }) => resolveClientId(handshake),
validate: validateCommsToken,
errorHints: AUTH_HINTS,
fetchImpl,
fetchImpl: withUsageHeaders(fetchImpl),
})

return {
Expand Down
19 changes: 15 additions & 4 deletions src/lib/search-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('./auth.js', () => ({
}))

import { extendedSearch } from './search-api.js'
import { usageTracking } from './usage-tracking.js'

const fetchMock = vi.fn()

Expand Down Expand Up @@ -57,11 +58,21 @@ describe('extendedSearch', () => {
expect(requestUrl.searchParams.get('type')).toBe('conversations')
})

it('attributes the request to the CLI via Doist-Platform', async () => {
it('sends the CLI usage headers alongside its own', async () => {
usageTracking.setCommand('search')

await extendedSearch({ workspaceId: 69, query: 'roadmap' })

const headers = fetchMock.mock.calls[0][1].headers as Record<string, string>
expect(headers['Doist-Platform']).toBe('cli')
expect(headers['Doist-Version']).toBe(packageJson.version)
expect(fetchMock.mock.calls[0][1].headers).toEqual({
authorization: 'Bearer oauth-token',
'content-type': 'application/json',
'user-agent': `comms-cli/${packageJson.version}`,
'doist-platform': 'cli',
'doist-version': packageJson.version,
'doist-os': expect.stringMatching(/^(macos|linux|windows|unknown)$/),
'request-id': expect.any(String),
'session-id': expect.any(String),
'cli-command': 'search',
})
})
})
7 changes: 3 additions & 4 deletions src/lib/search-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEFAULT_API_VERSION, type SearchResult } from '@doist/comms-sdk'
import { DOIST_ATTRIBUTION_HEADERS } from './attribution.js'
import { getApiTokenSnapshot } from './auth.js'
import { CliError } from './errors.js'
import { usageTracking } from './usage-tracking.js'

function getBaseUrl(authResource?: string): string {
const override = process.env.COMMS_BASE_URL
Expand Down Expand Up @@ -103,11 +103,10 @@ export async function extendedSearch(

const response = await fetch(url, {
method: 'GET',
headers: {
headers: usageTracking.mergeHeaders({
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
...DOIST_ATTRIBUTION_HEADERS,
},
}),
})

if (!response.ok) {
Expand Down
Loading
Loading