Skip to content

The TypeScript SDK uploads a file and follows its ingest job - #281

Merged
zlareb1 merged 7 commits into
mainfrom
feat/ts-sdk-upload
Nov 6, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
feat/ts-sdk-upload

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The TypeScript SDK sends a file for the server's extractor to parse: loom.knowledge.upload posts its bytes, loom.knowledge.waitForIngest follows the ingest job until it ends, and loom.knowledge.ingestFile does both around knowledge.ingest, from Node or a browser (Blob, File).

Why

The issue moves every client onto the upload and ingest path. #280 gave the Python SDK and the CLIs that path. In the TypeScript SDK, knowledge.ingest, ingest_status and ingest_cancel were typed, but POST /v1/knowledge.upload takes a raw body outside the op registry, so a TypeScript caller (a Node service, or a web app holding a File) still had to write that request by hand: the query parameters, the sha256 and the credential.

Design

  • Transport (src/transport.ts):
    • postBytes(path, query, data) posts raw bytes as application/octet-stream to {baseUrl}/v1/{path}?{query}.
    • It uses the same send loop as every op: the credential, the request id, 429 pacing within ratelimitMaxWaitS, and the retry policy.
    • The server keeps no Idempotency-Key for the route, so it is retried, like any op the server does not deduplicate, only on an answer that proves it never ran. A 429 is answered before the server reads the body, so pacing it resends bytes that were never stored.
    • The attempt's payload is a JSON string or a byte array; nothing else in the loop changes. The public signatures use plain Uint8Array, so the published types still compile on TypeScript 5.6 (the package's floor); tsc passes on 5.6.3 and 5.9.3.
  • Helpers (src/files.ts), merged into loom.knowledge beside its generated ops:
    • upload(scope, data, filename, { contentType }) takes a Uint8Array, an ArrayBuffer or a Blob. It computes the sha256 with Web Crypto, which Node 20+ and browsers provide on https pages and localhost; where it is missing (a browser page on plain http) the upload goes without one, which the server accepts (it checks only a hash it is sent).
    • waitForIngest(scope, ingestionId, { timeoutMs, pollMs, signal }) polls until the job is ready, partial, failed or cancelled. A job not found returns at once. Past timeoutMs (600 s by default) it throws TimeoutError, naming the state the job is in. Between polls it sleeps on real timers (not a Transport's injected sleep) with the transport's sleep helper, which an abort cuts short and which leaves no listener behind, so an abort surfaces as the transport's aborted by caller error, as it does during a request.
    • ingestFile({ ...knowledge.ingest fields, data, filename }) uploads, ingests, and waits (or not, with wait: false). Its title defaults to the file name less its extension, as the CLI and the Python SDK title it. waitTimeoutMs bounds the wait; call carries the upload's and the ingest's request options (a longer timeoutMs for a large file over a slow link, a retry policy, a request id), and call.signal also stops the wait.
    • INGEST_DONE and the types are exported. The method names follow the Python SDK's (upload, wait_for_ingest, ingest_file), camel-cased.
  • Typing: LoomClient["knowledge"] is GroupClient<"knowledge"> & KnowledgeFiles, attached in the constructor; loom.group("knowledge") stays the ops alone, as its type says; every other group is unchanged.
  • Docs: a Files section in docs/reference/typescript-sdk.md, a type-checked example in the package README (upload, ingest and waitForIngest named as its steps), and the knowledge page's paragraph on the SDK helpers now names both SDKs.

Alternatives considered:

  • A separate loom.files client (like loom.account). The Python SDK has these on knowledge, and the upload's id feeds knowledge.ingest, so the knowledge group is where a caller looks.
  • fetch called directly in files.ts. That would bypass the credential provider, pacing and logging the transport owns.

Scope and risk

  • The TypeScript SDK only: no op, server, table or setting changes. The parity test (ops only) is unaffected, and the generated ops.ts is unchanged.
  • The SDK version is not bumped here; the release does that.

Compatibility, rollout and rollback

  • The helpers need a server that serves /v1/knowledge.upload and the ingest ops. An older one answers 404, raised as NotFound.
  • Existing calls are unchanged: a JSON op body is sent exactly as before.
  • Rollback: reverting removes the helpers.

Tests

  • Unit, src/files.test.ts (15 tests):
    • every exported file type is what loom.knowledge takes and answers (type-level; the repository's test inventory requires a test of each export);
    • the upload sends the bytes unchanged, with their sha256, the octet-stream type and the bearer;
    • with no Web Crypto the upload sends no sha256 and the same bytes;
    • a Blob, an ArrayBuffer and a view of part of a shared buffer each send just their bytes;
    • a 429 is paced by its hint and sent again, and with no pacing budget it raises RateLimited;
    • a 503 is not retried (the server may have stored the file), and a connection failure is;
    • waitForIngest polls until the job ends, returns at once for a job not found, throws TimeoutError naming the state, and stops at an abort mid-sleep with the transport's error;
    • INGEST_DONE names the four end states;
    • ingestFile uploads (its contentType reaching the upload), ingests and waits with the stem as its title, and sends none of data, filename or contentType to knowledge.ingest;
    • ingestFile's answer is typed by wait: the status, the job, or either for a wait that is not a literal (a type-level test);
    • ingestFile's wait stops at call.signal's abort and throws TimeoutError past waitTimeoutMs;
    • wait: false returns the job, a title given is kept, and call's request id reaches both the upload and the ingest (the server keeps the id as a trace label only, so one id on both requests is fine).
  • Unit, updated: the package's export list (INGEST_DONE).
  • Live, test/integ/live.test.ts: against a server and its ClickHouse, the upload answers its format and size, and ingestFile({ wait: false }) queues a job that knowledge.ingest_status finds, with its file name and source. All 5 live tests pass. Against a server whose upload cap was set to 1,000 bytes, a 200 kB and a 50 MB upload from the built package each answered 413 PAYLOAD_TOO_LARGE after one send (the refusal arrives as an answer, not a dropped connection, so it is not resent).
  • npm run check (lint, type-check, version, the README's examples type-checked, 259 unit tests) and npm run check:dist (build, dist tests, publint, attw) pass, as CI runs them.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. Checked: the helpers against a real server, the failure paths in the unit tests above, and a review at every push.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 7 commits November 6, 2026 03:05
…ons; the helpers type on TypeScript 5.6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ll; the wait's timers are documented

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… per helper

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…entType is tested

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…at can leave

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 18a2444 into main Nov 6, 2026
19 of 20 checks passed
@zlareb1
zlareb1 deleted the feat/ts-sdk-upload branch November 6, 2026 04:21
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant