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
361 changes: 359 additions & 2 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion sdk/packages/node/iii-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "bun src/index.ts"
},
"dependencies": {
"iii-sdk": "workspace:*"
"iii-sdk": "workspace:*",
"zod": "^4.3.6"
},
"devDependencies": {
"@types/node": "^24.10.1",
Expand Down
25 changes: 25 additions & 0 deletions sdk/packages/node/iii-example/src/iii-zod-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from 'zod'
import { iii } from './iii'

const inputSchema = z.object({
scope: z.string(),
key: z.string(),
})

const outputSchema = z.object({
value: z.string(),
})

async function helloWorld(input: z.infer<typeof inputSchema>): Promise<z.infer<typeof outputSchema>> {
return { value: `${input.scope}::${input.key}` }
}

iii.registerFunction(
{
id: 'example::hello-world',
description: 'description',
request_format: z.toJSONSchema(inputSchema),
response_format: z.toJSONSchema(outputSchema),
},
helloWorld,
)
1 change: 1 addition & 0 deletions sdk/packages/node/iii-example/src/iii.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { registerWorker } from 'iii-sdk'
import { version } from '../package.json'

/** @ts-expect-error process.env is not typed */
// Engine WebSocket URL - used for both III and telemetry
const engineWsUrl = process.env.III_URL ?? 'ws://localhost:49134'

Expand Down
55 changes: 25 additions & 30 deletions sdk/packages/node/iii-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { state } from './state'
import { streams } from './stream'
import type { Todo } from './types'
import './http-example'
import './iii-zod-example'

useApi(
{
Expand Down Expand Up @@ -101,40 +102,34 @@ useApi(
},
)

useApi(
{ api_path: 'state', http_method: 'POST', description: 'Set application state' },
async (req, logger) => {
logger.info('Creating new todo', { body: req.body })
useApi({ api_path: 'state', http_method: 'POST', description: 'Set application state' }, async (req, logger) => {
logger.info('Creating new todo', { body: req.body })

const { description, dueDate } = req.body
const todoId = `todo-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
const { description, dueDate } = req.body
const todoId = `todo-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`

if (!description) {
return { status_code: 400, body: { error: 'Description is required' } }
}
if (!description) {
return { status_code: 400, body: { error: 'Description is required' } }
}

const newTodo: Todo = {
id: todoId,
description,
groupId: 'inbox',
createdAt: new Date().toISOString(),
dueDate: dueDate,
completedAt: null,
}
const todo = await state.set<Todo>({ scope: 'todo', key: todoId, data: newTodo })
const newTodo: Todo = {
id: todoId,
description,
groupId: 'inbox',
createdAt: new Date().toISOString(),
dueDate: dueDate,
completedAt: null,
}
const todo = await state.set<Todo>({ scope: 'todo', key: todoId, value: newTodo })

return { status_code: 201, body: todo, headers: { 'Content-Type': 'application/json' } }
},
)
return { status_code: 201, body: todo, headers: { 'Content-Type': 'application/json' } }
})

useApi(
{ api_path: 'state/:id', http_method: 'GET', description: 'Get state by ID' },
async (req, logger) => {
logger.info('Getting todo', { ...req.path_params })
useApi({ api_path: 'state/:id', http_method: 'GET', description: 'Get state by ID' }, async (req, logger) => {
logger.info('Getting todo', { ...req.path_params })

const todoId = req.path_params.id
const todo = await state.get<Todo | null>({ scope: 'todo', key: todoId })
const todoId = req.path_params.id
const todo = await state.get<Todo | null>({ scope: 'todo', key: todoId })

return { status_code: 200, body: todo, headers: { 'Content-Type': 'application/json' } }
},
)
return { status_code: 200, body: todo, headers: { 'Content-Type': 'application/json' } }
})
5 changes: 2 additions & 3 deletions sdk/packages/node/iii-example/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { iii } from './iii'
import type {
IState,
StateDeleteInput,
Expand All @@ -10,6 +9,7 @@ import type {
StateUpdateInput,
StateUpdateResult,
} from 'iii-sdk/state'
import { iii } from './iii'

export const state: IState = {
get: <TData>(input: StateGetInput): Promise<TData | null> =>
Expand All @@ -18,8 +18,7 @@ export const state: IState = {
iii.trigger({ function_id: 'state::set', payload: input }),
delete: (input: StateDeleteInput): Promise<StateDeleteResult> =>
iii.trigger({ function_id: 'state::delete', payload: input }),
list: <TData>(input: StateListInput): Promise<TData[]> =>
iii.trigger({ function_id: 'state::list', payload: input }),
list: <TData>(input: StateListInput): Promise<TData[]> => iii.trigger({ function_id: 'state::list', payload: input }),
update: <TData>(input: StateUpdateInput): Promise<StateUpdateResult<TData> | null> =>
iii.trigger({ function_id: 'state::update', payload: input }),
}
2 changes: 1 addition & 1 deletion sdk/packages/node/iii/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"devDependencies": {
"@types/ws": "^8.18.1",
"@vitest/coverage-v8": "^2.1.0",
"tsdown": "^0.17.0",
"tsdown": "^0.21.4",
"typedoc": "^0.28.17",
"typescript": "^5.9.3",
"vitest": "^2.1.0"
Expand Down
9 changes: 6 additions & 3 deletions sdk/packages/node/iii/src/iii-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export type HttpInvocationConfig = {
}

export type RegisterFunctionFormat = {
name: string
/**
* The name of the parameter
*/
name?: string
/**
* The description of the parameter
*/
Expand All @@ -96,15 +99,15 @@ export type RegisterFunctionFormat = {
/**
* The body of the parameter
*/
body?: RegisterFunctionFormat[]
properties?: Record<string, RegisterFunctionFormat>
/**
* The items of the parameter
*/
items?: RegisterFunctionFormat
/**
* Whether the parameter is required
*/
required?: boolean
required?: string[]
}

export type RegisterFunctionMessage = {
Expand Down
2 changes: 1 addition & 1 deletion sdk/packages/node/iii/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
dts: true,
sourcemap: true,
clean: true,
external: [],
minify: false,
treeshake: true,
deps: { neverBundle: [] },
})
Loading